自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

了解iOS開發(fā)之多媒體播放

移動開發(fā) iOS
本文將利用這些SDK做一個demo,來講述一下如何使用它們來播放音頻文件,使用AudioToolbox framework。這個框架可以將比較短的聲音注冊到 system sound服務(wù)上,來看詳細內(nèi)容。

iOS開發(fā)多媒體播放是本文要介紹的內(nèi)容,iOS SDK中提供了很多方便的方法來播放多媒體。本文將利用這些SDK做一個demo,來講述一下如何使用它們來播放音頻文件。

AudioToolbox framework

使用AudioToolbox framework。這個框架可以將比較短的聲音注冊到 system sound服務(wù)上。被注冊到system sound服務(wù)上的聲音稱之為 system sounds。它必須滿足下面幾個條件。

1、 播放的時間不能超過30秒

2、數(shù)據(jù)必須是 PCM或者IMA4流格式

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

聲音文件必須放到設(shè)備的本地文件夾下面。通過AudioServicesCreateSystemSoundID方法注冊這個聲音文件,AudioServicesCreateSystemSoundID需要聲音文件的url的CFURLRef對象。看下面注冊代碼:

  1. #import <AudioToolbox/AudioToolbox.h> 
  2. @interface MediaPlayerViewController : UIViewController{      
  3. IBOutlet UIButton *audioButton;      
  4. SystemSoundID shortSound;}- (id)init{      
  5. self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];      
  6. if (self) {          
  7. // Get the full path of Sound12.aif          
  8. NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Sound12"    
  9.                                               ofType:@"aif"];          
  10. // If this file is actually in the bundle...         
  11.  if (soundPath) {              
  12.  // Create a file URL with this path             
  13.   NSURL *soundURL = [NSURL fileURLWithPath:soundPath];   
  14. // Register sound file located at that URL as a system sound             
  15.  OSStatus err = AudioServicesCreateSystemSoundID((CFURLRef)soundURL,    
  16.                                       &shortSound);              
  17.        if (err != kAudioServicesNoError)                 
  18.         NSLog(@"Could not load %@, error code: %d", soundURL, err);  
  19.     }      
  20.   }     
  21. return self;  

這樣就可以使用下面代碼播放聲音了:

  1. - (IBAction)playShortSound:(id)sender{    AudioServicesPlaySystemSound(shortSound);} 

使用下面代碼,還加一個震動的效果:

  1. - (IBAction)playShortSound:(id)sender{      
  2. AudioServicesPlaySystemSound(shortSound);      
  3. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);}  
  4. AVFoundation framework 

對于壓縮過Audio文件,或者超過30秒的音頻文件,可以使用AVAudioPlayer類。這個類定義在AVFoundation framework中。

