ios播放视频文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController。前者是一个view,后者是个Controller。区别就是MPMoviePlayerViewController里面包含了一个MPMoviePlayerController
这两个都继承于NSObject,都能播放本地视频、流媒体、网络视频
首先要包含
#import
<MediaPlayer/MediaPlayer.h>头文件
一、MPMoviePlayerController 自定义播放视频方法
MPMoviePlayerController
*movie;
- (
void
)viewDidLoad {
[super viewDidLoad];
if(movie == nil)
NSURL
*url = [
NSURL
movie
= [[
MPMoviePlayerController
alloc
]
initWithContentURL
:url];
movie
.
view
.
frame
=
CGRectMake
(
20
,
20
,
200
,
200
);
movie
.
controlStyle
=
MPMovieControlStyleFullscreen;
[self.view
addSubview
:
movie
.
view
];
}
// 注册一个播放结束的通知 [[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (myMovieFinishedCallback:) name : MPMoviePlayerPlaybackDidFinishNotification object : movie ];
[movie play];
}
#pragma mark -------------------视频播放结束委托--------------------
/* @method 当视频播放完毕释放对象 */ -(void)myMovieFinishedCallback:(NSNotification*)notify { //视频播放对象 MPMoviePlayerController* theMovie = [notify object]; //销毁播放通知 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie]; [theMovie.view removeFromSuperview];
}
|
二、MPMoviePlayerViewController 自定义播放视频方法
注意:MPMoviePlayerViewController 必须 presentMoviePlayerViewControllerAnimated方式添加,否则Done按钮是不会响应通知MPMoviePlayerPlaybackDidFinishNotification事件的;
--------------------------------------------------------
代码--------------------------------------------------
MPMoviePlayerViewController *
moviePlayer;
- (
IBAction
)play:(
UIButton
*)sender {
MPMoviePlayerViewController *moviePlay = [[ MPMoviePlayerViewController alloc ] initWithContentURL :[ NSURL URLWithString : @"http://bcs.duapp.com/chenwei520/media/mobile_vedio.mp4" ]]; moviePlay. moviePlayer . view . backgroundColor = [ UIColor clearColor ]; // moviePlay.moviePlayer.controlStyle = MPMovieControlStyleFullscreen; /** 缓存 */ [moviePlay. moviePlayer prepareToPlay ]; /** 播放 */ [moviePlay. moviePlayer play ]; [ self presentMoviePlayerViewControllerAnimated :moviePlay]; /** 注册通知事件 */ [[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (myMovieFinishedCallback:) name : MPMoviePlayerPlaybackDidFinishNotification object : nil ]; } // 通知触发事件方法 - ( void )myMovieFinishedCallback:( NSNotification *)notif { // 拿到播放器 MPMoviePlayerController *player = notif. object ; if (player. playbackState == MPMoviePlaybackStateStopped ) { NSLog ( @" 停止播放 " ); } // 销毁通知 [[ NSNotificationCenter defaultCenter ] removeObserver : self name : MPMoviePlayerPlaybackDidFinishNotification object :player]; // 移除视图 [player. view removeFromSuperview ];
}
--------------------------------------------------------
代码--------------------------------------------------
以下是资料
moviePlayer.moviewControlMode = MPMovieControlModeDefault; MPMovieControlModeDefault 显示播放 / 暂停、音量和时间控制 MPMovieControlModeVolumeOnly 只显示音量控制 MPMovieControlModeHidden 没有控制器 moviePlayer.scallingMode = MPMovieScallingModeAspectFit; 你可以使用下列宽高比值: MPMovieScallingModeNone 不做任何缩放 MPMovieScallingModeAspectFit 适应屏幕大小,保持宽高比 MPMovieScallingModeAspectFill 适应屏幕大小,保持宽高比,可裁剪 MPMovieScallingModeFill 充满屏幕,不保持宽高比 MPMoviePlayerContentPreloadDidFinishNotification 当电影播放器结束对内容的预加载后发出。因为内容可以在仅加载了一部分的情况下播放,所以这个通知可能在已经播放后才发出。 MPMoviePlayerScallingModeDidChangedNotification 当用户改变了电影的缩放模式后发出。用户可以点触缩放图标,在全屏播放和窗口播放之间切换。 MPMoviePlayerPlaybackDidFinishNotification
当电影播放完毕或者用户按下了
Done
按钮后发出
|