MPMoviePlayerController stops playing after a few

2019-03-27 11:25发布

问题:

MPMoviePlayerController stops playing after a few seconds, this is the code I'm using:

NSString *urlAddress = @"http://67.159.28.74:8730";
NSURL *url = [NSURL URLWithString:urlAddress];

MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:url];
player.movieSourceType = MPMovieSourceTypeStreaming;

player.view.hidden = NO;
[self.view addSubview:player.view];
[player prepareToPlay];

if(player){
    [player play];
}

The error im getting in the console is:

2012-09-23 18:07:56.618 Reader[696:c07] [MPAVController] Autoplay: Disabling autoplay for pause

2012-09-23 18:07:56.619 Reader[696:c07] [MPAVController] Autoplay: Disabling autoplay

2012-09-23 18:07:56.638 Reader[696:c07] [MPAVController] Autoplay: Disabling autoplay for pause

2012-09-23 18:07:56.638 Reader[696:c07] [MPAVController] Autoplay: Disabling autoplay

2012-09-23 18:07:56.643 Reader[696:c07] [MPAVController] Autoplay: Enabling autoplay

2012-09-23 18:07:56.645 Reader[696:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0

2012-09-23 18:07:56.646 Reader[696:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.

2012-09-23 18:07:56.648 Reader[696:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0

2012-09-23 18:07:56.648 Reader[696:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.

2012-09-23 18:07:56.650 Reader[696:c07] [MPAVController] Autoplay: Enabling autoplay

2012-09-23 18:07:56.652 Reader[696:c07] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0

2012-09-23 18:07:58.746 Reader[696:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0

2012-09-23 18:07:58.746 Reader[696:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.

2012-09-23 18:07:58.747 Reader[696:c07] [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1

2012-09-23 18:07:58.748 Reader[696:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 1

2012-09-23 18:07:58.748 Reader[696:c07] [MPAVController] Autoplay: Enabling autoplay`

回答1:

solved it:

I turned my MPMoviePlayerController into a property:

//add in header

@property (strong,nonatomic) MPMoviePlayerController *myPlayer;

//add in .m file

- (IBAction)playStream:(id)sender {
    NSString *urlAddress = @"http://67.159.28.74:8730";
    NSURL *url = [NSURL URLWithString:urlAddress];

    MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:url];
    [player prepareToPlay];
    //Remember you have to add MediaPlayer.framework to your project
    /*[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:player];*/

    player.movieSourceType = MPMovieSourceTypeStreaming;



    player.view.hidden = YES;

    self.myPlayer = player;

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


    if(player){
      [self.myPlayer play];
    }
}


回答2:

You have to set a class variable like this:

@property (nonatomic, retain) MPMoviePlayerController *moviePlayer;

My code looks like this:

NSURL *theUrl = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"mp4"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theUrl];
[moviePlayer prepareToPlay];
[moviePlayer.view setFrame: self.view.bounds];
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self.view addSubview: moviePlayer.view];
[moviePlayer play];

This works perfect for me!



回答3:

on xCode 4.6.1 it worked fine.

But i had this problem with xCode 5!

But this solution is very helpful:

"The solution is that the player would have to be an instance variable or property of the view controller. ie We must use the instance of MPMoviePlayerController

@property (nonatomic,strong) MPMoviePlayerController *myMovieController;"

Thank author very much!

And this is my code:

UIGraphicsBeginImageContext(CGSizeMake(1,1));

NSString *path = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
self.moviePlayerView = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[self.moviePlayerView prepareToPlay];
[self.moviePlayerView.view setFrame:CGRectMake(0, 0, 320, self.view.frame.size.height/2)];
self.moviePlayerView.view.backgroundColor = [UIColor clearColor];
[self.moviePlayerView setContentURL:videoURL];

self.moviePlayerView.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayerView.controlStyle = MPMovieControlStyleDefault;
[self.view addSubview:self.moviePlayerView.view];
self.moviePlayerView.shouldAutoplay = NO;
[self.moviePlayerView play];

UIGraphicsEndImageContext();


回答4:

You can try to do this just after you instantiate your player: [player prepareToPlay]

More info here: iOS 6 streaming player com.apple.coremedia.networkbuffering bug



回答5:

Try this it's work well for me in ios6

NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"ddd" ofType:@"mp4"];
NSLog(@"%@",resourcePath);
NSURL *url = [NSURL fileURLWithPath:resourcePath];
NSLog(@"%@",url);
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayer.view.frame = self.view.frame;
moviePlayer.moviePlayer.shouldAutoplay=YES;
moviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
[moviePlayer.moviePlayer setFullscreen:YES animated:YES];
[self.view addSubview:moviePlayer.view];
[moviePlayer.moviePlayer play];


回答6:

Here is an interesting line of code which you are missing.

According to Apple's document

" When you add a movie player’s view to your app’s view hierarchy, be sure to size the frame correctly, as shown here: http://tinyurl.com/9wgv9wc

MPMoviePlayerController *player =

    [[MPMoviePlayerController alloc] initWithContentURL: myURL];

[player prepareToPlay];

[player.view setFrame: myView.bounds];  // player's frame must match parent's

[myView addSubview: player.view];

// ...

[player play];

http://tinyurl.com/aeh9f84



回答7:

The solution is that the player would have to be an instance variable or property of the view controller. ie We must use the instance of MPMoviePlayerController

@property (nonatomic,strong) MPMoviePlayerController *myMovieController;