IOS声音调用

AudioToolbox framework

使用AudioToolbox framework。这个框架可以将比较短的声音注册到 system sound服务上。被注册到system sound服务上的声音称之为 system sounds。它必须满足下面几个条件。

1、 播放的时间不能超过30秒

2、数据必须是 PCM或者IMA4流格式

3、必须被打包成下面三个格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff)

声音文件必须放到设备的本地文件夹下面。通过AudioServicesCreateSystemSoundID方法注册这个声音文件,AudioServicesCreateSystemSoundID需要声音文件的url的CFURLRef对象。看下面注册代码:

#import <AudioToolbox/AudioToolbox.h> 

-(void) playSound

{

   NSString *path = [[NSBundle mainBundle] pathForResource@"beep" ofType:@"wav"];

   SystemSoundID soundID;

   AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&sound);

   AudioServicesPlaySystemSound(soundID);

}
这样就可以使用下面代码播放声音了
使用下面代码,还加一个震动的效果:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

 

AVFoundation framework 
对于压缩过Audio文件,或者超过30秒的音频文件,可以使用AVAudioPlayer类。这个类定义在AVFoundation framework中

 

#import <AVFoundation/AVFoundation.h> 

@interface MediaPlayerViewController : UIViewController <AVAudioPlayerDelegate>{  

    IBOutlet UIButton *audioButton;      

    SystemSoundID shortSound;      
    AVAudioPlayer *audioPlayer; 
}

{

     NSString *musicPath = [[NSBundle mainBundle]  pathForResource:@"Music"   ofType:@"mp3"];          

     if (musicPath) {   

                  NSURL *musicURL = [NSURL fileURLWithPath:musicPath];  

                  audioPlayer = [[AVAudioPlayer alloc]  initWithContentsOfURL:musicURL  error:nil];   

                 [audioPlayer setDelegate:self];        

}

 

我们可以在一个button的点击事件中开始播放这个mp3文件,如:

- (IBAction)playAudioFile:(id)sender{  

   if ([audioPlayer isPlaying]) {  

      // Stop playing audio and change text of button  

      [audioPlayer stop];          
      [sender setTitle:@"Play Audio File"    forState:UIControlStateNormal];  
    }    else {         

      // Start playing audio and change text of button so    
     // user can tap to stop playback    
        [audioPlayer play];          

        [sender setTitle:@"Stop Audio File"     

        forState:UIControlStateNormal];  

      }  

 } 

 

这样运行我们的程序,就可以播放音乐了。

这个类对应的AVAudioPlayerDelegate有两个委托方法。一个是 audioPlayerDidFinishPlaying:successfully: 当音频播放完成之后触发。当播放完成之后,可以将播放按钮的文本重新回设置成:Play Audio File

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player   successfully:(BOOL)flag  {      
          [audioButton setTitle:@"Play Audio File"   forState:UIControlStateNormal];  

 } 

另一个是audioPlayerEndInterruption:,当程序被应用外部打断之后,重新回到应用程序的时候触发。在这里当回到此应用程序的时候,继续播放音乐。

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player{    [audioPlayer play];}  

 

我们在听音乐的时候,可以用iphone做其他的事情,这个时候需要播放器在后台也能运行,我们只需要在应用程序中做个简单的设置就行了。

1、在Info property list中加一个 Required background modes节点,它是一个数组,将第一项设置成设置App plays audio。

2、在播放mp3的代码中加入下面代码:

if (musicPath) {          

  NSURL *musicURL = [NSURL fileURLWithPath:musicPath];          

  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];          

  audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL  error:nil];          

  [audioPlayer setDelegate:self];
}  

在后台运行的播放音乐的功能在模拟器中看不出来,只有在真机上看效果。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值