1

I am trying to send the path of a LOCAL mp3 file to javascript and from there creating an audio tag populated with the source. When I try this however, I am presented with a "cannot play audio file" message. The file itself is coming from the app specific sandbox so the path looks something like this

/Users/username/Library/Application Support/iPhone Simulator/6.0/Applications/818263D7-4246-4DEC-9186-6AC14F0175A3/Documents/audiofile.mp3

The problem is that I can play an mp3 from my desktop using the same method, just not one that is stored in the sandbox or local filesystem for the application itself. Here is the code I am using to get the path and send it to javascript through the webview. Any help would be greatly appreciated.

UIWebView *webContainer = [[UIWebView alloc] initWithFrame: self.view.bounds];
webContainer.backgroundColor = [UIColor grayColor];
webContainer.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
[webContainer setBackgroundColor:[UIColor clearColor]];
webContainer.delegate = self;
[self.view addSubview:webContainer];

NSString *imagePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,         NSUserDomainMask, YES) objectAtIndex:0];

NSString *imPath = [NSString stringWithFormat:@"%@/angels.mp3",imagePath];
imPath = [imPath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

[webContainer stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"setupAudio('%@');", imPath]];
Tieme
  • 62,602
  • 20
  • 102
  • 156
Duran
  • 38
  • 6
  • 1
    How is `setupAudio()` working here; where does it come from, and what is it meant to achieve? Have you had a look at the resulting string when evaluating the JS, is it what you expect? – WDUK Nov 19 '12 at 23:18
  • the setupAudio function is pretty simple it builds the audio tag markup and adds it to the dom using jquery it looks like this: `function setupAudio($path){ var tx = ''; $('.ac').append(tx); }` – Duran Nov 20 '12 at 00:24

2 Answers2

1

If your path ends with 'audiofile.mp3' I don't think the file 'angels.mp3' will play.

But you could just use the MPMoviePlayerController to play the file:

NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,             NSUserDomainMask, YES) objectAtIndex:0];

NSString *audioPath = [NSString stringWithFormat:@"%@/angels.mp3",docPath];

MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: audioPath];

[moviePlayer.view  setFrame:self.view.bounds]; 

//add it to your view
[viewController.view addSubview:moviePlayer.view];

//if you don't want any controls
[moviePlayer setControlStyle:MPMovieControlStyleNone];

//play!
[moviePlayer prepareToPlay];

Modified source from some example.

Some other handy related question on this topic can be found here.

Community
  • 1
  • 1
Tieme
  • 62,602
  • 20
  • 102
  • 156
  • `initWithContentURL` requires an NSURL object, not an NSString – WDUK Nov 19 '12 at 23:07
  • Also, `[moviePlayer.view setFrame:CGRectMake()]` should really be `[moviePlayer.view setFrame:self.view.bounds]` for this example, to emulate the `UIWebView`. – WDUK Nov 19 '12 at 23:13
  • This could be a good backup, but I unfortunately need to try and get the file to play from the html itself. The reason is based on how the app itself was originally created. I tried your solution and it did work so maybe I will push back a bit and see if I can play the audio using objective-c. – Duran Nov 20 '12 at 00:38
  • @WDUK, correct! i updated the answer. Allright, good luck Duran! – Tieme Nov 20 '12 at 08:43
1

If you must use a UIWebView, and you only want to use the audio file, you can load it directly using a URL Request (Naturally, make sure the file exists at that URL!).

NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *audioPath = [NSString stringWithFormat:@"%@/angels.mp3",docPath];

NSURL *url = [NSURL fileURLWithPath:audioPath isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

[webContainer loadRequest:request];
WDUK
  • 18,870
  • 3
  • 64
  • 72
  • I tried this and it didn't work. I will keep working at it though and see if I was just doing something wrong. The problem is that I can't have the page reload when I load new data. I just need the audio to play without replacing or removing other content (if that makes sense). So far whenever i try to use loadRequest with a UIWebView, the page reloads and my dynamically placed data goes away. – Duran Nov 20 '12 at 00:40