簡易視頻播放

//數組

//  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

相關文章
相關標籤/搜索