Reputation: 21
hi I want to play 5 audio files one after the other.
I have some code that will play one.
does anyone know how to play a group of audiofiles one after the other?
thanks
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 */
}
}
})
}
}
Upvotes: 1
Views: 923
Reputation: 11683
You can use ColiseuPlayer, is an audio player framework written in Swift. It still in beta but you can use it or check the code.
Demo:
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)
}
}
}
Upvotes: 1