简体   繁体   English

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

[英]An AVPlayerItem cannot be associated with more than one instance of 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];





    }

}

Hi I am trying to stream a video on my iphone app. 嗨,我想在我的iPhone应用程序上流式传输视频。 The code plays a local file but crashes with an error " Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'" when used to stream a video from a server. 代码播放本地文件但崩溃时出现错误“由于未捕获的异常终止应用程序'NSInvalidArgumentException',原因:'当用于从服务器流式传输视频时,'AVPlayerItem不能与多个AVPlayer实例关联'”。 Also the link streams the video absolutely fine in a browser but however i am not able to play the video in the app. 此外,链接在浏览器中流媒体视频绝对正常但是我无法在应用中播放视频。 Please Help . 请帮忙 。 Thanks 谢谢

Had same issue, try setting ContentURL after Setting the SourceType like below, 有同样的问题,尝试在设置SourceType之后设置ContentURL,如下所示,

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

Source: devforums.apple.com/message/467199 资料来源: devforums.apple.com/message/467199

It might be a problem with MovieSourceType ; 这可能是MovieSourceType一个问题; you are playing a local file but using MPMovieSourceTypeStreaming ; 您正在播放本地文件但使用MPMovieSourceTypeStreaming ; just modify the type and try again 只需修改类型,然后重试

First you have to inform to player the source type ofter you set that content of video file; 首先,您必须通知播放器您设置视频文件内容的源类型; like this 像这样

Here is two types of urls; 这是两种类型的网址; if the url is like this you write this function 如果网址是这样你编写这个函数

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

aStr = [aStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

or (second one) 或(第二个)

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];

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

Not a direct answer to the question, but if you get this error after trying to update contentURL, calling stop() before solves the problem. 不是问题的直接答案,但如果在尝试更新contentURL后出现此错误,请在解决问题之前调用stop()。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM