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];
}  

在后台運行的播放音樂的功能在模擬器中看不出來,只有在真機上看效果。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM