zoukankan      html  css  js  c++  java
  • 利用MPMoviePlayerViewController 播放视频 iOS

    方法一:

    @property (nonatomic, strong) MPMoviePlayerController *player;

        NSString *url = [[NSBundle mainBundle] pathForResource:@"zz_video" ofType:@"mp4"];
        self.player = [[MPMoviePlayerController alloc]
                       initWithContentURL: [NSURL fileURLWithPath:url]];
        self.player.controlStyle = MPMovieControlStyleNone;
        self.player.view.frame = self.view.frame;
        [self.view addSubview:self.player.view];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(movieFinishedCallback:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:self.player];
        
        [self.player play]; //开始播放
    - (void) movieFinishedCallback:(NSNotification*) aNotification {
        MPMoviePlayerController *player = [aNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
        [player stop];
    }

    方法二:

    1、要使用MPMoviePlayerViewController首先要加入MediaPlayer.framework. 
    2、加入如下代码:

    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString *url = [[NSBundle mainBundle] pathForResource:@"TaylorSwift-LoveStory" ofType:@"mp4"];
        
        MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:[playerViewController moviePlayer]];
        //-- add to view---
        [self.view addSubview:playerViewController.view];
        
        //---play movie---
        MPMoviePlayerController *player = [playerViewController moviePlayer];
        [player play];
        
    }
    
    - (void) movieFinishedCallback:(NSNotification*) aNotification {
        MPMoviePlayerController *player = [aNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
        [player stop];
        [self.view removeFromSuperView];
        [player autorelease];
    }

    3、支持横屏修改shouldAutorotateToInterfaceOrientation:interfaceOrientation方法使其返回YES。

    备注:实测 用上面的方法直接播放本地m3u8格式或者线上的视频也是可以的。

    NSURL可直接初始化:

    NSURL*videoPathURL=[NSURL URLWithString:urlStr];//urlStr是视频播放地址

    如果是播放本地视频的话。这样初始化:

    NSURL*videoPathURL=[[NSURL alloc] initFileURLWithPath:urlStr];

    转:http://re-reference.iteye.com/blog/1106324

  • 相关阅读:
    解决Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile
    eclipse下解决明明有jar包,却找不到的问题
    Ngnix负载均衡安装及配置
    Redis入门教程(二)
    js监听组合按键
    js清空数组的方法
    js判断浏览器是否支持webGL
    Maven入门教程(一)
    杂记
    第一天 Requests库入门
  • 原文地址:https://www.cnblogs.com/ygm900/p/3991359.html
Copyright © 2011-2022 走看看