//AVAudioPlayer播放音乐
1.需要导入系统库 AVFoundation
let pathStr = NSBundle.mainBundle().pathForResource("泡沫.mp3", ofType: nil) //获取音乐路径
let url = NSURL(fileURLWithPath: pathStr!) //获取本地URL
_player = AVAudioPlayer(contentsOfURL: url, error: nil) //初始化音乐播放器
_player?.delegate = self //代理处理中断事件
_player?.numberOfLoops = -1 //循环次数 -1表示无限循环
_player?.prepareToPlay() // 播放
_player?.play()
_player?.pause() //暂停
_player?.stop() //停止
func audioPlayerBeginInterruption(player: AVAudioPlayer!) {
_player?.pause()
}
func audioPlayerEndInterruption(player: AVAudioPlayer!) {
_player?.prepareToPlay()
_player?.play()
}
//AVAudioPlayer(data: <#NSData!#>, error: <#NSErrorPointer#>) //一般来说只能播放本地音乐,播放网络音乐可以转换成data进行播放