ios网络学习------9 播放网络视频

IOS提供了叫做MPMoviePlayerController  MPMoviePlayerViewController两个类。能够轻松用来实现视频播放。

MPMoviePlayerViewController仅仅能全屏播放视频。

#import "MainViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface MainViewController ()
//视频播放器
@property (strong, nonatomic) MPMoviePlayerController *player;
@property (strong, nonatomic) UIImageView *imageView;
@end

@implementation MainViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //实例化视频播放器
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"promo_full" withExtension:@"mp4"];
    
    //视频播放是流媒体的播放模式。所谓流媒体就是把视频数据像流水一样。变载入,变播放。
//    //提示:假设url中包括中文。须要加入百分号。

// NSString *urlString = @"http:www.xxx.com/video/xxx.mp4"; // NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; self.player = [[MPMoviePlayerController alloc]initWithContentURL:url]; //1设置播放器的大小 [self.player.view setFrame:CGRectMake(0, 0, 320, 180)]; //16:9是主流媒体的样式 //2将播放器视图加入到根视图 [self.view addSubview:self.player.view]; //4播放 [self.player play]; //[self.player stop]; //通过通知中心。以观察者模式监听视频播放状态 //1 监听播放状态 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stateChange) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; //2 监听播放完毕 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(finishedPlay) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; //3视频截图 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(caputerImage:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil]; //3视频截图 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(caputerImage:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil]; //4退出全屏通知 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(exitFullScreen) name:MPMoviePlayerDidExitFullscreenNotification object:nil]; //异步视频截图,能够在attimes指定一个或者多个时间。 [self.player requestThumbnailImagesAtTimes:@[@10.0f, @20.0f] timeOption:MPMovieTimeOptionNearestKeyFrame]; UIImageView *thumbnailImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 160, 90)]; self.imageView = thumbnailImageView; [self.view addSubview:thumbnailImageView]; } #pragma mark 退出全屏 - (void)exitFullScreen { NSLog(@"退出全屏"); } #pragma mark -播放器事件监听 #pragma mark 视频截图 这种方法是异步方法 - (void)caputerImage:(NSNotification *)notification { NSLog(@"截图 %@", notification); UIImage *image = notification.userInfo[@"MPMoviePlayerThumbnailImageKey"]; [self.imageView setImage:image]; } #pragma mark 播放器事件监听 #pragma mark 播放完毕 - (void)finishedPlay { NSLog(@"播放完毕"); } #pragma mark 播放器视频的监听 #pragma mark 播放状态变化 /* MPMoviePlaybackStateStopped, //停止 MPMoviePlaybackStatePlaying, //播放 MPMoviePlaybackStatePaused, //暂停 MPMoviePlaybackStateInterrupted, //中断 MPMoviePlaybackStateSeekingForward, //快进 MPMoviePlaybackStateSeekingBackward //快退 */ - (void)stateChange { switch (self.player.playbackState) { case MPMoviePlaybackStatePaused: NSLog(@"暂停"); break; case MPMoviePlaybackStatePlaying: //设置全屏播放 [self.player setFullscreen:YES animated:YES]; NSLog(@"播放"); break; case MPMoviePlaybackStateStopped: //注意:正常播放完毕。是不会触发MPMoviePlaybackStateStopped事件的。 //调用[self.player stop];方法能够触发此事件。 NSLog(@"停止"); break; default: break; } } @end



posted on 2018-02-03 10:29  yjbjingcha  阅读(141)  评论(0编辑  收藏  举报

导航