Playing music from the assets folder

This topic is 2076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm trying to stream music that's stored in my assets folder, but it never works. I've already verified that filepath contains the correct local path from the root of my assets folder. I need to use AsyncPlayer so that I can stream music instead of loading it into memory. I use ogg files. I attached the debug output from logcat as well. How do you get a URI to a file in the assets folder that can be used with AsyncPlayer?

[attachment=12510:Screenshot from 2012-11-28 13:04:21.png]

 public class MusicWrapper { public static void setContext(Context context) { m_context = context; m_player = new AsyncPlayer(null); } public static void playTrack(String filepath) { m_player.play(m_context, Uri.parse("file:///android_asset/"+filepath), true, AudioManager.STREAM_MUSIC); } private static AsyncPlayer m_player; private static Context m_context; } 

Anyone?

Share on other sites
Why are you building the resource url yourself? Is there some reason you aren't using ClassLoader.getResource(filepath) ?

Share on other sites
[s]You're missing an s on your path, I believe. Try "file:///android_assets/"[/s]. Nevermind. The Internets seem to be conflicting on this, with some sites saying "file:///android_asset/" and some saying "file:///android_assets/". So far, I've only seen WebView use that path, so it's possible the AsyncPlayer doesn't work with that path. [Edit again: I asked about this path on StackOverflow (there are more Android devs there), for the curious]

If that doesn't work, seriously consider using the MediaPlayer. AFAIK, it doesn't load the entire audio clip into memory, and can stream. Edited by Cornstalks

Share on other sites
I didn't know about ClassLoader.getResource. Thanks for the help. 'll just try both the extra s and without it and see what works.

Share on other sites

I didn't know about ClassLoader.getResource. Thanks for the help. 'll just try both the extra s and without it and see what works.

I think that the android_asset and android_res directories only work with WebView. I don't think you can use it with other objects (or at least I'm failing to find any example of it being possible). AsyncPlayer was intended for playing audio files from an external server. I still think you really aught to be using MediaPlayer (are you already using MediaPlayer? because I can't tell if those MediaPlayer log entries in your log you posted are from you or from AsyncPlayer (which may just be a wrapper for MediaPlayer)).

Share on other sites
AsyncPlayer is a wrapper for MediaPlayer. I'll just switch to media player since it supports streaming anyways.

Share on other sites

why use assets in first place? use raw and your media files will get indexed and then its strigtforward playback usin soundpool and mediaplayer

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
18
5. 5

• 9
• 9
• 33
• 13
• 13
• Forum Statistics

• Total Topics
632587
• Total Posts
3007224

×