MPMoviePlayerController repeat mode not working in viewDidLoad

∥☆過路亽.° 提交于 2019-12-12 04:47:55

问题


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



来源:https://stackoverflow.com/questions/10489741/mpmovieplayercontroller-repeat-mode-not-working-in-viewdidload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!