1.添加 Mediaplayer framework 並且在viewcontroller中#import
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.