在 iPhone SDK 4.0 中使用 MPMovieController 的问题

发布于 2024-09-08 18:20:15 字数 2138 浏览 2 评论 0原文

我在我的应用程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2024-09-15 18:20:15

问题已解决 - 为了那些陷入类似问题的人的利益,解决方案是显式创建 MPMoviePlayerController 视图的框架,如下所示:

我将lines:更改

[videoPlayer play];

[videoPlayer setFullscreen:YES];

[self.view addSubview:videoPlayer.view];

为以下内容:

[videoPlayer prepareToPlay];

[videoPlayer play];

[self.view addSubview:videoPlayer.view];

videoPlayer.view.frame = CGRectMake(0.0, 0.0, 480.0, 320.0); //this is explicitly added and solves the problem

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:

[videoPlayer play];

[videoPlayer setFullscreen:YES];

[self.view addSubview:videoPlayer.view];

to the following:

[videoPlayer prepareToPlay];

[videoPlayer play];

[self.view addSubview:videoPlayer.view];

videoPlayer.view.frame = CGRectMake(0.0, 0.0, 480.0, 320.0); //this is explicitly added and solves the problem
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文