Playing local audio file in Android webview app

10,927

Solution 1

I have it working using Phonegap's Media class. I must have been doing something wrong initially, but I'm not sure what. The proper way to access it through the media object is as Simon stated previously: 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 also 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!

Solution 2

Have you tried pointing the file to the local file system path using "file:///android_assets/01.mp3" assuming your .mp3 is within the applications assets directory?

Solution 3

We had the same problem, the media files, in order to be played, needs to be in the external storage dir. Try to move your HTML and related files to /mnt/sdcard/.

Share:
10,927
Justin
Author by

Justin

Updated on June 04, 2022

Comments

  • Justin
    Justin almost 2 years

    I have a simple Android webview app, built with Phonegap Build. When trying to play a local mp3 file that's included in the APK, nothing happens. However, if I pull an mp3 from the web it works.

    e.g.

    <audio src="www.example.com/01.mp3" > WORKS JUST FINE
    
    <audio src="01.mp3" > DOES NOT WORK
    

    I'm new to Android development, so I can only guess that the file is somehow inaccessible by Android's media player. Here's a link to the errors/warnings from my log file if that helps shed any light on the problem.

    http://pastebin.com/isS542RE

  • Justin
    Justin about 12 years
    Yes. It's actually in file:///android_assets/www/01.mp3. I get the same result. The image files in that same location display just fine, but they are rendered directly via webview. My guess(and it's just that "a guess") is that since audio files are not played directly by a browser, but passed off to a player, that the player does not have access to files that are part of the app.
  • Devgeeks
    Devgeeks about 12 years
    Something else to note, html5 audio is notoriously flakey in Android. brianhadaway.com/html5-audio-support-on-android-devices If you are using PhoneGap you should probably consider using PhoneGap's Media API instead.
  • Justin
    Justin about 12 years
    I had the same issues with Phonegap's API, web files worked, local files did not. Thanks for the link though...it looks like Brian has built a nice simple player with Flash fallback. Most players out there are too bloated for what I need. Gonna give his a try.
  • Simon MacDonald
    Simon MacDonald about 12 years
    The proper way to new up a Media object for a file in the assets/www directory on PhoneGap Android is myMedia = new Media("/android_asset/www/test.mp3");
  • Justin
    Justin about 12 years
    I tried Phonegap's Media API one more time, just to be certain I wasn't missing something. Same results. Works fine when pulling a file from the web, but fails on local files. Even if I put the absolute path as Simon suggested.
  • Justin
    Justin about 12 years
    Also tried Brian Hadaway's player. Works great in the browser but also fails inside webview trying to pull local files.
  • Cairo
    Cairo almost 11 years
    I had the same problem, and moved the file to the sdcard, and now it works. Thanks dude.