笔记篇

播放本地MP4

2022-01-25  本文已影响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
上一篇 下一篇

猜你喜欢

热点阅读