博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
视频连续播放
阅读量:6817 次
发布时间:2019-06-26

本文共 4368 字,大约阅读时间需要 14 分钟。

查了下文档,发现了这个东东MPMoviePlayerPlaybackDidFinishReasonUserInfoKey;

可以通过这样    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason intValue]) {
        case MPMovieFinishReasonPlaybackEnded:
            NSLog(@"playbackFinished. Reason: Playback Ended");         
            break;
        case MPMovieFinishReasonPlaybackError:
            NSLog(@"playbackFinished. Reason: Playback Error");
            break;
        case MPMovieFinishReasonUserExited:
            NSLog(@"playbackFinished. Reason: User Exited");
            break;
        default:
            break;
    }进行不同的处理,谢谢大家的热情帮助了。。

 

答:

使用MPMoviePlayerController 

把它的view加到自己的view上。
增加一个播放结束的通知
播放完时,把播放器再用网络的url初始化,继续播放
理论上这么做是可以的

 

问:

不行,先不说别的,那个播放结束的通知有两种情况会调用,一是在播放完成后自动调用,二是在用户手动点完成的时候会调用,如果是第一种情况,还可以在播放结束的回调函数里在加载另外一个视频,但如果是手动点完成的话,这个视频就应该结束了,还怎么再去播放视频呢?所以就不能再这个回调函数里面再加载视频了,头大阿头大,还有木有高手给点意见??

 

答:

你说地对,在点击完成button的时候,也会调用DidFinish函数

可以有种方法,可能比较麻烦,走弯路了,其他地方法没发现
检查当前正在播放的视频播放进度和总进度,如果一致证明是视频播放完时调用地,这时再检查当前是否是在播放广告,如果是,则继续播放视频,如果不是,说明视频已播放完,可调用dismiss函数。反之,则是用户点了完成按钮,应该dismiss窗口。
你觉得怎样?

 

问:

你好,我的问题现在是这样的,就是我要先播放一段我的本地视频(就是加广告),本地视频完了之后,再播放网络的视频。

但是,问题是本地视频的播放很不稳定,给我的感觉是当网络慢时,可以正常加载本地视频,紧接着播放网络视频;当网络快时,
本地视频就播放一瞬间,更为糟糕的是整个播放器都退出来了,连网络视频都播放不了了。以下是我的代码:
- (void)myMovieFinishedCallback:(NSNotification*)aNotification{
    
    NSDictionary *userInfo = [aNotification userInfo];
    NSLog(@"MPMoviePlayerPlaybackDidFinishReasonUserInfoKey: %@",
          [userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]);
    
    NSNumber* reason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason intValue]) {
        case MPMovieFinishReasonPlaybackEnded:
            NSLog(@"playbackFinished. Reason: Playback Ended");         
            break;
        case MPMovieFinishReasonPlaybackError:
            NSLog(@"playbackFinished. Reason: Playback Error");
            break;
        case MPMovieFinishReasonUserExited:
            NSLog(@"playbackFinished. Reason: User Exited");
            break;
        default:
            break;
    }
    
    MPMoviePlayerController* theMovie=[aNotification object];    
    [[NSNotificationCenter defaultCenter] removeObserver:self                                                    
                                                    name:MPMoviePlayerPlaybackDidFinishNotification                                                  
                                                  object:theMovie];    
    
    [theMovie release];
}  
- (void)playMovieAtURL:(NSURL*)theURL{  
    
    MPMoviePlayerController* theMovie= [[MPMoviePlayerController alloc] initWithContentURL:theURL];    
    theMovie.scalingMode=MPMovieScalingModeAspectFill;    
       
    // Register for the playback finished notification.    
    [[NSNotificationCenter defaultCenter] addObserver:self                                            
                                             selector:@selector(myMovieFinishedCallback:)                                                
                                                 name:MPMoviePlayerPlaybackDidFinishNotification                                              
                                               object:theMovie];     
    // Movie playback is asynchronous, so this method returns immediately.    
    [theMovie play];
- (void)video_play:(NSString*)filename{    
    NSString* s = [[NSBundle mainBundle] pathForResource:filename ofType:@"mp4"];    
    NSURL* url = [NSURL fileURLWithPath:s];    
    NSLog(@"Playing URL: %@", url);    
    [self playMovieAtURL:url];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"进入,%d,%@",navigationType,[[request URL]absoluteURL]);
    
    [self video_play:@"Hy_PLAY"];//Hy_PLAY.mp4是我的本地视频
    return YES;
}
  所以,我想问你下,怎么来控制本地视频,让它完整播放完之后,紧接着播放网络视频,而不会出现上面的问题,谢谢!

 

 

 

