早上好
如何编写代码以停止此代码中的多个音频文件
我找了它,没找到任何有用的东西请帮帮我
import UIKit
import AVFoundation
class ViewController: UIViewController {
let soundFilenames = ["001" , "002" , "003" , "004" , "005" , "006" , "007" , "008"]
var Audios = [AVAudioPlayer]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for sound in soundFilenames {
do {
let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
Audios.append(audioPlayer)
}catch{
Audios.append(AVAudioPlayer())
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttun001(sender: UIButton) {
let audioplayer = Audios [sender.tag]
audioplayer.play()
}
}
答案 0 :(得分:0)
使用正常循环,检查是否正在播放并停止播放
func stopAudios(){
for audioPLayer in Audios{
if audioPLayer.playing{
audioPLayer.stop()
}
}
}
也不要忘记准备AVAudioPlayer
audioPlayer.prepareToPlay()