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

    在unity游戏的开头播放视频 , 根据需求 , 最后决定用 MPMoviePlayerController 来实现播放, 实现如下: by Tin

    需要在AppController.mm的 OpenEAGL_UnityCallback  修改下view的大小

        UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        // mainView.backgroundColor = [UIColor grayColor];
        
        [MyViewController Instance].view = mainView;
        
        
        [UnityGetGLViewController().view addSubview: [MyViewController Instance].view];

    需要在游戏中接收unity的命令

    // ========================   播放开头动画  start ========================
    // by:xihao
    // 2014-05-16
    
    void PlayMovieInIOS( char * path )
    {    
        [[MyViewController Instance] PlayVideo:[NSString stringWithUTF8String:path]];
        
    }
    
    
    void exPlayVideo( char * url )
    {
        [[MyViewController Instance] PlayVideo:[NSString stringWithUTF8String:url]];
    }
    
    
    void exReleaseVideo()
    {
        [[MyViewController Instance] ReleaseVideo];
    }
    
    
    MovieViewController *  mv ;
    
    -(void) PlayVideo:(NSString *)  path
    {
        if ( mv != nil) {
            [mv breakMovie] ;
            [mv release];
            mv= nil ;
        }
        
        mv = [[ MovieViewController alloc] init];
        [self.view addSubview:mv.view];
        [mv playMovie:path];
    }
    
    
    -(void) ReleaseVideo
    {
        if ( mv != nil) {
            [mv breakMovie] ;
            [mv release];
            mv= nil ;
        }
        
        UnitySendMessage("_IOSDoor","ReleaseVideoOver", "");
    }
    
    
    // ========================   播放开头动画  end ========================

    接下来是播放视频

    MPMoviePlayerController *movie ;
    
    /**
     @method 播放电影
     */
    -(void)playMovie:(NSString *)fileName{
        
        
        NSURL *url = [NSURL fileURLWithPath: fileName ];
        //视频播放对象 
        movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
        movie.controlStyle = MPMovieControlStyleNone;
        [movie.view setFrame:self.view.bounds];
        movie.initialPlaybackTime = -1;
        [self.view addSubview:movie.view];
        // 注册一个播放结束的通知
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(myMovieFinishedCallback:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:movie];
     
        [movie play];
    }
    
    #pragma mark -------------------视频播放结束委托--------------------
    
    -(void)  breakMovie
    {
        if (movie == nil) {
            return ;
        }
        
        //销毁播放通知
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:movie];
        [movie.view removeFromSuperview];
        // 释放视频对象
        [movie release];
        movie = nil ;
    }
    
    
    /*
     @method 当视频播放完毕释放对象
     */
    -(void)myMovieFinishedCallback:(NSNotification*)notify
    {
        
        NSNumber *reason =
        [notify.userInfo
         valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
        if (reason != nil){
            NSInteger reasonAsInteger = [reason integerValue];
            switch (reasonAsInteger){
                case MPMovieFinishReasonPlaybackEnded:{
                    /* The movie ended normally */
                    break; }
                case MPMovieFinishReasonPlaybackError:{
                    /* An error happened and the movie ended */
                    break;
                }
                case MPMovieFinishReasonUserExited:{
                    /* The user exited the player */
                    break;
                }
            }
            NSLog(@"Finish Reason = %ld", (long)reasonAsInteger);
        }
        
        /* 取消视频自动销毁  由break mv 执行
        //视频播放对象
        MPMoviePlayerController* theMovie = [notify object];
        //销毁播放通知
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:theMovie];
        [theMovie.view removeFromSuperview];
        // 释放视频对象
        [theMovie release];
        movie = nil ;
        NSLog(@"---------PlayVideoOver 11");
         */
        
        UnitySendMessage("_IOSDoor","PlayVideoOver", "");
        NSLog(@"---------PlayVideoOver 22");
    }
  • 相关阅读:
    Asp.net MVC Web.config配置技巧
    MySQL删除表的三种方式
    MySQL中count(字段) ,count(主键 id) ,count(1)和count(*)的区别
    Centos7部署k8s集群及应用
    composer更新指定包||composer 常用命令
    LVS负载均衡策略的部署与应用
    MySQL复制表的三种方式
    Centos7部署Nginx负载均衡Tomcat服务器及session共享架构
    企业级Nginx负载均衡与keepalived高可用实战视频教程
    CentOS下用于查看系统当前登录用户信息的4种方法
  • 原文地址:https://www.cnblogs.com/didiaodexi/p/3732066.html
Copyright © 2011-2022 走看看