An AVPlayerItem cannot be associated with more tha

2020-05-26 05:38发布

问题:

-(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. 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. 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

回答1:

Had same issue, try setting ContentURL after Setting the SourceType like below,

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

Source: devforums.apple.com/message/467199



回答2:

It might be a problem with MovieSourceType; you are playing a local file but using MPMovieSourceTypeStreaming; just modify the type and try again



回答3:

i have used moviePlayerController.movieSourceType = MPMovieSourceTypeUnknown its works for me.. try it..



回答4:

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


回答5:

Not a direct answer to the question, but if you get this error after trying to update contentURL, calling stop() before solves the problem.

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