Wow.. that IS weird.

I'm not sure what the problem is here, but there are a couple of
things I do differently:

1. Don't use MP3.  Apparently the MP3 decoder on Android is a bit
crufty.  I was advised to use the OGG format and it works well.  You
can create OGG files using Audacity (it's free) http://audacity.sourceforge.net/
2. I create MediaPlayer instances using a simpler method, specifically
the following:

A) ALL of my sound files are in resources/raw (not assets)
B) When I want to load a sound for the game it has it's own
MediaPlayer instance (same as you)
C) I create MediaPlayer instances using the following:

MediaPlayer.create(context, resourceId);

where resourceId is something like:

R.raw.bgmusic

This does all the data source creation etc for me, and once the call
is complete the sound is ready to play

I wonder if the way you are creating the datasource is somehow
creating a reference to the entire folder?

Also, I don't think you need to create the stream (assuming you don't
want to use the MediaPlayer.create() method I use).  According to the
doco the AssetFileDescriptor returned from openFd has a method called
getFileDescriptor()


On 23 Sep, 07:22, kk <kkostia...@gmail.com> wrote:
> Hi all,
>
> I'm having some trouble streaming a looping background track for my
> game using MediaPlayer.
>
> I have a bunch of short mp3s in /res/raw that I use as SFX and I play
> using SoundPool.
> The SFX playback all works fine...I'm mentioning it because it's
> relevant as you'll see below.
>
> The background music track (also an mp3) lives in /assets. The code I
> use to play this track is at the bottom
> of this post. Here's what happens:
>
> Instead of the track looping, once it has finished playing, it starts
> playing all the SFX from /res/raw in sequence!
> Then, once all these have finished playing it starts all over again,
> i.e. playing the background music track
> and then all the SFX in the same order.
>
> As if that wasn't weird enough, the behaviour described above only
> happens in the emulator.
> When tested on an HTC Tattoo and an HTC Desire it only does the above
> once. I.e. it plays the background music
> track, then all the SFX, and then it stops...it doesn't loop.
>
> Any help greatly appreciated since nobody here has any idea what's
> going wrong or how to fix it!
>
> This is developed using:
> SDK 1.6 API4 revision 3
> SDK Tools revision 6
>
> The code I use for the media player playback:
>
> =====
> // note: p_theSurfaceView below is my class that extends SurfaceView
> implements SurfaceHolder.Callback
> MediaPlayer m_mediaPlayer = new MediaPlayer();
> FileInputStream l_fis;
> try
> {
>     l_fis =
> p_theSurfaceView.getContext().getAssets().openFd("bgmusic.mp3").createInput 
> Stream();
>     m_mediaPlayer.setDataSource(l_fis.getFD());
>     // from the docs: It is the caller's responsibility to close the
> file descriptor.
>     // from the docs: It is safe to do so as soon as the setDataSource
> call returns.
>     l_fis.close();
>
>     m_mediaPlayer.prepare();
>
>     m_mediaPlayer.setLooping(true);
>     m_mediaPlayer.setOnErrorListener(
>             new MediaPlayer.OnErrorListener() {
>                 public boolean onError(MediaPlayer p_mp, int p_what,
> int p_extra)
>                 {
>                     Log.e("m_mediaPlayer", "ERROR: MediaPlayer: (" +
> p_what +") with extra (" + p_extra +")" );
>                     return false;
>                 }
>             });
>
>     m_mediaPlayer.start();}
>
> catch (IllegalArgumentException e)
> {
>     e.printStackTrace();
>     m_mediaPlayer = null;
>     return false;}
>
> catch (IllegalStateException e)
> {
>     e.printStackTrace();
>     m_mediaPlayer = null;
>     return false;}
>
> catch (IOException e)
> {
>     e.printStackTrace();
>     m_mediaPlayer = null;
>     return false;
>
> }
>
> return true;
> =====
>
> cheers,
> kk.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to