[英]swift- playing a series of audio files
嗨,我想一个接一个地播放5个音频文件。
我有一些代码将发挥作用。
有谁知道如何一个接一个地播放一组音频文件?
谢谢
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayer: AVAudioPlayer?
/* The delegate message that will let us know that the player
has finished playing an audio file */
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!,
successfully flag: Bool) {
println("Finished playing the song")
}
override func viewDidLoad() {
super.viewDidLoad()
let dispatchQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(dispatchQueue, {[weak self] in
let mainBundle = NSBundle.mainBundle()
/* Find the location of our file to feed to the audio player */
var x = 1
let filePath = mainBundle.pathForResource("\(x)", ofType:"mp3")
// }
if let path = filePath{
let fileData = NSData(contentsOfFile: path)
var error:NSError?
/* Start the audio player */
self!.audioPlayer = AVAudioPlayer(data: fileData, error: &error)
/* Did we get an instance of AVAudioPlayer? */
if let player = self!.audioPlayer{
/* Set the delegate and start playing */
player.delegate = self
if player.prepareToPlay() && player.play(){
/* Successfully started playing */
} else {
/* Failed to play */
}
} else {
/* Failed to instantiate AVAudioPlayer */
}
}
})
}
}
您可以使用ColiseuPlayer ,它是用Swift编写的音频播放器框架。 它仍处于测试阶段,但您可以使用它或检查代码。
演示:
import UIKit
import ColiseuPlayer
class ViewController: UIViewController {
let player = ColiseuPlayer()
override func viewDidLoad() {
super.viewDidLoad()
var list = [AudioFile]()
if let urlFile = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("1.m4a", ofType: nil)!) {
list.append(AudioFile(url: urlFile))
}
if let urlFile = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("2.m4a", ofType: nil)!) {
list.append(AudioFile(url: urlFile))
}
if list.count > 0 {
// Play first song (it will continue playing with the current playlist)
player.playSong(0, songsList: list)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.