Playing music from the assets folder

Started by
6 comments, last by Jan_Olex 11 years, 3 months ago
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;
}
Advertisement
Anyone?
Why are you building the resource url yourself? Is there some reason you aren't using ClassLoader.getResource(filepath) ?
[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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
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 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)).
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
AsyncPlayer is a wrapper for MediaPlayer. I'll just switch to media player since it supports streaming anyways.

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

This topic is closed to new replies.

Advertisement