8

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.

Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
  • possible duplicate of [Video/audio streaming does not stop even if UIWebView is closed - iPad](http://stackoverflow.com/questions/2719270/video-audio-streaming-does-not-stop-even-if-uiwebview-is-closed-ipad) – nielsbot Aug 17 '12 at 17:41

2 Answers2

5

Looks like a dupe of Video/audio streaming does not stop even if UIWebView is closed - iPad

The solution there:

[self.webContent loadRequest:NSURLRequestFromString(@"about:blank")];
Community
  • 1
  • 1
nielsbot
  • 15,922
  • 4
  • 48
  • 73
1

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.

iceydee
  • 1,009
  • 8
  • 10
  • @iceydee: I will try that and get back to you asap. – Ahmad Kayyali May 09 '11 at 12:59
  • @iceydee: Thank you for the answer, it's not what I expected though. I will wait for a while and see if anyone can help me with the `UIWebView` issue if no one does I will accept your answer and use your solution, Thanks. – Ahmad Kayyali May 09 '11 at 13:53
  • Do you actually want the play/pause controls to be shown to the user? – iceydee May 09 '11 at 14:07
  • @iceydee : No At all I want to do is to stop the music when the user click done "like done listening to the music" or when I want to move to another View. – Ahmad Kayyali May 09 '11 at 14:09
  • @AhmadTK but the snippet I gave you, doesn't it fulfill the purpose? Just call [audioPlayer stop] when the user clicks the button. If you want the track to be looping forever you can change numberOfLoops to -1 I believe it is. I'd put the audioPlayer in a property so it is easily accessible from other classes/views. – iceydee May 09 '11 at 19:19
  • @iceydee: it full fill the purpose **BUT** as I told you before my issue was loading mp3 audio files into a `UIWebView` and I really want to keep my current app design as it is, in a couple of days if no one showed up with a solution for the `UIWebView` I will Accept your answer. – Ahmad Kayyali May 09 '11 at 19:35
  • @AhmadTK I think your best shot then is to use the HTML5 – iceydee May 09 '11 at 19:44
  • @iceydee: Thank you very much for the suggestion i am not at the office right now I will try it first thing in the morning : ) – Ahmad Kayyali May 09 '11 at 20:13