Sign in to follow this  

Playing music from the assets folder

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

If you intended to correct an error in the post then please contact us.

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]

[CODE]
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;
}
[/CODE]

Share this post


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

Share this post


Link to post
Share on other sites
[s]You're missing an [b]s[/b] 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: [url="http://stackoverflow.com/questions/13638892/where-is-the-path-file-android-asset-documented"]I asked about this path on StackOverflow[/url] (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 this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='3DModelerMan' timestamp='1354285653' post='5005698']
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.
[/quote]
I [i]think [/i]that the [i]android_asset[/i] and [i]android_res[/i] 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 this post


Link to post
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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this