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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

IOS播放音頻AVAudioPlayer(實(shí)例)

2019-11-14 19:58:33
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1. AVFoundation

 

Build Phases => Link Binary With Libraies => + => AVFoundation.framework => add

 

firstviewcontroller.h

C代碼 復(fù)制代碼 收藏代碼
  1. #import <UIKit/UIKit.h>  
  2. #import <AVFoundation/AVFoundation.h>  
  3.   
  4. @interface FirstViewController : UIViewController  
  5. {    
  6.     __weak IBOutlet UILabel *label;  
  7.     AVAudioPlayer *player;  
  8. }  
  9.   
  10. - (IBAction)toplay:(id)sender;  
  11.   
  12. @end  
#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>@interface FirstViewController : UIViewController{      __weak IBOutlet UILabel *label;    AVAudioPlayer *player;}- (IBAction)toplay:(id)sender;@end

 

firstviewcontroller.m

C代碼 復(fù)制代碼 收藏代碼
  1. - (IBAction)toplay:(id)sender   
  2. {  
  3.     NSURL  *url = [NSURL fileURLWithPath:[NSString  stringWithFormat:@"%@/test.mp3",  [[NSBundle mainBundle]  resourcePath]]];  
  4.       
  5.     NSError  *error;  
  6.     player  = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];  
  7.          
  8.     player.numberOfLoops = -1;  
  9.     [player play];  
  10.       
  11.     [label setText:@"Play ..."];  
  12. }  
- (IBAction)toplay:(id)sender {    NSURL  *url = [NSURL fileURLWithPath:[NSString  stringWithFormat:@"%@/test.mp3",  [[NSBundle mainBundle]  resourcePath]]];        NSError  *error;    player  = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];           player.numberOfLoops = -1;    [player play];        [label setText:@"Play ..."];}

 

或者:

 

C代碼 復(fù)制代碼 收藏代碼
  1. NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];   
  2. NSError  *error;   
  3. player = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:path]error:&error];   
  4. [player PRepareToPlay];  
  5. player.numberOfLoops = -1;  
  6. [player play];  
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]; NSError  *error; player = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:path]error:&error]; [player prepareToPlay];player.numberOfLoops = -1;[player play];

 

test.mp3 拖放到 Supporting Files 文件夾。

 

播放,暫停和停止

C代碼 復(fù)制代碼 收藏代碼
  1. [player play];  //播放  
  2. [player pause]; //暫停  
  3. [player stop];  //停止  
[player play];  //播放[player pause]; //暫停[player stop];  //停止

 

更多功能:

 

1. 音量:

java代碼 復(fù)制代碼 收藏代碼
  1. player.volume=0.8;//0.0~1.0之間    
player.volume=0.8;//0.0~1.0之間  

 

2. 循環(huán)次數(shù)

C代碼 復(fù)制代碼 收藏代碼
  1. player.numberOfLoops = 3;//默認(rèn)只播放一次 負(fù)數(shù)(-1)為無(wú)限循環(huán)  
player.numberOfLoops = 3;//默認(rèn)只播放一次 負(fù)數(shù)(-1)為無(wú)限循環(huán)

 

3.播放位置

C代碼 復(fù)制代碼 收藏代碼
  1. player.currentTime = 15.0;//可以指定從任意位置開(kāi)始播放   
player.currentTime = 15.0;//可以指定從任意位置開(kāi)始播放 

 

3.1 顯示當(dāng)前時(shí)間

C代碼 復(fù)制代碼 收藏代碼
  1. NSLog(@"%f seconds played so  far", player.currentTime);  
NSLog(@"%f seconds played so  far", player.currentTime);

 

4.聲道數(shù)

C代碼 復(fù)制代碼 收藏代碼
  1. NSUInteger channels = player.numberOfChannels;//只讀屬性   
NSUInteger channels = player.numberOfChannels;//只讀屬性 

 

5.持續(xù)時(shí)間

C代碼 復(fù)制代碼 收藏代碼
  1. NSTimeInterval duration = player.dueration;//獲取采樣的持續(xù)時(shí)間    
NSTimeInterval duration = player.dueration;//獲取采樣的持續(xù)時(shí)間  

 

6.儀表計(jì)數(shù)

C代碼 復(fù)制代碼 收藏代碼
  1. player.meteringEnabled = YES;//開(kāi)啟儀表計(jì)數(shù)功能  
  2. [ player updateMeters];//更新儀表讀數(shù)  
  3. //讀取每個(gè)聲道的平均電平和峰值電平,代表每個(gè)聲道的分貝數(shù),范圍在-100~0之間。  
  4. for(int i = 0; i<player.numberOfChannels;i++){  
  5. float power = [player averagePowerForChannel:i];  
  6. float peak = [player peakPowerForChannel:i];  
  7. }  
