视频播放

Tanghulu.png

视频播放器的实现有多种方式,此处主要针对主流方式实现一个播放器

MediaPlayer框架<MediaPlayer/MediaPlayer.h>

  • 直接用系统提供的MPMoviePlayerViewController
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"];
MPMoviePlayerViewController *moviePlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
[self presentViewController:moviePlayerVC animated:YES completion:nil];

AVKit框架<AVFoundation/AVFoundation.h>

DDYPlayerView.h

#import <UIKit/UIKit.h>
@import AVFoundation;
@interface DDYPlayerView : UIView
@property (nonatomic ,strong) AVPlayer *player;
@end

DDYPlayerView.m

#import "DDYPlayerView.h"
@implementation DDYPlayerView
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor blackColor];
    }
    return self;
}
+ (Class)layerClass {
    return [AVPlayerLayer class];
}
- (AVPlayer *)player {
    return [(AVPlayerLayer *)[self layer] player];
}
- (void)setPlayer:(AVPlayer *)player {
    [(AVPlayerLayer *)[self layer] setPlayer:player];
}

@end

获取视频第一帧

 - (UIImage *)getVideoFirstPic:(NSURL *)url {
     // 获取资源类
     AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
     // 视频中截图类
     AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
     generator.appliesPreferredTrackTransform = YES;
     //设置时间为0秒
     CMTime time = CMTimeMakeWithSeconds(0, 600);
     // 取出视频在0秒时候的图片
     CGImageRef image = [generator copyCGImageAtTime:time actualTime:nil error:nil];
     UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
     CGImageRelease(image);
     return thumb;
 }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容