ios播放视频文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController。这两者的区别就是MPMoviePlayerViewController里面包含了一个MPMoviePlayerController 先说MPMoviePlayerController 首先要包含 #import <MediaPlayer/MediaPlayer.h>头文件和MediaPlayer.framework。 - (void)createMPPlayerController:(NSString *)sFileNamePath { NSURL *movieURL = [NSURL fileURLWithPath:sFileNamePath]; MPMoviePlayerController *movewController =[[MPMoviePlayerController alloc] initWithContentURL:movieURL]; [movewController prepareToPlay]; [self.view addSubview:movewController.view];//设置写在添加之后 // 这里是addSubView movewController.shouldAutoplay=YES; [movewController setControlStyle:MPMovieControlStyleDefault]; [movewController setFullscreen:YES]; [movewController.view setFrame:self.view.bounds]; 这里注册相关操作的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moveViewController.movewController]; //播放完后的通知 [movewController release]; } -(void)movieFinishedCallback:(NSNotification*)notify { MPMoviePlayerController* theMovie = [notifyobject]; [[NSNotificationCenterdefaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie]; [theMovie.view removeFromSuperview]; [theMovie release]; } end 2.介绍下MPMoviePlayerViewController。 注意:MPMoviePlayerViewController 必须 presentMoviePlayerViewControllerAnimated方式添加,否则Done按钮是不会响应通知MPMoviePlayerPlaybackDidFinishNotification事件的; - (void)createMPPlayerController:(NSString *)sFileNamePath { MPMoviePlayerViewController *moviePlayer =[[MPMoviePlayerViewControlleralloc] initWithContentURL:[NSURLfileURLWithPath:sFileNamePath]]; [moviePlayer.moviePlayerprepareToPlay]; [selfpresentMoviePlayerViewControllerAnimated:moviePlayer]; // 这里是presentMoviePlayerViewControllerAnimated [moviePlayer.moviePlayersetControlStyle:MPMovieControlStyleFullscreen]; [moviePlayer.view setBackgroundColor:[UIColor clearColor]]; [moviePlayer.view setFrame:self.view.bounds]; [[NSNotificationCenterdefaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer.moviePlayer]; [moviePlayer release]; } -(void)movieStateChangeCallback:(NSNotification*)notify { //点击播放器中的播放/ 暂停按钮响应的通知 } -(void)movieFinishedCallback:(NSNotification*)notify{ // 视频播放完或者在presentMoviePlayerViewControllerAnimated下的Done按钮被点击响应的通知。 MPMoviePlayerController* theMovie = [notifyobject]; [[NSNotificationCenterdefaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie]; [selfdismissMoviePlayerViewControllerAnimated]; }
MPMoviePlayerViewController 和 MPMoviePlayerController
最新推荐文章于 2019-05-14 08:44:00 发布