播放本地MP4

作者: 失忆的程序员 | 来源:发表于2022-01-25 10:42 被阅读0次
super dev.jpg
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
@property (strong, nonatomic) MPMoviePlayerController *player;

- (void)SetupVideoPlayer
{
    NSString *myFilePath = [[NSBundle mainBundle]pathForResource:@"name"ofType:@"mp4"];
    NSURL *movieURL = [NSURL fileURLWithPath:myFilePath];
    self.player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    [self addSubview:self.player.view];
    self.player.shouldAutoplay = YES;
    [self.player setControlStyle:MPMovieControlStyleNone];
    self.player.repeatMode = MPMovieRepeatModeOne;
    [self.player.view setFrame:self.bounds];
    self.player.view.alpha = 0;
    [UIView animateWithDuration:4 animations:^{
        self.player.view.alpha = 1;
        [self.player prepareToPlay];
    }];
}
不好用 用下面这个

- (void)SetupVideoPlayer
{
    NSString *myFilePath = [[NSBundle mainBundle]pathForResource:@"name"ofType:@"mp4"];
    NSURL *_videoUrl = [NSURL fileURLWithPath:myFilePath];
    //初始化视频播放器控制器
    self.playerVC = [[AVPlayerViewController alloc] init];
    //初始化播放器
    self.playerVC.player = [AVPlayer playerWithURL:_videoUrl];//[AVPlayer playerWithURL:[_videoUrl hasPrefix:@"http"] ? [NSURL URLWithString:_videoUrl]:[NSURL fileURLWithPath:_videoUrl]];
    //设置视频图像位置和大小
    self.playerVC.view.frame = self.bounds;
    //显示播放控制按钮
    self.playerVC.showsPlaybackControls = YES;
    //self.playerVC.entersFullScreenWhenPlaybackBegins = YES;//开启这个播放的时候支持(全屏)横竖屏哦
    //self.playerVC.exitsFullScreenWhenPlaybackEnds = YES;//开启这个所有 item 播放完毕可以退出全屏
    [self addSubview:self.playerVC.view];
    self.playerVC.showsPlaybackControls = NO; // 清空进度条 ...
    self.playerVC.view.backgroundColor = [UIColor whiteColor]; // 背景色
    
    [self.playerVC.player play];

//    //加载好之后,播放
//    if (self.playerVC.readyForDisplay) {
//        [self.playerVC.player play];
//    }
    
    
}
work.gif

相关文章

网友评论

    本文标题:播放本地MP4

    本文链接:https://www.haomeiwen.com/subject/owhwhrtx.html