mp3 works in browser but not in final APK

  • Replies:9
  • Answered
Justin D
  • Forum posts: 7

Feb 2, 2012, 11:16:43 PM via Website

I'm working on a fairly simple web based app. Basic functionality is push a button and hear a sound. It's all done in HTML and JavaScript and then built using Phonegap Build. It works perfectly in Android's browser. But when packaged into an APK and installed on the phone, the sounds don't play.

I'm still fairly new to Android development, so I'm just looking for some direction. I downloaded a logcat viewer, found where the mp3 files are being loaded up, but nothing stands out as an error or problem. Here's the basic JS code that should play the sound.

var sounds = new Array();
sounds[0] = "0.mp3";
sounds[1] = "1.mp3";
sounds[2] = "2.mp3";
sounds[3] = "3.mp3";

function PlaySound() {
randNum = Math.floor(Math.random()*4);
var sound = new Audio(sounds[randNum]);
sound.play();
}

The PlaySound function is called when the button is tapped. I've done console logs to ensure the function is called properly.

Not sure the best method to go about debugging this. Any help would be great.

Thanks,

Justin

Reply
Eric McBride
  • Forum posts: 1,790

Feb 3, 2012, 1:14:09 PM via Website

I hope there are a few devs around who can help. Have you also posted this problem over at XDA Developers?

Reply
Justin D
  • Forum posts: 7

Feb 3, 2012, 8:42:16 PM via Website

Well, through some trial and error I found part of the problem. If I have it pull the audio file from my webserver, it works fine. But when I try to package the audio file with app, it fails to play. The only thing I see in the logs to give any clue, is an entry regarding the mp3 file that says "isProtectedFile", but I really don't know exactly what that means. I'm guessing when it's trying to pull the file from local storage it's somehow locked/protected. Obviously that's not an issue if I'm pulling it from a remote server. Like I said, still new to Android development, I'm learning.

So anyway, how do I get around that file being inaccessible when packaged locally?

Reply
Justin D
  • Forum posts: 7

Feb 3, 2012, 9:55:56 PM via Website

here's the log entry I was referring to:

PlayerDriver::isProtectedFile(file:///android_asset/www/01.mp3)

Reply
Justin D
  • Forum posts: 7

Feb 6, 2012, 5:46:22 PM via Website

Still tryin to figure this one out. I compared the logs between 2 versions of my app. One version that pulls the MP3 from the web at runtime, and the other that has the file stored locally. Just like before, the web version works and the local version does not.

I did find quite a few differences:

They both start off with the ANDROID_DRM_TEST. As far as I can tell, they both pass. It's a file of my own creation and has no protection on it.

That's the only similarity though, it's all different after that.

The local file version (the one that does NOT work) has log entries referring PVPLAYER and PLAYERDRIVER. They're color coded RED and YELLOW which I'm assuming refers to ERROR and WARNING.

The web file version (the one that DOES work) has entries referring to STAGEFRIGHTPLAYER and AWESOMEPLAYER. It then goes on with a few lines about OMXCodec which just appears to be allocating buffers.

So it looks to me like it's using a completely different player depending on where the file is being pulled from. Again, I'm an Android newbie, so I may be completely wrong.

I'm not sure if there's a way I can force it to use a particular player even when playing a local file.

Anyhow, that's where I'm at. Thought I'd post an update and see if that gives anyone else any ideas for a solution. I'll keep plugging away at it myself as well.

Reply
Eric McBride
  • Forum posts: 1,790

Feb 7, 2012, 12:23:15 PM via Website

Keep us updated on this. Its always nice to see how devs find ways around problems.

Reply
Justin D
  • Forum posts: 7

Feb 7, 2012, 9:13:33 PM via Website

Still no luck, but here's the errors/warnings from my log file if that helps shed any light on the problem(s)

http://pastebin.com/isS542RE

Reply
Justin D
  • Forum posts: 7

Feb 10, 2012, 4:47:44 PM via Website

I have it working using Phonegap's Media class. The proper way to access it through the media object is like this:
myMedia = new Media("/android_asset/www/test.mp3");

You have to put the full path as the media class defaults to the /SDCARD directory on relative paths.

Simon MacDonald has a great write up about using the media class:
http://simonmacdonald.blogspot.com/2011/05/using-media-class-in-phonegap.html?m=1

As for the HTML5 method and using the AUDIO tag, I'm still not certain why it fails on local files. Phonegap's media class works just fine though and is probably a more reliable solution anyhow. Phonegap FTW!

Reply
Eric McBride
  • Forum posts: 1,790

Feb 10, 2012, 5:12:13 PM via Website

Epic! Progress! Glad to hear your making ground!

Reply