常用的方法有2种,一种是利用HTML5技术,使用一个webView来播放;第二种是使用MediaPlayer来播放,我这里介绍下第2种方法。
效果如下图显示:
首先需要引用framework:MediaPlayer.framework
然后在头文件中
#import <MediaPlayer/MediaPlayer.h> @property (nonatomic,retain) MPMoviePlayerController *player;
在M文件中开始实现方法
/** @method 播放视频 */ - (void)playMovie:(NSString *)fileURL{ //本地视频文件路径 // NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"]; //网络视频URL NSURL *url = [NSURL URLWithString:fileURL]; // NSLog(@"%@", url); //视频播放对象 self.player = [[MPMoviePlayerController alloc] init]; //设置为流媒体模式 self.player.movieSourceType = MPMovieSourceTypeStreaming; [self.player setControlStyle:MPMovieControlStyleFullscreen]; [self.player.view setFrame:self.view.bounds]; [self.player setContentURL:url]; self.player.fullscreen = YES;//全屏播放 self.player.initialPlaybackTime = -1; [self.view addSubview:self.player.view]; // 注册一个播放结束的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player]; [self.player prepareToPlay]; // [sharedData stopLoading]; [self.player play]; } #pragma mark -------------------视频播放结束委托-------------------- /* @method 当视频播放完毕释放对象 */ - (void)myMovieFinishedCallback:(NSNotification*)notify { //视频播放对象 MPMoviePlayerController* theMovie = [notify object]; //销毁播放通知 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie]; // 释放视频对象 [theMovie.view removeFromSuperview]; }
通过调用[self playMovie:videoURL];就可以播放视频文件了,很easy吧~