zoukankan      html  css  js  c++  java
  • 视频播放器

    //  RootViewController.m
    #import "RootViewController.h"
    #import <MediaPlayer/MediaPlayer.h>
    #define PATH @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
    @interface RootViewController ()
    {
        //创建视频播放器
        MPMoviePlayerController * player;
    }
    @end
    
    @implementation RootViewController
    
    
    -(void)createBtn
    {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(100, 100, 100, 100);
        [btn setTitle:@"本地播放" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag=1;
        [self.view addSubview:btn];
        UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn1.frame = CGRectMake(100, 200, 100, 100);
        [btn1 setTitle:@"网络播放" forState:UIControlStateNormal];
        [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        btn1.tag=2;
        [btn1 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn1];
        UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame = CGRectMake(100, 300, 100, 100);
        [btn2 setTitle:@"返回" forState:UIControlStateNormal];
        [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        btn2.tag=3;
        [btn2 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn2];
    
    }
    
    -(void)pressBtn:(id)sender
    {
    //    <1>找到要播放的视频的路径
        NSString * path = [[NSBundle mainBundle] pathForResource:@"xiatianweidao" ofType:@"mp4"];
    //    <2>将字符串路径转化成NSURL
    //    NSURL * url = [NSURL fileURLWithPath:path];
        UIButton *btn =(UIButton*)sender;
        NSURL * url=nil;
        if (btn.tag==2) {
             url = [NSURL URLWithString:PATH];
        }
        else if (btn.tag==1){
            url = [NSURL fileURLWithPath:path];
        }
        else {
            [self finishPlaying:nil];
        }
        
        
        //<3>初始化视频播放器
        player = [[MPMoviePlayerController alloc]initWithContentURL:url];
        //视频播放器 是一个视图控制器 上面有一个自带的视图 所以此处需要设置一些视图的大小
        //<4>设置视图的大小
        player.view.frame = CGRectMake(0, 100, 320, 200);
        //<5>将player上的视图添加在当前视图上
        [self.view addSubview:player.view];
        //<6>视频播放器没有协议 所有视频播放器的一些操作必须借助通知中心完成 传递的数据信息 都是一些固定的字符串
        
        //通知是由视频播放器发送 当前的视图控制器作为通知的观察者
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
       //<7>准备播放视频
        //将视频放在缓存中  这样做防止播放的时候出现卡顿的现象
        [player prepareToPlay];
    }
    -(void)finishPlaying:(NSNotification *)notification
    {
        [player.view removeFromSuperview];
    }
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self createBtn];
    }

  • 相关阅读:
    javascript游戏引擎
    BI 可视化
    nodeJS 相关开源项目
    bean validation 技术规范
    JForum 源码分析
    lemon OA 我长时间经历的第一个开源项目
    Rete算法
    摘自知乎--mysql
    copy and paste ,做到这样也很牛逼了
    servlet 3.0特性说明
  • 原文地址:https://www.cnblogs.com/sayimba/p/5711950.html
Copyright © 2011-2022 走看看