要使用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{ //處理中斷結束的代碼 }