【问题标题】:problem using MPMovieController in iPhone SDK 4.0在 iPhone SDK 4.0 中使用 MPMovieController 的问题
【发布时间】:2010-07-04 04:56:21
【问题描述】:

我使用 MPMoviePlayer 在我的应用中播放短视频,在 SDK 3.1.3 中没有问题。我对 SDK 4 中的代码进行了更改,但视频没有播放。我只是得到一个黑屏和音频。 Apple Dev Center 没有针对最新 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 上的常规视频播放列表中都可以正常播放。

谁能帮我解决这个问题?

提前致谢

【问题讨论】:

    标签: iphone objective-c ipad video mpmovieplayer


    【解决方案1】:

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

    我换了行:

    [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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2011-05-14
      • 1970-01-01
      相关资源
      最近更新 更多