IOS音频视频
视频播放 MediaPlayer.framework
MPMoviePlayerViewController VS MPMoviePlayerController
|
MPMoviePlayerViewController |
MPMoviePlayerController |
版本支持 |
Available in iOS 3.2 and later. |
Available in iOS 2.0 and later.(多数属性支持3.2后) |
大小 |
只支持全屏播放 如果addsubview 不支持横竖屏 |
可全屏也可自己设置frame |
调用 |
presentMoviePlayerViewControllerAnimated:
dismissMoviePlayerViewControllerAnimated |
addsubview: |
属性 |
moviePlayer [mMPVC. moviePlayer play];
|
BOOL shouldAutoplay NSTimInterval initialPlaybackTime
NSTimeInterval duration MPMovieControlStyle controlStyle |
函数
|
initWithContentURL
shouldAutorotateToInterfaceOrientation
|
initWithContentURL
requestThumbnailImagesAtTimes:timeOption
thumbnailImageAtTime:timeOption
timedMetadata (4.0) |
notification |
MPMoviePlayerPlaybackDidFinishNotification 播放完成
MPMovieMediaTypesAvailableNotification 视频开始播放 (载入完成)
MPMoviePlayerNowPlayingMovieDidChangeNotification 视频开播 (开始载入)
MPMoviePlayerPlaybackStateDidChangeNotification 播放状态变化 判断 mediaPlayer.playbackState
MPMoviePlayerDidEnterFullscreenNotification 全屏 相关 |
另外 UIWebview播放方式 方便 但是对一些视频不支持 经测试有的流媒体的 使用 MPMoviePlayerController 可以播放 但 UIWebview不支持.
因 MPMoviePlayerController 为单例4.0之后 可使用 AVPlayerLayer 的播放方式 addSubLayer实现多个视频同时播放
player1 = [AVPlayer playerWithURL:[NSURL fileURLWithPath:moviePath]];
player1.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[player1 currentItem]];
[player1 play];
playerLayer1 = [AVPlayerLayer playerLayerWithPlayer:player1];
playerLayer1.frame = self.bounds;
[self.layer addSublayer:playerLayer1];
参考:
MPMoviePlayerViewController
MPMoviePlayerController
http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html
音频播放
AVFoundation.framework
|
System Sound Services |
AVAudioPlayer 类 |
MPMusicPlayerController |
特点 |
播放短音效 |
播放任意长度音频 |
播放本地ipod音乐 |
版本 |
ios 2.0 or later |
ios 2.2 or later
|
ios 3.0 later |
属性 |
|
playing,duration,currentTime, |
repeatMode,currentPlaybackTime,numberOfLoops |
常用方法: |
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID) |
- (id)initWithContentsOfURL:(NSURL *)url error:(NSError *)outError;
- (id)initWithData:(NSData *)data error:(NSError *)outError;
- (BOOL)play; - (void)pause; - (void)stop; |
applicationMusicPlayer;
- (void)setQueueWithQuery:(MPMediaQuery *)query;
-(void)play; -(void)pause; -(void)stop; |
一 各个播放器初始化方法:
1 System Sound Services
// 创建路径
NSString*dropMusicPath = [[NSBundle mainBundle] pathForResource:@"bird drop" ofType:@"wav"];
CFURLRefdropURL = (CFURLRef)[NSURL fileURLWithPath:dropMusicPath];
//创建系统声音
AudioServicesCreateSystemSoundID(dropURL, &birdDropID);
//播放音效
AudioServicesPlaySystemSound(birdDropID);
2 AVAudioPlayer 类
// 设置音乐文件路径
path = [[NSBundle mainBundle] pathForResource:@"InTheMood" ofType:@"mp3"];
// 设置 player url为本地音频文件路径
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
在线播放用data初始化
player = [[AVAudioPlayer alloc] initWithData:receiveData error:&err];
[player play];
3 MPMusicPlayerController
player = [MPMusicPlayerController applicationMusicPlayer];
MPMediaItemCollection *_mediaCollection = [[MPMediaItemCollection alloc]initWithItems:SongList];
self.mediaCollection = _mediaCollection;
[_mediaCollection release];
[player setQueueWithItemCollection:mediaCollection];
[player setRepeatMode:MPMusicRepeatModeAll];
[player play];
二 音频后台播放:
(1) 设置 AVAudioSession 属性支持
NSError * err;
AVAudioSession*audioSession;
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
(2) 设置工程文件plist属性
三 系统后台控制音频播放
(1) 重写方法 canBecomeFirstResponder 返回YES
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self canBecomeFirstResponder];
}
(2) 实现接收RemoteControlEvents方法
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
(3) 在回调方法做相应处理
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl)
{
switch (receivedEvent.subtype)
{
case UIEventSubtypeRemoteControlTogglePlayPause:
break;
case UIEventSubtypeRemoteControlPlay:
break;
case UIEventSubtypeRemoteControlPause:
break;
case UIEventSubtypeRemoteControlPreviousTrack:
break;
case UIEventSubtypeRemoteControlNextTrack:
break;
default:
break;
}
}
}