Hi,

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

                // mp.prepareAsync();
                if (isLooping())
                {
                        mp.setLooping(true);
                }
                mp.prepare();

        } catch (Exception e) {
                mp.release();
                deleteSoundFile(res);
                isPlayingSound = false;
                mp = null;
                new PlayerImpl(fileName,fileInputStream);
                last_req="";
                playSound(fileName);
        }
}


please reply asap...

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to