要播放本地视频文件,您可以使用 MPMoviePlayerController。
- (void) playVideo:(NSString *) videoName {
if ([self.fileManager fileExistsAtPath:videoName]) {
[self stopVideo];
self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:videoName]];
[self.videoPlayer.view setFrame:CGRectMake(0, 0, 480, 360)];
CGAffineTransform t1;
t1 = CGAffineTransformMakeScale(-1.0,1.0);
self.videoPlayer.view.layer.transform = CATransform3DMakeAffineTransform(t1);
[self.view addSubview:self.videoPlayer.view];
[self.videoPlayer setControlStyle:MPMovieControlStyleNone];
[self.videoPlayer setScalingMode:MPMovieScalingModeAspectFit];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.videoPlayer];
[self.videoPlayer play];
}
}
- (void)stopVideo {
if (self.videoPlayer) {
if ([self.videoPlayer playbackState] == MPMoviePlaybackStatePlaying) {
[self.videoPlayer stop];
}
[self.videoPlayer.view removeFromSuperview];
self.videoPlayer = nil;
}
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[self.videoPlayer.view removeFromSuperview];
}