iOS 视频播放基础

#import "ViewController.h"

//视频播放 导入#import <MediaPlayer/MediaPlayer.h>

#import <MediaPlayer/MediaPlayer.h>

#import <AVFoundation/AVFoundation.h>

//avasset 通过url 获得视频的资源

// AVAssetImageGenerator 可以得到视频里面的图片

//如果想使用AVAssetImageGenerator 必须导入#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

{

    MPMoviePlayerController *moviePlayer;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

//    moviePlayer 可以通过通知来监测到视频的播放状态MPMoviePlayerPlaybackStateDidChangeNotification

//   

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeState:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];

    

//    播放视频

    NSString *path = [[NSBundle mainBundle]pathForResource:@"06-图片轮播器01-基本功能.mp4" ofType:nil];

   UIImage *image = [self thumbnailImage:path];

    

    self.view.backgroundColor = [UIColor colorWithPatternImage:image];

    NSURL *url = [NSURL fileURLWithPath:path];

    moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];

    //    设置视频 全频

    moviePlayer.fullscreen = YES;

//    需要 设置moviePlayer.viewframe

//    moviePlayer.view.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));

    需要把 视频播放器的view 添加在需要播放的视图上

//    [self.view addSubview:moviePlayer.view];

//    设置视频显示的控制样式

//    moviePlayer.controlStyle = MPMovieControlStyleEmbedded;

    //    设置视频是否循环播放 MPMovieRepeatModeNone(不循环播放), MPMovieRepeatModeOne(循环播放)

//   是否允许分享到AirPlay

//    moviePlayer.allowsAirPlay = YES;

//    如果播放的是网络视频的话 是否需要继续 自动继续自动播放

//    moviePlayer.shouldAutoplay = YES;

    


    

//    [moviePlayer setFullscreen:YES animated:YES];

//    moviePlayer.repeatMode = MPMovieRepeatModeNone;

    

//    播放

//    [moviePlayer play];

//    NSLog(@"是否全屏%d",moviePlayer.isFullscreen);

    

    

//    获得视频的加载状态

//    MPMovieLoadStateUnknown        = 0,

//    MPMovieLoadStatePlayable       = 1 << 0,

//    MPMovieLoadStatePlaythroughOK  = 1 << 1, // Playback will be automatically started in this state when shouldAutoplay is YES

//    MPMovieLoadStateStalled        = 1 << 2, // Playback will be automatically paused in this state, if started


    

}

- (void)changeState:(NSNotification *)not

{

    

   


    NSLog(@"%@",not.object);

    MPMoviePlayerController *mp = not.object;

//    mp.playbackState  只读属性 可以通过它来获得视频播放的状态

//    MPMoviePlaybackStateStopped,停止

//    MPMoviePlaybackStatePlaying,播放

//    MPMoviePlaybackStatePaused,暂停

//    MPMoviePlaybackStateInterrupted,中断

//    MPMoviePlaybackStateSeekingForward,快进

//    MPMoviePlaybackStateSeekingBackward 回退

    NSLog(@"%ld",mp.playbackState);

    switch (mp.playbackState) {

        case  MPMoviePlaybackStatePaused:

             NSLog(@"视频播放的当前时间:%f",moviePlayer.currentPlaybackTime);

            NSLog(@"暂停");

            break;

        case  MPMoviePlaybackStatePlaying:

            NSLog(@"播放");

            break;

        case  MPMoviePlaybackStateSeekingForward:

            NSLog(@"快进");


            break;

        default:

            break;

    }

//    通过MoviePlayerController 获得截图的方法

//    MPMovieTimeOptionNearestKeyFrame,相对时间

//    MPMovieTimeOptionExact 绝对时间

//    [moviePlayer requestThumbnailImagesAtTimes:@[@(1),@(5)] timeOption:MPMovieTimeOptionNearestKeyFrame];

//    监测截图成功的通知名字

//    MPMoviePlayerThumbnailImageRequestDidFinishNotification

    

//    MPMoviePlayerThumbnailImageKey  可以获得通知传过来内容的图片的key

}

//ffmpeg vfl 转码的开源框架

- (UIImage *)thumbnailImage:(NSString *)path

{

//    1.通过url获得视频的资源

//    AVAsset AVURLAsset 一样

    AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:path]];

//    2.初始化  视频资源图片的持有者

    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:asset];

//    3.开始截图

//    CMTime 包含了时间的值和帧数

//    拷贝了哪个时间段的图片

  CGImageRef ref = [generator copyCGImageAtTime:CMTimeMake(15000,10) actualTime:nil error:nil];

//    CGImageRef 是图片的数据  参数

    UIImage *image = [UIImage imageWithCGImage:ref];

    NSLog(@"!!%@",image);

    return image;

}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值