简易视频播放

//数组

//  ViewController.m网络

//  视频播放atom

//url

//  Created by dc0061 on 15/12/28.spa

//  Copyright © 2015 dc0061. All rights reserved..net

//3d


#import "ViewController.h"rest

#import <MediaPlayer/MediaPlayer.h>//导入库code


@interface ViewController ()orm


@property (nonatomic, strong) MPMoviePlayerController *movePlayer;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //开始播放

    [self.moviePlayer play];

    //添加通知

    [self addNotification];

    //剪切图片

    [self imageRequest];

}


//建立视频

- (MPMoviePlayerController *) moviePlayer{

    if(!_movePlayer){

        //获取视频地址(能够是本地,也能够是网上)

        NSString *path=[[NSBundle mainBundle] pathForResource:@"01九宫格介绍01" ofType:@"mp4"];

        NSURL *url=[NSURL fileURLWithPath:path];

        //初始化网络视频播放器

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

        _movePlayer.view.frame=self.view.frame;

        //播放器视图--->自适应屏幕宽高

        _movePlayer.view.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

        [self.view addSubview:_movePlayer.view];

    }

    return _movePlayer;

}

- (void) imageRequest{

    //参数第一个数组(传入获取图片的时间点),第二个时间选项(获取时间点附近最近的一帧)

    [self.movePlayer requestThumbnailImagesAtTimes:@[@3.0,@18.0] timeOption:MPMovieTimeOptionNearestKeyFrame];

}

//添加通知

- (void) addNotification{

    NSNotificationCenter *notficaton=[NSNotificationCenter defaultCenter];

    //添加状态改变的通知

    [notficaton addObserver:self selector:@selector(stateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.movePlayer];

    //添加播放完成状态

    [notficaton addObserver:self selector:@selector(finishPlayer:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.movePlayer];

    //添加一个缩略图的通知

    [notficaton addObserver:self selector:@selector(imageRequestFinished:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:self.movePlayer];

}

//视屏缩略图-->此方法每次截图成功都会调用一次

- (void) imageRequestFinished : (NSNotification *) notification{

    NSLog(@"视屏截图成功");

    UIImage *image=notification.userInfo[MPMoviePlayerThumbnailImageKey];

    //保存在模拟器的相册里面

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

}

//添加播放完成状态

- (void) finishPlayer : (NSNotification *) notification{

    NSLog(@"播放结束,移除全部的通知监控");

    [self deallocs];

}

//添加状态改变的通知

- (void) stateChange : (NSNotification *) notification{

    //判断视屏播放状态

    switch (self.movePlayer.playbackState) {

        case MPMoviePlaybackStatePlaying:

            NSLog(@"播放状态");

            break;

        case MPMoviePlaybackStatePaused:

            NSLog(@"暂停播放");

            break;

        case MPMoviePlaybackStateStopped:

            NSLog(@"中止播放");

            break;

        default:

            NSLog(@"播放状态为: %li",self.movePlayer.playbackState);

            break;

    }

}


- (void) deallocs{

    //移除全部的通知监控

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

相关文章
相关标签/搜索