objective-c - MPMoviePlayerController 在四秒后停止

标签 objective-c ios mpmovieplayercontroller

我正在尝试设置一个非常简单的视频播放器。 (iOS 5.1, Xcode 4.3.1)

-(void)playMedia {
NSString *movieFile = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieFile]];

[moviePlayer prepareToPlay];
moviePlayer.view.frame = self.view.bounds;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.fullscreen = YES;
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self.view addSubview: moviePlayer.view];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(playMediaFinished:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:moviePlayer];
[moviePlayer play];
}

调用时它工作正常,但只播放了四秒钟,然后出现黑屏。如果我在播放期间点击屏幕,它将播放整个序列。如果我停止点击屏幕四秒钟,就会出现黑屏。

我错过了什么?

库尔特


编辑后的版本运行良好。

在接口(interface)文件中:

@property (nonatomic,strong) MPMoviePlayerController *myMovieController;

在 .m 文件中:

-(void)playMedia {
NSString *movieFile = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieFile]];

[moviePlayer prepareToPlay];
moviePlayer.view.frame = self.view.bounds;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.fullscreen = YES;
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;

self.myMovieController = moviePlayer;
[self.view addSubview: self.myMovieController.view];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(playMediaFinished:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:moviePlayer];
[self.myMovieController play];
}

最佳答案

如果您使用的是 ARC,我相信您需要保留外部的 moviePlayer。我只是自己将它分配给了一个新属性。

关于objective-c - MPMoviePlayerController 在四秒后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780018/

相关文章:

iphone - 导航 iPhone 应用程序的核心数据最佳实践问题

ios - Xcode 将我的框架导入项目

ios - 适用于特定国家/地区的 Appstore EULA

ios - 使用图像创建自定义 map 注释标注,从数组访问数据

objective-c - 在 MPMoviePlayerController 中禁用用户交互

ios - Mpmovieplayercontroller video pause while clicking button for another functional

ios - sqlite3 "unable to open database file"- ios

ios - 如何使用 Reachability 的不同实现来解决不同的 CocoaPods?

ios - 模拟 CKContainer 用于单元测试

ios - 使用带有自签名证书的 MPMoviePlayerController