PhoneGap app在背景上播放视频(隐藏)

时间:2012-01-04 12:57:35

标签: ios xcode video cordova

使用网络应用程序和phonegap播放视频时遇到问题。我的网络应用程序中有一个链接直接链接到mp4文件。我在phonegap的AppDelegate类中为这种类型的URL添加了一个处理程序。

- (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实现这个:(任何想法?

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。问题是,我无法访问WebView的ViewController。但我发现https://gist.github.com/527494作者使用带有phonegap的模态窗口。当我有ViewController时 - 很简单:)

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

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