Hi,
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:03 am, niko20 <[email protected]> wrote:
> Sorry you are right, I meant you have to pause. Worth a try anyway..
>
> On Apr 27, 4:19 pm, Dave Sparks <[email protected]> wrote:
>
> > Try 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:20 am, Sudha <[email protected]> wrote:
>
> > > 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
-~----------~----~----~----~------~----~------~--~---