MediaPlayer.start() doesnot work fine after calling MediaPlayer.seekTo(0)

by Sudha » Mon, 27 Apr 2009 16:34:59 GMT


Sponsored Links
 i,

I have a requirement to play several sounds many times in my game so
instead of creating the MediaPlayer again and again I have called
mp.seekTo(0) in onCompletion(mp) so that it will restart.
Sometimes the sound is not audible from the device when I call mp.start
() after setting mp.seekTo(0); but the player calls onCompletion()
without playing any sound, this is observed randomly on most of the
sounds

FYI:

- My sounds are of short duration mostly less than a second.
- I am using a separate MediaPlayer for each sound (as I need this)
- There are almost 28 sounds in my game so i will be creating 28
MediaPlayers.
- Below is the attached code for player

Also If I try to play many sounds one after the other in a short
period of time i get an error saying "no more track names available".
can u tell Why this is happening...?


check the below code:

public boolean isPlayingSound; //class member
MediaPlayer mp = null;
String last_req = "";
public void playSound(final String res) {
if (isPlayingSound){
return;
}
try {

if (last_req.equals(res))
{
System.out.println("starting again................:
"+res); //
Sudhaker
mp.start();
isPlayingSound = true;
return;
}
last_req = res;
mp = new MediaPlayer();

FileInputStream fIn = Utils.getContext().openFileInput(res);
if (fIn != null)
{
//we tell the mediaplayer which file he needs to play.
notice we
don't tell him if this is a MIDI, WAV, MP3 or even AVI for that
matter.
//why? he is smart enough to recognize them!! we don't
even have to
use valid extensions like say .mid, .mp3, .wav!!! ... WOAH!! =D
mp.setDataSource(fIn.getFD());
fIn.close();
}

mp.setOnPreparedListener(new
MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp)
{
mp.start();
isPlayingSound = true;
}
});

mp.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp)
{
System.out.println("sound playing finished :
"+res + " and seek to
start"); //Sudhaker
//mp.release();
mp.seekTo(0);
isPlayingSound = false;
}
});

mp.setOnErrorListener(new
MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int
extra)
{
mp.release();
mp = null;
deleteSoundFile(res);
isPlayingSound = false;
last_req="";
return false;



MediaPlayer.start() doesnot work fine after calling MediaPlayer.seekTo(0)

by niko20 » Tue, 28 Apr 2009 02:34:32 GMT


 ou have to call Stop() before you can call Seek(). Also you have lots
of MediaPlayers which is not a good idea.

I would upgrade to 1.5 cupcake SDK and use the AudioTrack classes (or
soundpool maybe)

-niko20

On Apr 27, 3:20am, Sudha <sudhaker...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


MediaPlayer.start() doesnot work fine after calling MediaPlayer.seekTo(0)

by BlackLight » Tue, 28 Apr 2009 02:50:35 GMT


  have same problem. I tried to seek()/start() and tried to create
players too. Some sounds are silent if play them often. Any idea?
Sometimes sound stuck at all. No sound anymore in active Activity, but
if I go to my program menu (back button) and lunch play activity again
- sounds are fine.

On Apr 27, 10:34pm, niko20 <nikolatesl...@yahoo.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



MediaPlayer.start() doesnot work fine after calling MediaPlayer.seekTo(0)

by BlackLight » Tue, 28 Apr 2009 03:11:44 GMT


 TW:
Seeks to specified time position. Call this after start(), pause(), or
prepare(), or after receiving onPrepared or onCompletion event
notification from OnPreparedListener or OnCompletionListener
interface.

No need to stop. Right?

On Apr 27, 10:34pm, niko20 <nikolatesl...@yahoo.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



MediaPlayer.start() doesnot work fine after calling MediaPlayer.seekTo(0)

by Dave Sparks » Tue, 28 Apr 2009 05:19:33 GMT


 ry this:

mp.prepare();
mp.seekTo(0);
mp.start();

And get rid of your onPreparedListener. It is unnecessary since you
are calling prepare().

On Apr 27, 1:20am, Sudha <sudhaker...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



MediaPlayer.start() doesnot work fine after calling MediaPlayer.seekTo(0)

by niko20 » Tue, 28 Apr 2009 11:03:29 GMT


 orry you are right, I meant you have to pause. Worth a try anyway..

On Apr 27, 4:19pm, Dave Sparks <davidspa...@android.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



MediaPlayer.start() doesnot work fine after calling MediaPlayer.seekTo(0)

by Sudha » Tue, 28 Apr 2009 20:11:11 GMT


 i,

for now i have found a work around

In onCompletionListener() am resetting the player and setting
DataSource and then calling prepare() then start() so that no sounds
are skipped.

mp.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp)
{
try
{
mp.reset();
FileInputStream fIn =
Utils.getContext().openFileInput(res);
if (fIn != null)
{
mp.setDataSource(fIn.getFD());
fIn.close();
}

isPlayingSound = false;
}
catch(Exception e){e.printStackTrace();}
}
});


But this way I am getting the below error but still the sounds are
being played, (i didn't observe any sound to be skipped).

---- IllegalStateException: setDataSource() called in state 2

I dont know why am getting this exception coz as per below link call
to setDataSource() can be followed by reset(), Can anybody please
clarify this exception.

As per
http://developer.android.com/reference/android/media/MediaPlayer.html#Valid_and_Invalid_States

I suppose We can also try:
mp.stop()
mp.prepare()
mp.seekTo(0)
mp.start()

i will try the above case, but want the answer to the
IllegalStateException..


On Apr 28, 8:03am, niko20 <nikolatesl...@yahoo.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



MediaPlayer.start() doesnot work fine after calling MediaPlayer.seekTo(0)

by Sudha » Tue, 28 Apr 2009 20:32:28 GMT


 ops I tried the below code:

mp.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp)
{
mp.stop();
isPlayingSound = false;
}
});

then when the same sound is requested again.... called

mp.prepare();
mp.seekTo(0);
mp.start();

Even in this way most of the sounds are not played and am getting an
exception for prepare

E/MidiFile( 31): EAS_Prepare failed: [-31]
W/System.err( 4133): java.io.IOException: Prepare failed.:
status=0xFFFFFFFD
W/System.err( 4133): at android.media.MediaPlayer.prepare(Native
Method)

and the most important is my game's FPS dropped to the least and the
device is shedding its sweat to run the game. :(

Is there a simple way to play the sounds smoothly (i mean repeat the
sounds without creating them again & again or resetting it every
time...?)

coz if i reset am getting the IlleagalStateException even though the
setDataSource is called right after reset()...?

I cannot upgrade the SDK to 1.5 in the current situation but will try
the new SDK from my next project.

Can you please suggest me what to do in the current situation with SDK
1.1....?


On Apr 28, 5:10pm, Sudha <sudhaker...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



MediaPlayer.start() doesnot work fine after calling MediaPlayer.seekTo(0)

by Sudha » Wed, 29 Apr 2009 12:57:01 GMT


 ey niko,

when calling Stop() then SeekTo() then start(), my game is lagging
very much.... seems this is not working fine too.... FPS of my game
drops to 7 from 24 if i do this... :(

On Apr 27, 11:34pm, niko20 <nikolatesl...@yahoo.com> wrote:
--~--~---------~--~----~------------~-------~--~----~