了解iOS開(kāi)發(fā)之多媒體播放
iOS開(kāi)發(fā)之多媒體播放是本文要介紹的內(nèi)容,iOS SDK中提供了很多方便的方法來(lái)播放多媒體。本文將利用這些SDK做一個(gè)demo,來(lái)講述一下如何使用它們來(lái)播放音頻文件。
AudioToolbox framework
使用AudioToolbox framework。這個(gè)框架可以將比較短的聲音注冊(cè)到 system sound服務(wù)上。被注冊(cè)到system sound服務(wù)上的聲音稱之為 system sounds。它必須滿足下面幾個(gè)條件。
1、 播放的時(shí)間不能超過(guò)30秒
2、數(shù)據(jù)必須是 PCM或者IMA4流格式
3、必須被打包成下面三個(gè)格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff)
聲音文件必須放到設(shè)備的本地文件夾下面。通過(guò)AudioServicesCreateSystemSoundID方法注冊(cè)這個(gè)聲音文件,AudioServicesCreateSystemSoundID需要聲音文件的url的CFURLRef對(duì)象??聪旅孀?cè)代碼:
- #import <AudioToolbox/AudioToolbox.h>
 - @interface MediaPlayerViewController : UIViewController{
 - IBOutlet UIButton *audioButton;
 - SystemSoundID shortSound;}- (id)init{
 - self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];
 - if (self) {
 - // Get the full path of Sound12.aif
 - NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Sound12"
 - ofType:@"aif"];
 - // If this file is actually in the bundle...
 - if (soundPath) {
 - // Create a file URL with this path
 - NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
 - // Register sound file located at that URL as a system sound
 - OSStatus err = AudioServicesCreateSystemSoundID((CFURLRef)soundURL,
 - &shortSound);
 - if (err != kAudioServicesNoError)
 - NSLog(@"Could not load %@, error code: %d", soundURL, err);
 - }
 - }
 - return self;
 - }
 
這樣就可以使用下面代碼播放聲音了:
- - (IBAction)playShortSound:(id)sender{ AudioServicesPlaySystemSound(shortSound);}
 
使用下面代碼,還加一個(gè)震動(dòng)的效果:
- - (IBAction)playShortSound:(id)sender{
 - AudioServicesPlaySystemSound(shortSound);
 - AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);}
 - AVFoundation framework
 
對(duì)于壓縮過(guò)Audio文件,或者超過(guò)30秒的音頻文件,可以使用AVAudioPlayer類。這個(gè)類定義在AVFoundation framework中。
下面我們使用這個(gè)類播放一個(gè)mp3的音頻文件。首先要引入AVFoundation framework,然后MediaPlayerViewController.h中添加下面代碼:
- #import <AVFoundation/AVFoundation.h>
 - @interface MediaPlayerViewController : UIViewController <AVAudioPlayerDelegate>{
 - IBOutlet UIButton *audioButton;
 - SystemSoundID shortSound;
 - AVAudioPlayer *audioPlayer;
 
AVAudioPlayer類也是需要知道音頻文件的路徑,使用下面代碼創(chuàng)建一個(gè)AVAudioPlayer實(shí)例:
- - (id)init{
 - self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];
 - if (self) {
 - 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];
 - }
 - NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Sound12"
 - ofType:@"aif"];
 
我們可以在一個(gè)button的點(diǎn)擊事件中開(kāi)始播放這個(gè)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];
 - }
 - }
 
這樣運(yùn)行我們的程序,就可以播放音樂(lè)了。
這個(gè)類對(duì)應(yīng)的AVAudioPlayerDelegate有兩個(gè)委托方法。一個(gè)是 audioPlayerDidFinishPlaying:successfully: 當(dāng)音頻播放完成之后觸發(fā)。當(dāng)播放完成之后,可以將播放按鈕的文本重新回設(shè)置成:Play Audio File
- - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
 - successfully:(BOOL)flag
 - {
 - [audioButton setTitle:@"Play Audio File"
 - forState:UIControlStateNormal];
 - }
 
