工作生活

iOS 小视频循环播放

2019-07-01  本文已影响0人  天蝎座沫沫

项目中以前使用的是MPMoviePlayerController

        NSString *webPPath = [createPath stringByAppendingPathComponent:_model.VediaName];
     
        NSURL *mpUrl;
        if([fileManager fileExistsAtPath:webPPath]){//文件存在
            mpUrl = [NSURL fileURLWithPath:webPPath];  
        }else{
            mpUrl =[NSURL URLWithString:[NSString    stringWithFormat:@"%@%@",Mp4PathUrl,_model.VediaName]];
        }
        _mpc = [[MPMoviePlayerController alloc]init];
        [_headerView addSubview:_mpc.view];
        
        _mpc.contentURL = mpUrl;
        [_mpc prepareToPlay];
        _mpc.repeatMode = MPMovieRepeatModeOne;
        _mpc.scalingMode=MPMovieScalingModeAspectFit;
        [_mpc setControlStyle:MPMovieControlStyleNone];
        [_mpc play];

不幸的是在iOS13使用到MPMoviePlayerController的地方,直接抛了异常:

'MPMoviePlayerController is no longer available. Use AVPlayerViewController in AVKit.' 

于是改用AVPlayerViewController

       AVPlayer * player;
        if([fileManager fileExistsAtPath:webPPath]){//文件存在
             player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:webPPath]];
        }else{
             player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:_model.VediaName]];
        }
        _mpc = [[AVPlayerViewController alloc]init];
        _mpc.showsPlaybackControls = NO;
        [_headerView addSubview:_mpc.view];
        _mpc.player = player;
        [player play];

实现循环播放

///添加通知
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:_mpc.player.currentItem];
///播放完成后重复播放
-(void)playbackFinished:(NSNotification *)notification{
    [_mpc.player seekToTime:CMTimeMake(0, 1)];
    [_mpc.player play];
}
上一篇 下一篇

猜你喜欢

热点阅读