說到播放器,大家都是很熟悉的,但是在IOS中設計和實現音頻播放器不是那么簡單,在進行音頻回放時,建議不要使用第三方庫,這無疑會給項目帶來高風險和維護成本,本文是武林技術頻道小編為大家簡單介紹iOS開發中實現音頻播放功能,一起來學習吧!
音頻播放
1、介紹
?- 功能介紹
?用于播放比較長的音頻、說明、音樂 ,使用到的是AVFoundation
?- 框架介紹
?* AVAudioPlayer
?* 初始化:
?注意 :
(3)必須聲明全局變量的音樂播放對象、或者是屬性的音樂播放對象? 才可以播放
(4)在退出播放頁面的時候 一定要把播放對象置空? 同時把delegate置空
導入框架:#import 
聲明全局變量
@interface ViewController (){ AVAudioPlayer *audioPlayer;}@end 
音頻的基本屬性
預播放 [audioPlayer prepareToPlay];
獲取? 當前音樂的聲道 audioPlayer.numberOfChannels
audioPlayer.currentTime //當前播放的時間?
audioPlayer.playing//判斷是否正在播放?
audioPlayer.numberOfLoops ;//設置循環播放的此次?????
audioPlayer.duration? 獲得播放音頻的時間
audioPlayer.pan? 設置左右聲道效果
audioPlayer.volume 設置音量 0.0- 1.0? 是一個百分比???
//設置速率 必須設置enableRate 為YES;???? audioPlayer.enableRate =YES;???? audioPlayer.rate =3.0;
音量 audioPlayer.volume = 0.1;
設置播放次數 負數是無限循環的 0 是一次 1是兩次 依次類推 audioPlayer.numberOfLoops = 0;
獲得當前峰值 audioPlayer peakPowerForChannel:2
平均峰值
audioPlayer averagePowerForChannel:2
音頻播放的幾個代理方法
//播放完成的時候調用
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
??? NSLog(@"播放完成");
}
//解碼出現錯誤的時候調用
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error{
}
//開始被打擾中斷的時候調用
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
???
}
//中斷結束后調用
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{
???
}
? 實例解析音頻播放
初始化一個按鈕
在ViewDidLoad中
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 100, 100); button.backgroundColor = [UIColor brownColor]; [button setTitle:@"Play" forState:UIControlStateNormal]; [button setTitle:@"Pause" forState:UIControlStateSelected]; [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; [self playMusicWithName:@"TFBOYS-青春修煉手冊.mp3"];
按鈕的觸發方法:
- (void)play:(UIButton *)sender{  sender.selected = !sender.selected;  sender.selected != YES ? [audioPlayer pause]:[audioPlayer play];}- (void)playMusicWithName:(NSString *)name{  NSError *error;  //  創建一個音樂播放對象  audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSBundle mainBundle]URLForResource:name withExtension:nil] error:&error];  if (error) {    NSLog(@"%@",error);  }   預播放  [audioPlayer prepareToPlay];    播放 在這里不寫在這 但它是必須的步驟  [audioPlayer play];    獲取 當前音樂的聲道  NSLog(@"%ld",audioPlayer.numberOfChannels);    durtion:獲得播放音頻的時間    設置聲道 -1.0左 0.0中間 1.0右  audioPlayer.pan = 0.0;    音量  audioPlayer.volume = 0.1;    設置速率 必須設置enableRate為YES  audioPlayer.enableRate = YES;  設置速率 0.5是一半的速度 1.0普通 2.0雙倍速率  audioPlayer.rate = 1.0;    currentTime 獲得時間  獲得峰值 必須設置meteringEnabled為YES  audioPlayer.meteringEnabled = YES;  更新峰值  [audioPlayer updateMeters];    獲得當前峰值  NSLog(@"當前峰值:%f",[audioPlayer peakPowerForChannel:2]);  NSLog(@"平均峰值%f",[audioPlayer averagePowerForChannel:2]);    設置播放次數 負數是無限循環的 0 是一次 1是兩次 依次類推  audioPlayer.numberOfLoops = 0;  掛上代理  audioPlayer.delegate = self;  }上面是武林技術頻道小編為大家簡單介紹iOS開發中實現音頻播放功能,相信大家都有了一定的了解,如需了解更多的相關資訊,請繼續關注武林技術頻道吧!
新聞熱點
疑難解答