答:

你说的那种情况我到是没遇到,看了你的代码,感觉有些地方得指出来:

首先,将你在WebView的委托方法里面的 return YES 改成 return NO, 因为你是自己写的MpmoviePlayerController,若是是YES的话,系统会默认用自带的播放器进行播放,其次将你的MpMoviePlayer设置成全局的,在结束通知的方法里面这这样写
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason intValue]) {
        case MPMovieFinishReasonPlaybackEnded:{
            NSLog(@"playbackFinished. Reason: Playback Ended");
            if (Firstplay) {
                NSLog(@"11111111");
                [pWebVIew HidePlayingStatusView];
                if ([self IsDeviceIphone4OrHigher])
                {
                    [self ClearIphone4Setting];
                }
                
                //每次放完都release掉
                if ( nil != moviePlayer )
                {
                    [moviePlayer release];
                    moviePlayer = nil;        
                }
            }else {
                NSLog(@"222222222");
                [moviePlayer setContentURL:self.url];
                [moviePlayer play];
                Firstplay = TRUE;
            }
            
            
        }
            break;
        case MPMovieFinishReasonPlaybackError:{
            NSLog(@"playbackFinished. Reason: Playback Error");
            
        }
            break;
        case MPMovieFinishReasonUserExited:
            NSLog(@"playbackFinished. Reason: User Exited");
            [pWebVIew HidePlayingStatusView];
            
            if ([self IsDeviceIphone4OrHigher])
            {
                [self ClearIphone4Setting];
            }
            
            //每次放完都release掉
            if ( nil != moviePlayer )
            {
                [moviePlayer release];
                moviePlayer = nil;        
            }
            break;
        default:
            break;
    }
    
}

转载于:https://www.cnblogs.com/pengyingh/articles/2417906.html

你可能感兴趣的文章
Drupal采集网站需要用到的模块
查看>>
“亲切照料”下的领域驱动设计
查看>>
全面异步化:淘宝反应式架构升级探索
查看>>
90 后 CTO 创业 6 年,做了一件改变互联网的“小事”
查看>>
sequelize入门
查看>>
ThinkJS 2.0发布 JavaScript全栈开发框架
查看>>
独家!阿里开源自用OpenJDK版本,Java社区迎来中国力量
查看>>
Spring Web Services 3.0.4.RELEASE和2.4.3.RELEASE发布
查看>>
调查:Android的领先地位稳固
查看>>
专访阿里巴巴研究员赵海平:从Facebook到阿里巴巴
查看>>
处理团队中的消极情绪
查看>>
深入浅出Tensorflow(三):训练神经网络模型的常用方法
查看>>
区块链编程完全指南
查看>>
构建一个运行在Azure虚拟机上的MySQL Spring Boot应用程序
查看>>
英特尔披露新的预测执行漏洞L1终端故障
查看>>
Vue.js表单控件绑定
查看>>
如何撰写好文档?精益文档的六个实践
查看>>
魔鬼存在于细节中:从Redshift迁移到ClickHouse后再无数据丢失
查看>>
上学还是坐牢?百年老校“监控”学生惹争议
查看>>
作者访谈录——关于战略、领导力以及灵魂
查看>>