在 iPhone SDK 4.0 中使用 MPMovieController 的问题
我在我的应用程序中使用 MPMoviePlayer 播放短视频,在 SDK 3.1.3 中没有任何问题。我对 SDK 4 中的代码进行了更改,但视频无法播放。我只看到黑屏和音频。 Apple 开发中心没有最新 SDK 的此类的任何示例代码。以下是我正在使用的代码:
- (void)viewDidLoad {
[super viewDidLoad];
//videoPlayer is a MPMoviePlayerController object defined in the header file of the view controller
if (videoPlayer == nil){
NSString * videoPath = [[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"mp4"];
if (videoPath == NULL){
return;
}
NSURL * videoURL = [NSURL fileURLWithPath:videoPath];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
videoPlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:@"MPMoviePlayerPlaybackDidFinishNotification" object:movieController.moviePlayer];
[videoPlayer play];
[videoPlayer setFullscreen:YES];
[self.view addSubview:videoPlayer.view];
}
}
上面的结果只是在黑屏下播放音频。播放结束时正确调用通知。
当上述方法不起作用时,我什至尝试使用新的 MPMoviePlayerViewController 类,如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
NSString * videoPath = [[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"mp4"];
if (videoPath == NULL){
return;
}
NSURL * videoURL = [NSURL fileURLWithPath:videoPath];
//movieController is an MPMoviePlayerViewController object defined in the header file of view controller
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:@"MPMoviePlayerPlaybackDidFinishNotification" object:movieController.moviePlayer];
[movieController.moviePlayer setFullscreen:YES];
[movieController.moviePlayer play];
[self presentMoviePlayerViewControllerAnimated:movieController];
}
同样的问题仍然存在 - 我可以听到音频,并且按预期调用播放结束时的通知。但是我只看到黑屏而不是视频。
视频编码没有任何问题,因为相同的视频在 iTunes 以及我的 iPod Touch 上的常规视频播放列表中都可以正常播放。
有人能帮我解决这个问题吗?
提前致谢
I was using MPMoviePlayer to play a short video in my app with no problems in SDK 3.1.3. I made the changes to the code in SDK 4 but the video is not playing. I just get a black screen and audio. The Apple Dev Center doesnt have any sample code for this class for the latest SDK. Following is the code I'm using:
- (void)viewDidLoad {
[super viewDidLoad];
//videoPlayer is a MPMoviePlayerController object defined in the header file of the view controller
if (videoPlayer == nil){
NSString * videoPath = [[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"mp4"];
if (videoPath == NULL){
return;
}
NSURL * videoURL = [NSURL fileURLWithPath:videoPath];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
videoPlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:@"MPMoviePlayerPlaybackDidFinishNotification" object:movieController.moviePlayer];
[videoPlayer play];
[videoPlayer setFullscreen:YES];
[self.view addSubview:videoPlayer.view];
}
}
The above results in just the audio being played with a black screen. The notification is called correctly at the end of the playback.
When the above did not work, then I even tried using the new MPMoviePlayerViewController class as follows:
- (void)viewDidLoad {
[super viewDidLoad];
NSString * videoPath = [[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"mp4"];
if (videoPath == NULL){
return;
}
NSURL * videoURL = [NSURL fileURLWithPath:videoPath];
//movieController is an MPMoviePlayerViewController object defined in the header file of view controller
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:@"MPMoviePlayerPlaybackDidFinishNotification" object:movieController.moviePlayer];
[movieController.moviePlayer setFullscreen:YES];
[movieController.moviePlayer play];
[self presentMoviePlayerViewControllerAnimated:movieController];
}
Same problem persists - I can hear the audio and the notification at the end of the playback is called as expected. However I just see a black screen instead of the video.
There is nothing wrong in the encoding of the video because the same video plays fine in iTunes as well as on my iPod Touch in the regular videos playlist.
Could anyone help me out with this problem?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题已解决 - 为了那些陷入类似问题的人的利益,解决方案是显式创建 MPMoviePlayerController 视图的框架,如下所示:
我将lines:更改
为以下内容:
problem solved - for the benefit of those who are stuck on a similar issue, the solution is to explicitly create the frame for the view of the
MPMoviePlayerController
as follows:I changed the lines:
to the following: