views:

1622

answers:

5

Hi

I see this issue only on the iPad. The same things works as expected on the iPhone.

I am opening the URL from my application in a UIWebView. If the URL is a normal web page, it works fine as expected. But if the URL is that of a remote video/audio file, the UIWebView opens the default player which is again good.

Now when I dismiss the UIWebView (by clicking on the Done button on the player), the streaming doesn't stop and the audio/video keeps playing in the background (I cannot see it but it does keep playing in the background, can hear it). The UIViewController in which the webview was created is also dealloced (I put in a log statement in the dealloc method) but the streaming doesn't stop.

Can someone please help me out on why this could be happening? And how can I stop the audio/video streaming when the UIWebView is closed?

Thanks.

+1  A: 

I'm working on the same problem. I've found that if you define something like this in your script tag:

function stopVideo(){ video.pause(); }
window.onunload = stopVideo;

Then in your UIViewController, add in:

-(void)viewWillDisappear:(BOOL)animated{
    [webView stringByEvaluatingJavaScriptFromString:@"window.onunload();"];
    [super viewWillDisappear:animated];
}

It seems to try to pause/stop the video for several seconds, but then you hear the audio continue to play!


Update!

This is a general bug with the media player. You have to set the playback time to -1 in order to make it really stop.

tolar
the playback time to -1 for what? The video, the webview, somewhere in the settings? appreciate the help
lostInTransit
On your HTML5 video object, simply set currentTime to -1 via javascript, ala:function stopVideo(){ video = document.getElementById('myVideo'); video.pause(); video.currentTime = -1;}
tolar
Thanks, works great where I embed the videos in webviews. But in case I open a video's URL in webview, how can I ensure this works as well? (I just create an NSURLRequest with the URL of the video)
lostInTransit
A: 

Hello, I have the same issue, with no solution.... In your previous post you mentioned that you can set the playback time to -1 to solve this. But how? Are we talking about MPMoviePlayerController? Or what do you call "media player"? Thanks for any suggestions!

Levend
+6  A: 

I have the same issue as stated but in this case the video that won't stop playing is a Youtube video embeded using the object/embed method.

I spent a long time trying to figure out how to get the video to stop playing and the only solution I found was to tell the UIWebView to load a blank page before dismissing the view:

    [self.webContent loadRequest:NSURLRequestFromString(@"about:blank")];
nivekastoreth
thats a workaround albeit a dirty one! Thanks for sharing. Let us know if you find anything else.
lostInTransit
I opened google.com ;)
Madhup
I've modified my original post to show what I'm actually doing. I think I like the "about:blank" approach since it doesn't involve yet more network overhead (especially if it starts trying to fetch resources).
nivekastoreth
A: 

Hey folks, thanks to your thread on this issue I was able to figure out a solution that resolves the issue completely, and I tested it on my IPad, not just the simulator. This also resolves the issue with the audio playing. This is not the permanent resolution but an effective work around.

Overview of the approach:

Basically all that is needed is to send a short audio file to the webView. I made a copy of the IMac submarine.m4v file, I called ping.m4v and added it to my xcode project in the resource folder.

Then at the point when I want the video / audio to stop I do the following steps:

webView.hidden = TRUE; NSString *mimeType = [[NSString alloc] initWithString:@"video/x-m4v"]; NSData *PingData = [NSData alloc]; PingData = [NSData dataWithContentsOfFile:PingFilePath];

[webView loadData:PingData MIMEType:mimeType textEncodingName:nil baseURL:[NSURL URLWithString:PingFilePath]];

Now you also have to handle error "204". Error 204 seems to be just a warning saying that webView is going to handle this audio file. To handle the error I added this single line (see >>>) to "didFailLoadWithError"

(void)webView:(UIWebView *)BHwebView didFailLoadWithError:(NSError *)error {

NSLog(@"Error %i", error.code);
if (error.code == NSURLErrorCancelled) return; // this is Error -999    
    if (error.code == 204) return; // this is Error 204 - for audio player in webview.

Finally I turn webView.hidden = FALSE right before I display my next Audio/Video. This way the little play bar for the sound file does not show on the display. There is just a soft ping sound... any audio file will do...

I hope this helps...

Steve S.

Steve S.
A: 

I've just had an issues with an mp4 file opening automatically in media player from a web-page without possibility to close it - "Done" button was only seen in video fullscreen mode and just closed fullscreen, so I had no way to return to the webpage without adding a "Back" button to the navigation bar. (on iPhone the video was opening in a separate view with "Done" button taking back to the WebView with the source page)

The workaround that helped me is to open the video file in a separate media player.

catch opening an MP4 file

- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
    NSRange range = [request.URL.absoluteString rangeOfString: @".mp4" options: NSCaseInsensitiveSearch];

    if ( range.location != NSNotFound ) //opening MP4 video file
    {           
        [self showFullscreenMediaWithURL: request.URL];

        return NO;
    }

    return YES;
} 

where

- (void) showFullscreenMediaWithURL: (NSURL *) mediaURL
{
    MPMoviePlayerViewController *ctrl = [[MPMoviePlayerViewController alloc] initWithContentURL: mediaURL];

    ctrl.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController: ctrl animated: YES];

    [ctrl release];
}

opens video with URL in a media player in a model view

don't forget to add MediaPlayer.framework to the project and import

#import <MediaPlayer/MediaPlayer.h>

for the project to be built

PS. many thanks to Viktor Gubrienko for the solution

Kostiantyn Sokolinskyi