2012-01-04 100 views
0

使用网络应用程序和手机播放视频时出现问题。我的网络应用程序中有一个链接直接链接到mp4文件。我在phonegap的AppDelegate类中为这种类型的URL添加了一个处理程序。PhoneGap应用程序在背景上播放视频(隐藏)

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest: 
(NSURLRequest *)request navigationType: 
(UIWebViewNavigationType)navigationType 
{ 
    NSURL *url = [request URL]; 
    NSLog(@"redirect detected"); 
    if ([[[url path] pathExtension] isEqualToString:@"mp4" ] || [[[url path] pathExtension] isEqualToString:@"m4v" ] || [[[url path] pathExtension] isEqualToString:@"m3u8" ]) { 
     //video files are .mp4 or m4v, stream indexes are m3u8 
     //it's a movie, go play! 
     NSLog(@"Movie detected - playing"); 
     NSLog([url path]); 
     [self playMovieAtURL:url]; 
     return NO; 
    } else { 
     return [ super webView:theWebView shouldStartLoadWithRequest:request 
       navigationType:navigationType ]; 
    } 
} 

这工作正常,电影播放的方法被称为。该方法是这样实现的(1):

-(void) playMovieAtURL: (NSURL*) theURL { 
    NSLog(@"PlayMovieAtUrl"); 
    MPMoviePlayerController* theMovie = 
    [[MPMoviePlayerController alloc] initWithContentURL: theURL]; 
    [self presentMoviePlayerViewControllerAnimated:theMovie]; 
} 

也试过这种(2):

-(void) playMovieAtURL: (NSURL*) theURL { 
    NSLog(@"PlayMovieAtUrl"); 
    MPMoviePlayerController* theMovie = 
    [[MPMoviePlayerController alloc] initWithContentURL: theURL]; 

    [theMovie setControlStyle:MPMovieControlStyleFullscreen]; 
    [theMovie setFullscreen:YES]; 


    // Register for the playback finished notification 
    [[NSNotificationCenter defaultCenter] 
    addObserver: self 
    selector: @selector(myMovieFinishedCallback:) 
    name: MPMoviePlayerPlaybackDidFinishNotification 
    object: theMovie]; 
    NSLog(@"About to play"); 
    // Movie playback is asynchronous, so this method returns immediately. 
    [theMovie play]; 
} 

但在这两种情况下是创建的球员,它起着。唯一的问题是,它是隐藏的。声音可以被听到,但没有图片。 webView仍然显示。 (1)应该在没有基于其他地方的职位的任何设置的情况下工作。问题(我猜)是,它在AppDelegate类中调用,而不是在Controller类中调用。但我不知道如何实现这个使用phonegap :(任何想法?

+0

如果您需要更多信息,请让我知道 – 2012-01-04 15:25:41

回答

0

我设法解决这个问题是,我无法访问WebView的ViewController但我发现https://gist.github.com/527494作者使用模态当我有ViewController - 这很容易:)

MPMoviePlayerViewController* theMovie = 
    [[MPMoviePlayerViewController alloc] initWithContentURL: theURL]; 
    [super.viewController presentMoviePlayerViewControllerAnimated:theMovie]; 

我希望能帮助别人。我花了整整一天的时间来解决它;)