MPMoviePlayerController不会播放视频

时间:2013-11-04 10:38:42

标签: ios mpmovieplayercontroller

这是我的代码。我想播放一个存在于设备内存中的视频。视频格式为.mov。但我只有黑屏。

NSString *documentdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *tileDirectory = [documentdir stringByAppendingPathComponent:urlpath];
        self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:tileDirectory]];
        [self.videoPlayer.view setFrame:CGRectMake(0, 0, 320, 420)];

        [self.videoPlayer prepareToPlay];
        [self.view addSubview:self.videoPlayer.view];
        [self.videoPlayer play];

1 个答案:

答案 0 :(得分:0)

我得到了我的输出。我引用此链接:How to play video stream with MPMoviePlayerController in iOS来解决我的问题。

我修改后的代码..

self.videoPlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:urlpath]];
    self.videoPlayer.movieSourceType = MPMovieSourceTypeStreaming;
    [self.videoPlayer.view setFrame:CGRectMake(0, 0, 320, 420)];
    [self.view addSubview: self.videoPlayer.view];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
    [self.videoPlayer.backgroundView setBackgroundColor:[UIColor blackColor]];
    [self.videoPlayer play];

此行是关键.. self.videoPlayer.movieSourceType = MPMovieSourceTypeStreaming;