zoukankan      html  css  js  c++  java
  • 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];
    }  

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

     

  • 相关阅读:
    Apache的Thrift引发的编译思考
    QQ的小秘密
    快速简化Android截屏工作
    Solution of wireless link "PCI unknown" on Centos 7.1
    Java Date Compare
    eclipse集成tomcat日志文件输出配置
    身份证号码验证正则表达式
    curl用法一例 传递代理用户名密码
    HTML 5 placeHolder
    JavaScript(ECMAScript) with 语句
  • 原文地址:https://www.cnblogs.com/martin1009/p/2549473.html
Copyright © 2011-2022 走看看