MPMoviePlayerController repeat mode not working in

2019-09-17 08:16发布

问题:

It seems that I'm having a problem with repeatmodeone: it does not repeat the video.

This is the code for the video I have in the implementation:

- (void)viewDidLoad{

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Space Particle" ofType:@"mp4"]];


    MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
    [self presentMoviePlayerViewControllerAnimated:playerController];
    [playerController.moviePlayer prepareToPlay];
    playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
    playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
    playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
    [MyView1 addSubview: playerController.view];


    [playerController.moviePlayer play];
    [playerController release];playerController=nil;
}

It works as an animated background with buttons above it. The video plays but when it finishes, it does not repeat.

I found out that, as an IbAction, it repeats, but as a viewDidLoad it doesn´t.

Note that the "MyView" outlet has been linked to a custom UIButton, and it´s in the button view where the movie plays.

The videos I'm using aren't large in size.

My objective is that the movie must repeat using the viewdidload method in order to auto play and repeat.

Please, is there anything I am doing wrong? Suggestions on how to solve it? Any help would be appreciated.

回答1:

Try following code. This is working perfectly.

    NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Video" ofType:@"mp4"]];
    MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:fileUrl];
    [moviePlayerController.moviePlayer prepareToPlay];
    [moviePlayerController.moviePlayer setRepeatMode:MPMovieRepeatModeOne];
    [moviePlayerController.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
    [self.view addSubview:moviePlayerController.view];


回答2:

It's been deleted by ARC after the viewDidLoad function finished. You should hold the MPMoviePlayerController instance as a class member. NSNotification also solves the issue because it retains the object. Making it an instance variable will make notification solution redundant.



回答3:

The same issue. I solved it with the next steps: 1) Subscribe to the end playback notification

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinished:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:self.moviePlayer];

2) Start playback manually in moviePlayBackDidFinished method