要使用AVAudioPlayer必须先要导入:AVFoundation.framework
初始化:
_Player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:backgroundMusicPath] error:&error];
属性:
1.音量
player.volume=0.8;//0.0~1.0之间
2.循环次数
player.numberOfLoops = 3;//默认只播放一次
3.播放位置
player.currentTime = 15.0;//可以指定从任意位置开始播放
4.声道数
NSUInteger channels = player.numberOfChannels;//只读属性
5.持续时间
NSTimeInterval duration = player.dueration;//获取采样的持续时间
6.仪表计数
player.meteringEnabled = YES;//开启仪表计数功能 [ player Meters];//更新仪表读数 //读取每个声道的平均电平和峰值电平,代表每个声道的分贝数,范围在-100~0之间。 for(int i = 0; i<player.numberOfChannels;i++){ float power = [player averagePowerForChannel:i]; float peak = [player peakPowerForChannel:i]; }
7.速度rate
player.rate = 1.0f;
回调:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag{ //播放结束时执行的动作 } - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer*)player error:(NSError *)error{ //解码错误执行的动作 } - (void)audioPlayerBeginInteruption:(AVAudioPlayer*)player{ //处理中断的代码 } - (void)audioPlayerEndInteruption:(AVAudioPlayer*)player{ //处理中断结束的代码 }