另一個(gè)是audioPlayerEndInterruption:,當(dāng)程序被應(yīng)用外部打斷之后,重新回到應(yīng)用程序的時(shí)候觸發(fā)。在這里當(dāng)回到此應(yīng)用程序的時(shí)候,繼續(xù)播放音樂(lè)。
- - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player{ [audioPlayer play];}
 - MediaPlayer framework
 
播放電影文件:
iOS sdk中可以使用MPMoviePlayerController來(lái)播放電影文件。但是在iOS設(shè)備上播放電影文件有嚴(yán)格的格式要求,只能播放下面兩個(gè)格式的電影文件。
- • H.264 (Baseline Profile Level 3.0)
 - • MPEG-4 Part 2 video (Simple Profile)
 
幸運(yùn)的是你可以先使用iTunes將文件轉(zhuǎn)換成上面兩個(gè)格式。
MPMoviePlayerController還可以播放互聯(lián)網(wǎng)上的視頻文件。但是建議你先將視頻文件下載到本地,然后播放。如果你不這樣做,iOS可能會(huì)拒絕播放很大的視頻文件。
這個(gè)類定義在MediaPlayer framework中。在你的應(yīng)用程序中,先添加這個(gè)引用,然后修改MediaPlayerViewController.h文件。
- #import <MediaPlayer/MediaPlayer.h>
 - @interface MediaPlayerViewController : UIViewController <AVAudioPlayerDelegate>
 - {
 - MPMoviePlayerController *moviePlayer;
 
下面我們使用這個(gè)類來(lái)播放一個(gè).m4v 格式的視頻文件。與前面的類似,需要一個(gè)url路徑。
- - (id)init{
 - self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];
 - if (self) { NSString *moviePath = [[NSBundle mainBundle]
 - pathForResource:@"Layers"
 - ofType:@"m4v"
 - ];
 - if (moviePath) {
 - NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
 - moviePlayer = [[MPMoviePlayerController alloc]
 - initWithContentURL:movieURL];
 - }
 
MPMoviePlayerController有一個(gè)視圖來(lái)展示播放器控件,我們?cè)趘iewDidLoad方法中,將這個(gè)播放器展示出來(lái)。
- - (void)viewDidLoad{
 - [[self view] addSubview:[moviePlayer view]];
 - float halfHeight = [[self view] bounds].size.height / 2.0;
 - float width = [[self view] bounds].size.width;
 - [[moviePlayer view] setFrame:CGRectMake(0, halfHeight, width, halfHeight)];
 - }
 
還有一個(gè)MPMoviePlayerViewController類,用于全屏播放視頻文件,用法和MPMoviePlayerController一樣。
- MPMoviePlayerViewController *playerViewController =
 - [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
 - [viewController presentMoviePlayerViewControllerAnimated:playerViewController];
 
我們?cè)诼?tīng)音樂(lè)的時(shí)候,可以用iphone做其他的事情,這個(gè)時(shí)候需要播放器在后臺(tái)也能運(yùn)行,我們只需要在應(yīng)用程序中做個(gè)簡(jiǎn)單的設(shè)置就行了。
1、在Info property list中加一個(gè) Required background modes節(jié)點(diǎn),它是一個(gè)數(shù)組,將第一項(xiàng)設(shè)置成設(shè)置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];
 
在后臺(tái)運(yùn)行的播放音樂(lè)的功能在模擬器中看不出來(lái),只有在真機(jī)上看效果。
小結(jié):了解iOS開(kāi)發(fā)之多媒體播放的內(nèi)容介紹完了,本文通過(guò)例子詳細(xì)講解了iOS SDK中用于播放音頻文件的類,最后希望本文對(duì)你有所幫助!本文提供代碼供友們方便的去學(xué)習(xí),代碼下載地址:http://files.cnblogs.com/zhuqil/MediaPlayer.zip
















 
 
 
 
 
 
 