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").createInputStream();
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en