播放音频的几种方式

50 篇文章 0 订阅

近日做音频播放方面的内容,整理关于音频的东西,发现IOS中有四种种播放音频的方式:

  • System Sound Services
  • AVAudioPlayer 类
  • Audio Queue Services
  • OpenAL
。现整理其中的两种方式如下

1.运用AVAudioPlayer

a.首先导入AVFoundation.framework。

b.创建一个播放器

url 创建



AVAudioPlayer* player = [[AVAudioPlayer alloc]  

  1.                         initWithContentsOfURL:[NSURL fileURLWithPath:  
  2.                                               [[NSBundle mainBundle]pathForResource:  
  3.                                            @"music" ofType:@"m4a"   
  4.                                            inDirectory:@"/"]]  
  5.                         error:&err ];//使用本地URL创建

data 创建

  1. AVAudioPlayer* player = [[AVAudioPlayer alloc]  
  2.                             initWithData:myData   
  3.                             error:&err ];//使用NSData创建 
c.播放器属性

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 updateMeters];//更新仪表读数 

//读取每个声道的平均电平和峰值电平,代表每个声道的分贝数,范围在-100~0之间。 

for(int i = 0; i<player.numberOfChannels;i++){ 

float power = [player averagePowerForChannel:i]; 

float peak = [player peakPowerForChannel:i];  } 

c.播放声音

  1. [ player prepareToPlay];//分配播放所需的资源,并将其加入内部播放队列  
  2. [player play];//播放  
  3. [player stop];//停止
d.代理方法

player.delegate = self; 

  1. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag{  
  2.     //播放结束时执行的动作  
  3. }  
  4. - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer*)player error:(NSError *)error{  
  5.     //解码错误执行的动作  
  6. }  
  7. - (void)audioPlayerBeginInteruption:(AVAudioPlayer*)player{  
  8.     //处理中断的代码  
  9. }  
  10. - (void)audioPlayerEndInteruption:(AVAudioPlayer*)player{  
  11.     //处理中断结束的代码  

2.播放System Sound Services系统的声音

传入 AudioToolbox.framework

a. 播放声音

  1. - (IBAction)playSystemSound:(id)sender  
  2. {  
  3.     NSURL* system_sound_url = [NSURL fileURLWithPath:[[NSBundle mainBundle]  
  4.                                                       pathForResource:@"BeepGMC500" ofType:@"wav"]];  
  5.     SystemSoundID system_sound_id;  
  6.     AudioServicesCreateSystemSoundID(  
  7.                                      (__bridge CFURLRef)system_sound_url,  
  8.                                      &system_sound_id  
  9.                                      );  
  10.     // Register the sound completion callback.  
  11.     AudioServicesAddSystemSoundCompletion(  
  12.                                           system_sound_id,  
  13.                                           NULL, // uses the main run loop  
  14.                                           NULL, // uses kCFRunLoopDefaultMode  
  15.                                           MySoundFinishedPlayingCallback, // the name of our custom callback function  
  16.                                           NULL // for user data, but we don't need to do that in this case, so we just pass NULL  
  17.                                           );  
  18.     // Play the System Sound  
  19.     AudioServicesPlaySystemSound(system_sound_id);  

b.震动

  1. - (IBAction)vibrate:(id)sender  
  2. {  
  3.     AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  

c.回调函数实现

  1. void MySoundFinishedPlayingCallback(SystemSoundID sound_id, void* user_data)  
  2. {  
  3.     AudioServicesDisposeSystemSoundID(sound_id);  
  4. }
转自: http://blog.csdn.net/iukey/article/details/7296107





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值