player.meteringEnabled = YES;//開(kāi)啟儀表計(jì)數(shù)功能[ player updateMeters];//更新儀表讀數(shù)//讀取每個(gè)聲道的平均電平和峰值電平,代表每個(gè)聲道的分貝數(shù),范圍在-100~0之間。for(int i = 0; i<player.numberOfChannels;i++){float power = [player averagePowerForChannel:i];float peak = [player peakPowerForChannel:i];}

 

7. 初始化播放器  

C代碼 復(fù)制代碼 收藏代碼
  1. [player prepareToPlay];  
[player prepareToPlay];

 

8. 判斷是否正在播放

C代碼 復(fù)制代碼 收藏代碼
  1. [player isPlaying]  
[player isPlaying]

 

9、代理方法

 

          加入播放出現(xiàn)異常,或者被更高級(jí)別的系統(tǒng)任務(wù)打斷,我們的程序還沒(méi)來(lái)得及收?qǐng)鼍蛼炝耍趺崔k?不急,我們可以通過(guò)幾個(gè)委托方法很好地處理所有的情形。

首先給player設(shè)置委托是必須的:

 

C代碼 復(fù)制代碼 收藏代碼
  1. player.delegate = self;    
player.delegate = self;  
C代碼 復(fù)制代碼 收藏代碼
  1. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag{  
  2.     //播放結(jié)束時(shí)執(zhí)行的動(dòng)作  
  3. }  
  4. - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer*)player error:(NSError *)error{  
  5.     //解碼錯(cuò)誤執(zhí)行的動(dòng)作  
  6. }  
  7. - (void)audioPlayerBeginInteruption:(AVAudioPlayer*)player{  
  8.     //處理中斷的代碼  
  9. }  
  10. - (void)audioPlayerEndInteruption:(AVAudioPlayer*)player{  
  11.     //處理中斷結(jié)束的代碼  
  12. }  
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag{    //播放結(jié)束時(shí)執(zhí)行的動(dòng)作}- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer*)player error:(NSError *)error{    //解碼錯(cuò)誤執(zhí)行的動(dòng)作}- (void)audioPlayerBeginInteruption:(AVAudioPlayer*)player{    //處理中斷的代碼}- (void)audioPlayerEndInteruption:(AVAudioPlayer*)player{    //處理中斷結(jié)束的代碼}

  

 

 

參考:

http://blog.csdn.net/xys289187120/article/details/6595919

http://blog.csdn.net/iukey/article/details/7295962

 

視頻:

http://www.youtube.com/watch?v=kCpw6ip90cY

 

 

2. AudioToolbox

 

Build Phases => Link Binary With Libraies => + => AudioToolbox.framework => add

 

firstviewcontroller.h

C代碼 復(fù)制代碼 收藏代碼
  1. #import <UIKit/UIKit.h>  
  2. #import <AudioToolbox/AudioToolbox.h>  
  3.   
  4. @interface FirstViewController : UIViewController  
  5. {    
  6. }  
  7.   
  8. - (IBAction)toplay:(id)sender;  
  9.   
  10. @end  
#import <UIKit/UIKit.h>#import <AudioToolbox/AudioToolbox.h>@interface FirstViewController : UIViewController{  }- (IBAction)toplay:(id)sender;@end

 

firstviewcontroller.m

C代碼 復(fù)制代碼 收藏代碼
  1. - (IBAction)toplay:(id)sender  
  2. {  
  3.     CFBundleRef mainBundle = CFBundleGetMainBundle();  
  4.     CFURLRef soundFileURLRef;  
  5.     soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound1", CFSTR ("wav"), NULL);  
  6.       
  7.     UInt32 soundID;  
  8.     AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);  
  9.     AudioServicesPlaySystemSound(soundID);  
  10. }  
- (IBAction)toplay:(id)sender{    CFBundleRef mainBundle = CFBundleGetMainBundle();    CFURLRef soundFileURLRef;    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound1", CFSTR ("wav"), NULL);        UInt32 soundID;    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);    AudioServicesPlaySystemSound(soundID);}

 

視頻:

http://www.youtube.com/watch?v=lSJhYx28Krg&feature=youtu.be

 

 

 

 

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 台北市| 广昌县| 塔城市| 方山县| 海伦市| 沙湾县| 淳安县| 古田县| 米泉市| 江山市| 古丈县| 澎湖县| 县级市| 娄烦县| 大同市| 洛川县| 宜良县| 江安县| 五华县| 宁津县| 阜宁县| 六枝特区| 利津县| 怀远县| 哈巴河县| 安陆市| 霍林郭勒市| 横峰县| 富蕴县| 蛟河市| 奉新县| 南投市| 祥云县| 彭阳县| 海伦市| 嘉义市| 商城县| 灯塔市| 噶尔县| 峡江县| 门头沟区|