IOS-视频播放

1 ios提供了内置的播放器

视频播放有三种方式:(都支持流媒体和本地视频播放)

  • MPMoviePlayerController:可以改变大小(必须设置frame才能播放,需要添加到控制器视图上,并且可以监听通知:进度的改变,播放是否完成)
  • MPMoviePlayerViewController:是一个特殊的视图控制器类,它包含了一个播放器 (MPMoviePlayerController),可直接播放,注意需要引入框架
  • AVPlayerViewContrller:它自带AVPlayer的播放器(必须初始化才能使用),需要设置frame,并添加到当前视图控制器上.并且还需要引入两个框架

2 代码实现

现在故事板上分别放三个按钮:故事板绘制如下图:

IOS-视频播放_第1张图片
故事板的绘制

代码:


#import "ViewController.h"
#import 
#import 
#import 

@interface ViewController (){
    MPMoviePlayerController *movieController;
    MPMoviePlayerViewController *movieViewController;
    AVPlayerViewController *playerVC;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)movieController:(id)sender {
    
    //添加通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAction:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishAction:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
    //网络加载视频
    NSString *urlStr1 = @"http://vf1.mtime.cn/Video/2012/06/21/mp4/120621104820876931.mp4";
    NSURL *url1 = [NSURL URLWithString:urlStr1];
    //本地加载视频
    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
  
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    
    movieController = [[MPMoviePlayerController alloc] initWithContentURL:url];
    movieController.view.frame = CGRectMake(50, 200, 300, 300);
    [movieController prepareToPlay];
    [movieController play];
    [self.view addSubview:movieController.view];
}

- (IBAction)movieViewController:(id)sender {
    
    //网络加载视频
    NSString *urlStr1 = @"http://vf1.mtime.cn/Video/2012/06/21/mp4/120621104820876931.mp4";
    NSURL *url1 = [NSURL URLWithString:urlStr1];
    //本地加载视频
    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
    NSURL *url2 = [NSURL fileURLWithPath:urlStr];
    
   movieViewController = [[MPMoviePlayerViewController  alloc] initWithContentURL:url2];
  
    [self presentViewController:movieViewController animated:YES completion:nil];
}

- (IBAction)AVMoviePlayerC:(id)sender {
    
    //本地加载视频
    NSString *urlStr1 = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
    NSURL *url1 = [NSURL fileURLWithPath:urlStr1];
    
    //网络加载视频
    NSString *urlStr = @"http://vf1.mtime.cn/Video/2012/06/21/mp4/120621104820876931.mp4";
    NSURL *url = [NSURL URLWithString:urlStr];
    
    playerVC = [[AVPlayerViewController alloc] init];
    playerVC.view.frame = self.view.bounds;
    playerVC.player = [[AVPlayer alloc] initWithURL:url];
    [playerVC.player play];
    [self.view addSubview:playerVC.view];
    
}

#pragma mark - 通知的方法
- (void) didFinishAction:(NSNotification *) notifi{
    
    NSLog(@"播放完成");
}

- (void) changeAction:(NSNotification *) notifi {
   
    MPMoviePlayerController *player = notifi.object;
    MPMoviePlaybackState state = player.playbackState;
    if (state == MPMoviePlaybackStatePaused) {
        NSLog(@"暂停");
    }else if (state == MPMoviePlaybackStateSeekingForward){
        NSLog(@"快进");
    }else if (state == MPMoviePlaybackStatePlaying){
         NSLog(@"播放");
    }
    
}
@end

IOS-视频播放_第2张图片
视频播放

你可能感兴趣的:(IOS-视频播放)