Packing audio files into Android APKs

74 views
Skip to first unread message

Raju Bitter

unread,
Oct 13, 2010, 10:05:42 AM10/13/10
to phonegap
I'm testing the audio playback functionality on Android, and ran into
the following problem: What if I want to include the MP3 file in my
APK file instead of putting it onto the SD card? Has anyone tried
that?

Thanks,
Raju

Bryce Curtis

unread,
Oct 13, 2010, 3:04:43 PM10/13/10
to phonegap
Using the latest edge version of Android, you can specify an MP3 file
that is in the same directory as your index.html:

var media = new Media("/android_asset/www/music.mp3");
media.play();


On Oct 13, 9:05 am, Raju Bitter <r.bitter.mailingli...@googlemail.com>
wrote:

Raju Bitter

unread,
Oct 13, 2010, 6:41:42 PM10/13/10
to Bryce Curtis, phonegap
Thanks a lot, Bryce. I'll try that tomorrow.

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com

Raju Bitter

unread,
Oct 13, 2010, 8:24:54 PM10/13/10
to Bryce Curtis, phonegap
Tested with the latest edge version, and for both approaches (putting
the MP3 file onto the sdcard or packaging the MP3 into the APK, I
don't hear any audio playing. In logcat I can see the following
messages for resources on the sdcard:

I/System.out( 8674): PluginManager.exec(Media, startPlayingAudio,
Media2, ["50e32e7e-5ff6-071b-1ee0-53d15704c1d6","/resources/where_to_begin.mp3"],
true)
I/System.out( 8674): PluginManager.addPlugin(com.phonegap.AudioHandler)
I/StagefrightPlayer( 8618):
setDataSource('/sdcard//resources/where_to_begin.mp3')

And here's what I see when I'm trying to access APK package content
using "/android_asset":
I/System.out( 542): PluginManager.exec(Media, startPlayingAudio,
Media2, ["b3ec58be-055d-fa4f-cc0d-454d4ca6ebe1","/android_asset/www/resources/where_to_begin.mp3"],
true)
I/System.out( 542): PluginManager.addPlugin(com.phonegap.AudioHandler)
W/AudioFlinger( 59): write blocked for 197 msecs, 1 delayed writes,
thread 0xd7a8

I don't see any error messages, and the same MP3 file worked with
PhoneGap 0.9.1, when I had the file on the sdcard. Has anyone else
experienced the same problem?

Raju

Raju Bitter

unread,
Oct 13, 2010, 8:30:45 PM10/13/10
to Bryce Curtis, phonegap
I've tested on the Nexus One, Android 2.2, build FRF91.

On Thu, Oct 14, 2010 at 2:24 AM, Raju Bitter

Raju Bitter

unread,
Oct 13, 2010, 8:45:15 PM10/13/10
to Bryce Curtis, phonegap
For testing purposes I used a file name which doesn't exists, and in
that case I can see the FileNotFoundException as shown here:
I/System.out( 621): PluginManager.exec(Media, startPlayingAudio,
Media2, ["497e8185-3822-e4ec-f702-776dbd817b25","/android_asset/www/resources/Koto1.mp3"],
true)
I/System.out( 621): PluginManager.addPlugin(com.phonegap.AudioHandler)
W/System.err( 621): java.io.FileNotFoundException: www/resources/Koto1.mp3
W/System.err( 621): at
android.content.res.AssetManager.openAssetFd(Native Method)
W/System.err( 621): at
android.content.res.AssetManager.openFd(AssetManager.java:329)
W/System.err( 621): at
com.phonegap.AudioPlayer.startPlaying(AudioPlayer.java:192)
W/System.err( 621): at
com.phonegap.AudioHandler.startPlayingAudio(AudioHandler.java:155)
W/System.err( 621): at com.phonegap.AudioHandler.execute(AudioHandler.java:57)
W/System.err( 621): at
com.phonegap.api.PluginManager$1.run(PluginManager.java:82)
W/System.err( 621): at java.lang.Thread.run(Thread.java:1096)

On Thu, Oct 14, 2010 at 2:30 AM, Raju Bitter

Raju Bitter

unread,
Oct 14, 2010, 10:30:34 AM10/14/10
to Bryce Curtis, phonegap
The problem is that AudioPlayer#onPrepared(MediaPlayer) method is
never called, hence the playing is not started. That's is due to the
fact that - within the startPlaying method - call

this.mPlayer.prepare()

before you set the onPreparedListener for the MediaPlayer object:

this.mPlayer.setOnPreparedListener(this);

If you set the onPreparedListener before call this.mPlayer.prepare(),
everything works fine. Should I file a bug?

Raju


On Thu, Oct 14, 2010 at 2:45 AM, Raju Bitter

Bryce Curtis

unread,
Oct 14, 2010, 11:01:32 AM10/14/10
to phonegap
Raju,

I had the fix on my local copy, but forgot to update master. It
should be fixed now.

Thanks,
Bryce

On Oct 14, 9:30 am, Raju Bitter <r.bitter.mailingli...@googlemail.com>
wrote:
> The problem is that AudioPlayer#onPrepared(MediaPlayer) method is
> never called, hence the playing is not started. That's is due to the
> fact that - within the startPlaying method - call
>
> this.mPlayer.prepare()
>
> before you set the onPreparedListener for the MediaPlayer object:
>
> this.mPlayer.setOnPreparedListener(this);
>
> If you set the onPreparedListener before call this.mPlayer.prepare(),
> everything works fine. Should I file a bug?
>
> Raju    
>
> On Thu, Oct 14, 2010 at 2:45 AM, Raju Bitter
>

Raju Bitter

unread,
Oct 14, 2010, 11:27:25 AM10/14/10
to Bryce Curtis, phonegap
Thanks, Bryce. Appreciate your help.

Raju

Andrew Jones

unread,
Oct 14, 2010, 7:41:53 AM10/14/10
to phonegap
Hi Raju,

I was having the same problems, but it definitely works with the edge
version. Try building Bryce's demo..

http://github.com/brycecurtis/phonegap-demo

Put, for example, song.mp3 in the www folder and change the audio
source to "/android_asset/www/sound.mp3". (index.html line 421.) Works
for me on emulator and my Desire.


On Oct 14, 1:24 am, Raju Bitter <r.bitter.mailingli...@googlemail.com>
wrote:
> Tested with the latest edge version, and for both approaches (putting
> the MP3 file onto the sdcard or packaging the MP3 into the APK, I
> don't hear any audio playing. In logcat I can see the following
> messages for resources on the sdcard:
>
> I/System.out( 8674): PluginManager.exec(Media, startPlayingAudio,
> Media2, ["50e32e7e-5ff6-071b-1ee0-53d15704c1d6","/resources/where_to_begin.mp3"],
> true)
> I/System.out( 8674): PluginManager.addPlugin(com.phonegap.AudioHandler)
> I/StagefrightPlayer( 8618):
> setDataSource('/sdcard//resources/where_to_begin.mp3')
>
> And here's what I see when I'm trying to access APK package content
> using "/android_asset":
> I/System.out(  542): PluginManager.exec(Media, startPlayingAudio,
> Media2, ["b3ec58be-055d-fa4f-cc0d-454d4ca6ebe1","/android_asset/www/resources/where _to_begin.mp3"],
> true)
> I/System.out(  542): PluginManager.addPlugin(com.phonegap.AudioHandler)
> W/AudioFlinger(   59): write blocked for 197 msecs, 1 delayed writes,
> thread 0xd7a8
>
> I don't see any error messages, and the same MP3 file worked with
> PhoneGap 0.9.1, when I had the file on the sdcard. Has anyone else
> experienced the same problem?
>
> Raju
>
> On Thu, Oct 14, 2010 at 12:41 AM, Raju Bitter<r.bitter.mailingli...@googlemail.com> wrote:
> > Thanks a lot, Bryce. I'll try that tomorrow.
>

Andrew Jones

unread,
Oct 14, 2010, 7:07:22 AM10/14/10
to phonegap
Bryce, thanks very much for this. I stumbled across it whilst trying
to figure out how to play local audio.

Your Phonegap Demo is also much appreciated, it's a really good
reference to anyone using it. Saved me lots of hair. Thanks!

Cheers,
Andy

alarm

unread,
Dec 13, 2010, 5:28:56 AM12/13/10
to phonegap
But still.. is it possible to pack media files to APK so, that after
installation they will be automatically placed to SDcard? Or should I
implement initial move to SDCard in my application?

SDCard seems to be logical place for large audio files as phone memory
is restricted...

Thanks,
Alar

On 14 okt, 13:41, Andrew Jones <andyjones1...@gmail.com> wrote:
> Hi Raju,
>
> I was having the same problems, but it definitely works with the edge
> version. Try building Bryce's demo..
>
> http://github.com/brycecurtis/phonegap-demo
>
> Put, for example, song.mp3 in the www folder and change the audio
> source to "/android_asset/www/sound.mp3". (index.html line 421.) Works
> for me on emulator and my Desire.
>
> On Oct 14, 1:24 am, Raju Bitter <r.bitter.mailingli...@googlemail.com>
> wrote:
>
> > Tested with the latest edge version, and for both approaches (putting
> > the MP3 file onto thesdcardor packaging the MP3 into the APK, I
> > don't hear any audio playing. In logcat I can see the following
> > messages for resources on thesdcard:
>
> > I/System.out( 8674): PluginManager.exec(Media, startPlayingAudio,
> > Media2, ["50e32e7e-5ff6-071b-1ee0-53d15704c1d6","/resources/where_to_begin.mp3"],
> > true)
> > I/System.out( 8674): PluginManager.addPlugin(com.phonegap.AudioHandler)
> > I/StagefrightPlayer( 8618):
> > setDataSource('/sdcard//resources/where_to_begin.mp3')
>
> > And here's what I see when I'm trying to access APK package content
> > using "/android_asset":
> > I/System.out(  542): PluginManager.exec(Media, startPlayingAudio,
> > Media2, ["b3ec58be-055d-fa4f-cc0d-454d4ca6ebe1","/android_asset/www/resources/where _to_begin.mp3"],
> > true)
> > I/System.out(  542): PluginManager.addPlugin(com.phonegap.AudioHandler)
> > W/AudioFlinger(   59): write blocked for 197 msecs, 1 delayed writes,
> > thread 0xd7a8
>
> > I don't see any error messages, and the same MP3 file worked with
> > PhoneGap 0.9.1, when I had the file on thesdcard. Has anyone else
Reply all
Reply to author
Forward
0 new messages