Fork me on GitHub

视频连续播放

查了下文档,发现了这个东东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;
    }
    

}

posted on 2012-03-26 15:00  pengyingh  阅读(1376)  评论(0)    收藏  举报

导航