国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發實現音頻播放功能

2020-07-26 03:26:00
字體:
來源:轉載
供稿:網友

音頻播放

1、介紹

 - 功能介紹

 用于播放比較長的音頻、說明、音樂 ,使用到的是AVFoundation

 - 框架介紹

 * AVAudioPlayer

 * 初始化:

 注意 :

(3)必須聲明全局變量的音樂播放對象、或者是屬性的音樂播放對象  才可以播放

(4)在退出播放頁面的時候 一定要把播放對象置空  同時把delegate置空

導入框架:#import <AVFoundation/AVFoundation.h>

聲明全局變量

@interface ViewController ()<AVAudioPlayerDelegate>{  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;  }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平凉市| 庄河市| 黄龙县| 大丰市| 宁波市| 景谷| 饶河县| 平度市| 蓝山县| 拉萨市| 广元市| 舞阳县| 启东市| 永康市| 平罗县| 罗田县| 宜黄县| 哈巴河县| 林周县| 同仁县| 时尚| 南投县| 林芝县| 工布江达县| 诏安县| 东兴市| 盐源县| 会泽县| 保康县| 长汀县| 都安| 雅安市| 宿迁市| 水城县| 新化县| 沽源县| 西贡区| 桂东县| 呼伦贝尔市| 岫岩| 都兰县|