码迷,mamicode.com
首页 > 其他好文 > 详细

MPMoviePlayerViewController播放在线视频

时间:2015-07-08 18:56:07      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:视频   mpmovieplayerviewcon   ios   

前言:之前介绍过 使用MPMoviePlayerController播放在线视频,本文将介绍MPMoviePlayerViewController,二者的区别就是MPMoviePlayerViewController包含了一个MPMoviePlayerViewController


一 使用方法

1 初始化和展示

 _playerViewVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[self url]];
<span style="font-size:24px; color: rgb(51, 102, 255);"> </span><pre name="code" class="objc" style="color: rgb(51, 102, 255);">[self presentMoviePlayerViewControllerAnimated:self.playerViewVC];



2 具体实现


import <MediaPlayer/MediaPlayer.h>

@interface SecondVC()


@property (nonatomic,strong) MPMoviePlayerViewController *playerViewVC;

@end


@implementation SecondVC


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
}

- (NSURL *)url
{
    NSString *urlStr = @"http://7xawdc.com2.z0.glb.qiniucdn.com/o_19p6vdmi9148s16fs1ptehbm1vd59.mp4";
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url1 = [NSURL URLWithString:urlStr];
    return url1;
}

- (MPMoviePlayerViewController *)playerViewVC
{
    if (!_playerViewVC) {
        _playerViewVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[self url]];
        [self addNofi];
    }
    return _playerViewVC;
}

/**
 *  添加视频播放的通知
 */
- (void)addNofi
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackstateDidChange:)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification   //播放状态改变,可配合playbakcState属性获取具体状态
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playDidFinish:) //媒体播放完成或用户手动退出,具体完成原因可以通过通知userInfo中的key为MPMoviePlayerPlaybackDidFinishReasonUserInfoKey的对象获取
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(thumImageGet:)
                                                 name:MPMoviePlayerThumbnailImageRequestDidFinishNotification   //视频缩略图截取成功时调用
                                               object:nil];
}


- (void)playbackstateDidChange:(NSNotification *)noti
{
    switch (self.playerViewVC.moviePlayer.playbackState) {
        case MPMoviePlaybackStateInterrupted:
            //中断
            NSLog(@"中断");
            break;
        case MPMoviePlaybackStatePaused:
            //暂停
            NSLog(@"暂停");
            break;
        case MPMoviePlaybackStatePlaying:
            //播放中
            NSLog(@"播放中");
            break;
        case MPMoviePlaybackStateSeekingBackward:
            //后退
            NSLog(@"后退");
            break;
        case MPMoviePlaybackStateSeekingForward:
            //快进
            NSLog(@"快进");
            break;
        case MPMoviePlaybackStateStopped:
            //停止
            NSLog(@"停止");
            break;
            
        default:
            break;
    }
}

- (void)playDidFinish:(NSNotification *)noti
{
    //播放完成
}

/**
 *  截图完成,每截取一张,会调取一次
 *
 *  @param noti <#noti description#>
 */
- (void)thumImageGet:(NSNotification *)noti
{
    UIImage *thumImage = [[noti userInfo] objectForKey:MPMoviePlayerThumbnailImageKey];
    UIImageWriteToSavedPhotosAlbum(thumImage, nil, nil, nil);
}

#pragma mark - 
#pragma mark - action

- (IBAction)showMoviePlayerView:(id)sender {
    
    self.playerViewVC = nil;
   // [self presentViewController:self.playerViewVC animated:YES completion:nil];
    [self presentMoviePlayerViewControllerAnimated:self.playerViewVC];  //注意,在MPMoviePlayerViewController.h中对UIViewController扩展两个用于模态展示和关闭MPMoviePlayerViewController的方法,增加了一种下拉展示动画效果
    
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

MPMoviePlayerViewController播放在线视频

标签:视频   mpmovieplayerviewcon   ios   

原文地址:http://blog.csdn.net/jueyi1127/article/details/46806237

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!