Search code examples
objective-ciosipaduiwebviewmp3

How to Stop UIWebView from playing mp3?


I am loading a MP3 file into UIWebView control on an iPad Application, I have a UIButton called done the user expected to dismiss the UIWebView and stop the music, here is a code snippet:

// points to the mp3 file path
NSURL *url = [NSURL fileURLWithPath:self.sFilePath]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[web loadRequest:request];

When the user hit done I do the following:

[self.webview removeFromSuperview];
[self.webview release];

But that does not stop the music from playing, I noticed that loading mp3 files on UIWebView opens the QuickTime player is that correct way?

I am greatly appreciative of any guidance or help.


Solution

  • I suggest that you play the sound yourself using for example AVAudioPlayer (probably the easiest way to play and control sounds in iOS).

    Something like this:

    NSURL *url = [NSURL fileURLWithPath:self.sFilePath];
    NSData *mySound = [NSData dataWithContentsOfURL:url];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:mySound error:NULL];
    audioPlayer.numberOfLoops = 0;
    [audioPlayer play];
    

    You can then stop the sound as simple as:

    [audioPlayer stop];
    

    or

    [audioPlayer pause];
    

    Don't forget to include the AVFoundation.framework in your project.