iOS 在应用中播放本地视频文件

jopen 10年前

可以使用MPMoviePlayerController来播放本地视频文件

1.添加 Mediaplayer framework 并且在viewcontroller中#import <MediaPlayer/MediaPlayer.h> 

2. 把视频文件拖拽到xcode中

3. 获取文件存放的路径

NSString*thePath=[[NSBundle mainBundle] pathForResource:@"yourVideo" ofType:@"MOV"];  NSURL*theurl=[NSURL fileURLWithPath:thePath];

4. 用该路径初始化moviePlayer

self.moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];  [self.moviePlayer.view setFrame:CGRectMake(40, 197, 240, 160)];  [self.moviePlayer prepareToPlay];  [self.moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.  [self.view addSubview:self.moviePlayer.view];

5. 添加playback后需要做什么操作的控制

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];   //playBackFinished will be your own method.

 

翻译自:

http://stackoverflow.com/a/9802543/3458781


来自:http://blog.csdn.net/willyang519/article/details/39557913