我试图一个接一个地播放几个音频文件。
这里是我必须播放1个音频文件名" 1"的代码。谁能帮助我们播放一组音频文件顺序?
import UIKit
import AVFoundation
class ViewController: UIViewController {
var ding:AVAudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
prepareAudios()
ding.play()
}
func prepareAudios() {
var path = NSBundle.mainBundle().pathForResource("1", ofType: "mp3")
ding = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!), error: nil)
ding.prepareToPlay()
}
}
答案 0 :(得分:1)
您可以使用AVQueuePlayer按顺序播放多个音频。
class ViewController: UIViewController {
var playerQueue: AVQueuePlayer = {
let url1 = NSBundle.mainBundle().URLForResource("UpTown", withExtension: "mp3")!
let url2 = NSBundle.mainBundle().URLForResource("TurnMeOn", withExtension: "mp3")!
let item1 = AVPlayerItem(URL: url1)
let item2 = AVPlayerItem(URL: url2)
let queue = AVQueuePlayer(items: [item1, item2])
return queue
}()
override func viewDidLoad() {
super.viewDidLoad()
playerQueue.play()
}
}