美文网首页
ios视频播放

ios视频播放

作者: 被风吹乱的思念 | 来源:发表于2017-08-19 10:42 被阅读7次

    1.效果图

    .

    2.注意点

    导入依赖库


    屏幕快照 2017-08-19 上午10.33.50.png

    3代码展示

    #import <MediaPlayer/MediaPlayer.h>
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIView *movieView;
    @property(nonnull,strong)MPMoviePlayerController *moviePlayer;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        //1. 创建本地URL(也可创建基于网络的URL)
      //播放本地视频
        NSURL *movieUrl = [[NSBundle mainBundle] URLForResource:@"9999"withExtension:@"mov"];
        
        // 使用指定URL创建MPMoviePlayerController
        
        //2. MPMoviePlayerController将会播放该URL对应的视频
        
        _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:movieUrl];
        
        
        //3. 设置该播放器的控制条风格。
        
        _moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
        
        //4. 设置该播放器的缩放模式
        
        _moviePlayer.scalingMode =MPMovieScalingModeAspectFill;
        
        [_moviePlayer.view setFrame:self.movieView.bounds];
    //这是为了检测屏幕是否变换(横屏竖屏)
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    }
    //在检测中加入视频
    - (void)orientChange:(NSNotification *)notification
    {
        [_moviePlayer.view setFrame:self.movieView.bounds];
    }
    - (IBAction)playClick:(id)sender {
        //添加视频显示内容
        
        [self.movieView addSubview:_moviePlayer.view];
        
        //添加视频声音内容
        _moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
        
        [_moviePlayer prepareToPlay];
    }
    
    

    相关文章

      网友评论

          本文标题:ios视频播放

          本文链接:https://www.haomeiwen.com/subject/taqtdxtx.html