Tanghulu.png
視頻播放器的實(shí)現(xiàn)有多種方式,此處主要針對(duì)主流方式實(shí)現(xiàn)一個(gè)播放器
MediaPlayer框架<MediaPlayer/MediaPlayer.h>
- 直接用系統(tǒng)提供的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;
//設(shè)置時(shí)間為0秒
CMTime time = CMTimeMakeWithSeconds(0, 600);
// 取出視頻在0秒時(shí)候的圖片
CGImageRef image = [generator copyCGImageAtTime:time actualTime:nil error:nil];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return thumb;
}
附