下面我們使用這個類播放一個mp3的音頻文件。首先要引入AVFoundation framework,然后MediaPlayerViewController.h中添加下面代碼:

  1. #import <AVFoundation/AVFoundation.h> 
  2. @interface MediaPlayerViewController : UIViewController <AVAudioPlayerDelegate>{  
  3.     IBOutlet UIButton *audioButton;      
  4.     SystemSoundID shortSound;      
  5.     AVAudioPlayer *audioPlayer; 

AVAudioPlayer類也是需要知道音頻文件的路徑,使用下面代碼創(chuàng)建一個AVAudioPlayer實例:

  1. - (id)init{  
  2.     self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];  
  3.       if (self) {  
  4.                   NSString *musicPath = [[NSBundle mainBundle]  pathForResource:@"Music"     
  5.         ofType:@"mp3"];          
  6.       if (musicPath) {   
  7.                  NSURL *musicURL = [NSURL fileURLWithPath:musicPath];  
  8.                audioPlayer = [[AVAudioPlayer alloc]  initWithContentsOfURL:musicURL     
  9.                       error:nil];   
  10.                [audioPlayer setDelegate:self];        
  11.     }          
  12.     NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Sound12"      
  13.  ofType:@"aif"]; 

我們可以在一個button的點擊事件中開始播放這個mp3文件,如:

  1. - (IBAction)playAudioFile:(id)sender{  
  2.     if ([audioPlayer isPlaying]) {  
  3.             // Stop playing audio and change text of button  
  4.               [audioPlayer stop];          
  5.               [sender setTitle:@"Play Audio File"   
  6.               forState:UIControlStateNormal];  
  7.     }    else {         
  8.     // Start playing audio and change text of button so    
  9.     // user can tap to stop playback    
  10.     [audioPlayer play];          
  11.     [sender setTitle:@"Stop Audio File"     
  12.     forState:UIControlStateNormal];  
  13.    }  

這樣運行我們的程序,就可以播放音樂了。

這個類對應(yīng)的AVAudioPlayerDelegate有兩個委托方法。一個是 audioPlayerDidFinishPlaying:successfully: 當(dāng)音頻播放完成之后觸發(fā)。當(dāng)播放完成之后,可以將播放按鈕的文本重新回設(shè)置成:Play Audio File

  1. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player   
  2.                       successfully:(BOOL)flag  
  3.               {      
  4.            [audioButton setTitle:@"Play Audio File"  
  5.                             forState:UIControlStateNormal];  
  6.             } 

另一個是audioPlayerEndInterruption:,當(dāng)程序被應(yīng)用外部打斷之后,重新回到應(yīng)用程序的時候觸發(fā)。在這里當(dāng)回到此應(yīng)用程序的時候,繼續(xù)播放音樂。

  1. - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player{    [audioPlayer play];}  
  2. MediaPlayer framework 

播放電影文件:

iOS sdk中可以使用MPMoviePlayerController來播放電影文件。但是在iOS設(shè)備上播放電影文件有嚴格的格式要求,只能播放下面兩個格式的電影文件。

  1. • H.264 (Baseline Profile Level 3.0)  
  2. • MPEG-4 Part 2 video (Simple Profile) 

幸運的是你可以先使用iTunes將文件轉(zhuǎn)換成上面兩個格式。

MPMoviePlayerController還可以播放互聯(lián)網(wǎng)上的視頻文件。但是建議你先將視頻文件下載到本地,然后播放。如果你不這樣做,iOS可能會拒絕播放很大的視頻文件。

這個類定義在MediaPlayer framework中。在你的應(yīng)用程序中,先添加這個引用,然后修改MediaPlayerViewController.h文件。

  1. #import <MediaPlayer/MediaPlayer.h> 
  2. @interface MediaPlayerViewController : UIViewController <AVAudioPlayerDelegate> 
  3. {    
  4.   MPMoviePlayerController *moviePlayer; 

下面我們使用這個類來播放一個.m4v 格式的視頻文件。與前面的類似,需要一個url路徑。

  1. - (id)init{     
  2.  self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];  
  3.     if (self) {            NSString *moviePath = [[NSBundle mainBundle]   
  4.     pathForResource:@"Layers"       
  5.     ofType:@"m4v"  
  6.   ];         
  7.    if (moviePath) {            
  8.      NSURL *movieURL = [NSURL fileURLWithPath:moviePath];              
  9.      moviePlayer = [[MPMoviePlayerController alloc]                                       
  10.      initWithContentURL:movieURL];          
  11.  } 

MPMoviePlayerController有一個視圖來展示播放器控件,我們在viewDidLoad方法中,將這個播放器展示出來。

  1. - (void)viewDidLoad{      
  2. [[self view] addSubview:[moviePlayer view]];  
  3.     float halfHeight = [[self view] bounds].size.height / 2.0;  
  4.     float width = [[self view] bounds].size.width;      
  5.     [[moviePlayer view] setFrame:CGRectMake(0, halfHeight, width, halfHeight)];  
  6.  } 

還有一個MPMoviePlayerViewController類,用于全屏播放視頻文件,用法和MPMoviePlayerController一樣。

  1. MPMoviePlayerViewController *playerViewController =  
  2.     [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];    
  3. [viewController presentMoviePlayerViewControllerAnimated:playerViewController]; 

我們在聽音樂的時候,可以用iphone做其他的事情,這個時候需要播放器在后臺也能運行,我們只需要在應(yīng)用程序中做個簡單的設(shè)置就行了。

1、在Info property list中加一個 Required background modes節(jié)點,它是一個數(shù)組,將第一項設(shè)置成設(shè)置App plays audio。

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

  1. if (musicPath) {          
  2. NSURL *musicURL = [NSURL fileURLWithPath:musicPath];          
  3. [[AVAudioSession sharedInstance]                     
  4.            setCategory:AVAudioSessionCategoryPlayback error:nil];          
  5. audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL     
  6.                                error:nil];          
  7.        [audioPlayer setDelegate:self];  

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

了解iOS開發(fā)之多媒體播放

小結(jié):了解iOS開發(fā)多媒體播放的內(nèi)容介紹完了,本文通過例子詳細講解了iOS SDK中用于播放音頻文件的類,最后希望本文對你有所幫助!本文提供代碼供友們方便的去學(xué)習(xí),代碼下載地址:http://files.cnblogs.com/zhuqil/MediaPlayer.zip

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2013-12-17 13:29:04

iOS開發(fā)多媒體

2013-12-17 11:18:53

iOS開發(fā)多媒體API

2021-09-13 15:15:18

鴻蒙HarmonyOS應(yīng)用

2010-01-27 16:21:29

Android多媒體播

2009-02-18 17:15:51

Fedora 10多媒體播放解決方案

2017-03-01 14:01:31

android多媒體音樂代碼

2011-06-24 10:21:11

Qt phonon 多媒體

2013-08-28 16:08:19

多媒體Windows8.1

2011-08-18 17:07:23

IOS開發(fā)多線程NSInvocatio

2010-08-01 15:34:27

Android

2011-06-09 10:07:28

Qt phonon

2010-06-30 10:38:05

2009-12-25 17:02:33

WPF多媒體

2010-10-27 11:27:50

MAS視頻監(jiān)控H3C

2018-05-25 14:37:58

2014-09-24 11:04:31

微信企業(yè)號開發(fā)

2020-12-20 09:05:30

騰訊多媒體5G

2018-02-07 15:35:04

2009-12-22 16:29:51

Linux多媒體軟件

2015-09-11 11:02:23

ios靜態(tài)庫開發(fā)
點贊
收藏

51CTO技術(shù)棧公眾號