swift 音频 - 从阵列中的几个播放声音(木琴)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 音频 - 从阵列中的几个播放声音(木琴)相关的知识,希望对你有一定的参考价值。
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer : AVAudioPlayer!
var selectedSoundFileName : String = ""
let soundArray = ["note1", "note2", "note3", "note4", "note5", "note6", "note7"]
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func notePressed(_ sender: UIButton) {
selectedSoundFileName = soundArray[sender.tag - 1]
playSound()
}
func playSound() {
let soundURL = Bundle.main.url(forResource: selectedSoundFileName, withExtension: "wav")
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
}
catch {
print(error)
}
audioPlayer.play()
}
}
以上是关于swift 音频 - 从阵列中的几个播放声音(木琴)的主要内容,如果未能解决你的问题,请参考以下文章