欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開發(fā)實現(xiàn)音頻播放功能

 更新時間:2016年03月16日 10:05:06   投稿:hebedich  
本文給大家分享的是在IOS開發(fā)過程中實現(xiàn)音頻播放的功能,講解的十分細致,有需要的小伙伴可以參考下

音頻播放

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 ;//設置循環(huán)播放的此次     

audioPlayer.duration  獲得播放音頻的時間

audioPlayer.pan  設置左右聲道效果

audioPlayer.volume 設置音量 0.0- 1.0  是一個百分比   

//設置速率 必須設置enableRate 為YES;     audioPlayer.enableRate =YES;     audioPlayer.rate =3.0;

音量 audioPlayer.volume = 0.1;

設置播放次數(shù) 負數(shù)是無限循環(huán)的 0 是一次 1是兩次 依次類推 audioPlayer.numberOfLoops = 0;

獲得當前峰值 audioPlayer peakPowerForChannel:2

平均峰值
audioPlayer averagePowerForChannel:2
音頻播放的幾個代理方法


//播放完成的時候調用
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"播放完成");
}
//解碼出現(xiàn)錯誤的時候調用
- (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"];

按鈕的觸發(fā)方法:

- (void)play:(UIButton *)sender{
  sender.selected = !sender.selected;
  sender.selected != YES ? [audioPlayer pause]:[audioPlayer play];
}

- (void)playMusicWithName:(NSString *)name{
  NSError *error;
  
//  創(chuàng)建一個音樂播放對象
  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]);
  
  設置播放次數(shù) 負數(shù)是無限循環(huán)的 0 是一次 1是兩次 依次類推
  audioPlayer.numberOfLoops = 0;
  掛上代理
  audioPlayer.delegate = self;
  
}


相關文章

  • touchesBegan: withEvent: 不執(zhí)行解決

    touchesBegan: withEvent: 不執(zhí)行解決

    這篇文章主要介紹了touchesBegan: withEvent: 不執(zhí)行解決的相關資料,需要的朋友可以參考下
    2016-12-12
  • iOS CAEmitterLayer實現(xiàn)粒子發(fā)射動畫效果

    iOS CAEmitterLayer實現(xiàn)粒子發(fā)射動畫效果

    這篇文章主要為大家詳細介紹了iOS CAEmitterLayer 實現(xiàn)粒子發(fā)射動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • iOS NSURLSessionDownloadTask實現(xiàn)文件斷點下載的方法

    iOS NSURLSessionDownloadTask實現(xiàn)文件斷點下載的方法

    本篇文章主要介紹了iOS NSURLSessionDownloadTask實現(xiàn)文件斷點下載的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • iOS NSNotificationCenter通知中心使用小結

    iOS NSNotificationCenter通知中心使用小結

    IOS中經(jīng)常會使用到NSNotification和delegate來進行一些類之間的消息傳遞,這篇文章主要介紹了iOS NSNotificationCenter使用小結,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 使用AVFoundation實現(xiàn)視頻錄制詳解

    使用AVFoundation實現(xiàn)視頻錄制詳解

    這篇文章主要介紹了使用AVFoundation實現(xiàn)視頻錄制詳解的相關資料,需要的朋友可以參考下
    2022-09-09
  • 在iOS應用中使用UIWebView創(chuàng)建簡單的網(wǎng)頁瀏覽器界面

    在iOS應用中使用UIWebView創(chuàng)建簡單的網(wǎng)頁瀏覽器界面

    這篇文章主要介紹了在iOS應用中使用UIWebView創(chuàng)建簡單的網(wǎng)頁瀏覽器界面的方法,包括動態(tài)獲取UIWebView高度的實現(xiàn),需要的朋友可以參考下
    2016-01-01
  • iOS和JS交互教程之WKWebView-協(xié)議攔截詳解

    iOS和JS交互教程之WKWebView-協(xié)議攔截詳解

    這篇文章主要給大家介紹了關于iOS和JS交互教程之WKWebView-協(xié)議攔截的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-09-09
  • iOS11實現(xiàn)App內自動連接Wi-Fi的方法

    iOS11實現(xiàn)App內自動連接Wi-Fi的方法

    這篇文章主要給大家介紹了關于iOS11實現(xiàn)App內自動連接Wi-Fi的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-10-10
  • iOS如何裁剪圓形頭像

    iOS如何裁剪圓形頭像

    這篇文章主要介紹了iOS如何裁剪圓形頭像的方法,如何為圓形頭像加邊框,如何進行截圖操作,感興趣的小伙伴們可以參考一下
    2016-04-04
  • IOS網(wǎng)絡請求之AFNetWorking 3.x 使用詳情

    IOS網(wǎng)絡請求之AFNetWorking 3.x 使用詳情

    本篇文章主要介紹了IOS網(wǎng)絡請求之AFNetWorking 3.x 使用詳情,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02

最新評論