AVAudioPlayer播放音频
创建播放器:
static AVAudioPlayer* player; + (void)playAudio:(NSString *)audioFile{ //如果播放器也存在则停止播放并释放资源。 if(player){ if(player.isPlaying) [player stop]; [player release]; } NSURL* url = [[NSURL alloc] initFileURLWithPath:@"a.mp3"]; player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; player.numberOfLoops = 1; //循环数 player.volume = 1.0f; //音量0.0~1.0之间 player.currentTime = 15.0;//可以指定从任意位置开始播放 [player prepareToPlay]; //分配播放所需的资源,并将其加入内部播放队列 [player play]; [url release]; }
事件:
player.delegate = self; - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag{ //播放结束时执行的动作 } - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer*)player error:(NSError *)error{ //解码错误执行的动作 } - (void)audioPlayerBeginInteruption:(AVAudioPlayer*)player{ //处理中断的代码 } - (void)audioPlayerEndInteruption:(AVAudioPlayer*)player{ //处理中断结束的代码 }
懒得敲字,资料来源:http://www.cnblogs.com/mrhgw/archive/2012/07/03/2574613.html