AVPlayerItem不能与多个AVPlayer实例相关联

时间:2012-08-20 16:12:29

标签: iphone ios video avplayer

-(IBAction)play:(id)sender
{

    thumbImageView.alpha=0.0;

    NSString *stringvideo=[NSString stringWithFormat:@"http://streaming-service",[[NSUserDefaults standardUserDefaults]valueForKey:@"VideoIdselected"]];

    NSString *videoURLString = stringvideo;

       NSURL *url = nil;

  url = [NSURL URLWithString:videoURLString];





    self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];




    if (self.moviePlayerController)
    {
        /* Save the movie object. */
          [self setMoviePlayerController:self.moviePlayerController];


        [self installMovieNotificationObservers];

        /* Specify the URL that points to the movie file. */
        [self.moviePlayerController setContentURL:url];


        [self.moviePlayerController setMovieSourceType:MPMovieSourceTypeStreaming]; // for streaming



        self.moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;

        // [self.videoPlayer setMovieSourceType:MPMovieSourceTypeStreaming];

      // self.moviePlayerController.shouldAutoplay = YES;

        [self.moviePlayerController.view setFrame: CGRectMake(0, 0, 320,160)];



        [self.moviePlayerController setFullscreen:NO animated:YES];

        [self.moviePlayerController view].backgroundColor = [UIColor lightGrayColor];

        [self.view addSubview:self.moviePlayerController.view];

        [self.view bringSubviewToFront:self.moviePlayerController.view];
        [self.moviePlayerController prepareToPlay];
        //self.moviePlayerController.useApplicationAudioSession = NO;

        [self.moviePlayerController play];





    }

}

您好我正在尝试在我的iphone应用上播放视频。代码播放本地文件但崩溃时出错 “由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'当用于从服务器流式传输视频时,'AVPlayerItem不能与多个AVPlayer实例关联'。此外,链接在浏览器中流媒体视频绝对正常但是我无法在应用中播放视频。请帮忙 。 感谢

5 个答案:

答案 0 :(得分:99)

如果出现同样的问题,请在设置SourceType之后尝试设置ContentURL,如下所示

moviePlayerController_ = [[MPMoviePlayerViewController alloc] init];
moviePlayerController_.movieSourceType = MPMovieSourceTypeStreaming;
[moviePlayerController_.moviePlayer setContentURL:url];

来源:devforums.apple.com/message/467199

答案 1 :(得分:2)

MovieSourceType可能存在问题;您正在播放本地文件但使用MPMovieSourceTypeStreaming;只需修改类型,然后重试

答案 2 :(得分:1)

首先,您必须告知播放器您设置视频文件内容的源类型;像这样

这是两种类型的网址;如果网址是这样你就写这个函数

 NSString *aStr=@"http://www.my url like this.com/video/videos/review/Michale JACKSON  AUDIENCE Comment.mp4";   

aStr = [aStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

或(第二个)

NSString *aStr=@"http://www.normal url.com/video.mp4"

// after add your url string to this url

NSURL *url=[NSURL URLWithString:aStr];
MPMoviePlayerViewController *MPlayer = [[MPMoviePlayerViewController alloc]init];
MPlayer.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[MPlayer.moviePlayer setContentURL:url];
[self presentMoviePlayerViewControllerAnimated:MPlayer];

答案 3 :(得分:1)

我使用过moviePlayerController.movi​​eSourceType = MPMovieSourceTypeUnknown它适用于我..试试吧..

答案 4 :(得分:1)

不是问题的直接答案,但如果在尝试更新contentURL后出现此错误,请在解决问题之前调用stop()。

moviePlayerController.stop()
moviePlayerController.movieSourceType = .File
moviePlayerController.contentURL = newURL