无尽的视频播放 ios

Posted

技术标签:

【中文标题】无尽的视频播放 ios【英文标题】:endless video playback ios 【发布时间】:2017-02-08 10:11:18 【问题描述】:

我想播放一个视频作为我的视图的背景,所以我决定使用 MPMoviePlayerController 来播放无尽的视频,我做了这样的事情:

NSString *pathForFile = [[NSBundle mainBundle] pathForResource:@"clear" ofType:@"mp4"];
player = [[MPMoviePlayerController alloc] init];
player.shouldAutoplay = YES;
player.repeatMode = MPMovieRepeatModeNone;
player.fullscreen = YES;
player.movieSourceType = MPMovieSourceTypeFile;
player.scalingMode = MPMovieScalingModeAspectFill;
player.contentURL =[NSURL fileURLWithPath:pathForFile];
player.controlStyle = MPMovieControlStyleNone;
[player.view setFrame:self.view.bounds];
[player.view setUserInteractionEnabled:NO];
[player.view setAlpha:0.0f];
[self.view addSubview:player.view];
[self.view sendSubviewToBack:player.view];
[player prepareToPlay];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayerDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:player];

- (void)moviePlayerDidFinish:(NSNotification *)note 
if (note.object == player) 
    NSInteger reason = [[note.userInfo objectForKey:@"MPMoviePlayerPlaybackDidFinishReasonUserInfoKey"] integerValue];
    if (reason == MPMovieFinishReasonPlaybackEnded) 
        [player play];
    

每次视频结束时都会调用我的方法,但播放器不再播放视频。我做错了什么?

提前致谢。

【问题讨论】:

[玩家 seekToTime:kCMTimeZero];在 if (reason == MPMovieFinishReasonPlaybackEnded) 条件中使用这一行 【参考方案1】:
- (void)moviePlayerDidFinish:(NSNotification *)note 
    if (note.object == player) 
        NSInteger reason = [[note.userInfo objectForKey:@"MPMoviePlayerPlaybackDidFinishReasonUserInfoKey"] integerValue];
        if (reason == MPMovieFinishReasonPlaybackEnded) 
             player.seekToTime[kCMTimeZero];
             // you must have to give time to start video again so use seekToTime
             [player play];
        
    

【讨论】:

@AlexChekel 解决方案的另一个选择是 AVPlayerLooper 不同时期,我认为是 MPMoviePlayerController 的 bug @HimanshuMoradiya @AlexChekel MPMoviePlayerController 可能存在一些错误,但我建议您使用 avplayer 进行循环

以上是关于无尽的视频播放 ios的主要内容,如果未能解决你的问题,请参考以下文章

iOS视频流开发 — 视频播放

iOS开发系列--音频播放录音视频播放拍照视频录制

iOS开发系列--音频播放录音视频播放拍照视频录制

iOS音视频播放

iOS视频播放常用重点知识

iOS音频播放录音视频播放拍照视频录制