视频播放器的实现有多种方式,此处主要针对主流方式实现一个播放器
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];
- 基于MPMoviePlayerController自定义
例如KRVideoPlayer
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;
}
附