iOS Developer ios零碎记录

IOS-视频播放

2016-08-21  本文已影响177人  如风家的秘密

1 ios提供了内置的播放器

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

2 代码实现

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

故事板的绘制
代码:

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

@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

视频播放
上一篇 下一篇

猜你喜欢

热点阅读