Jump to content

  • Log In with Google      Sign In   
  • Create Account

Playing music from the assets folder


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 3DModelerMan   Members   -  Reputation: 1001

Like
0Likes
Like

Posted 28 November 2012 - 03:06 PM

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?

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;
}


Sponsor:

#2 3DModelerMan   Members   -  Reputation: 1001

Like
0Likes
Like

Posted 29 November 2012 - 07:56 PM

Anyone?

#3 frob   Moderators   -  Reputation: 21115

Like
1Likes
Like

Posted 29 November 2012 - 08:23 PM

Why are you building the resource url yourself? Is there some reason you aren't using ClassLoader.getResource(filepath) ?
Check out my personal indie blog at bryanwagstaff.com.

#4 Cornstalks   Crossbones+   -  Reputation: 6985

Like
1Likes
Like

Posted 29 November 2012 - 10:04 PM

You're missing an s on your path, I believe. Try "file:///android_assets/". 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, 29 November 2012 - 10:47 PM.

[ 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 ]

#5 3DModelerMan   Members   -  Reputation: 1001

Like
0Likes
Like

Posted 30 November 2012 - 08:27 AM

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.

#6 Cornstalks   Crossbones+   -  Reputation: 6985

Like
0Likes
Like

Posted 30 November 2012 - 09:50 AM

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)).
[ 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 ]

#7 3DModelerMan   Members   -  Reputation: 1001

Like
0Likes
Like

Posted 30 November 2012 - 12:24 PM

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

#8 Jan Olex   Members   -  Reputation: 101

Like
0Likes
Like

Posted 13 January 2013 - 05:06 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS