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



Other Threads

1. Android:: Service::OnDestroy being called late..

Hello all,

I have service with thread, From activity I am calling start
service(from Activity::onDestroy()) and stop service
(activity::onCreate()).

StopService() returned instantly but my Service::OnDestroy called
after so many time, pretty late, Why? How to make Service::OnDestroy
called immediately when calling StopService().

Activity class

MyActivity::OnDestroy()
{
  System.out.println("MyActivity-OnDestroy Start");
  // we are leaving activity so start service
  Intent svcIntent = new Intent(this,MyService.class);
  svcIntent.putExtra("SomeData", m_iData);
  startService(svcIntent);
  System.out.println("MyActivity-OnDestroy, startservice called");
  ..
  ..
  System.out.println("MyActivity-OnDestroy, End");
}
MyActivity::OnCreate()
{
  System.out.println("MyActivity-onCreate Start");
  // if service running stop it.
  if (m_bCalledFromNotification)
  {
   System.out.println("Stopping Service");
   boolean b = stopService(new Intent(this,AudioService.class));
   System.out.println("Stopping Service, Status="+b);
  }
  System.out.println("MyActivity-onCreate End");
}
Service class

@Override
public IBinder onBind(Intent intent)
{
    return null;
}

// When service is created
@Override
public void onCreate()
{
    // initialize service data
    System.out.println("MyService Created");
}

@Override
public void onDestroy()
{
    System.out.println("In MyService::onDestroy");
    m_UIUpdater.interrupt();
    System.out.println("In MyService::onDestroy, updater thread
interrupted");
    ..
    System.out.println("My Service Stopped");
}

// This is the old onStart method that will be called on the pre-2.0
// platform.  On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId)
{
    StartupHandler(intent,startId);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    StartupHandler(intent,startId);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

public void StartupHandler(Intent intent, int startid)
{
    // send notification
    System.out.println("In MyService::StartupHandler, Sending
notification");

    // start thread
    m_UIUpdater = new Thread(new Runnable()
        {
        public void run()
        {
            System.out.println("In MyService::Thread::Run");
            while (!Thread.interrupted())
            {
                // check update
                System.out.println("In MyService::Thread::Run::got
update and loop for next update");
            }
        }
    }
}
Now here is output of logcat which show how late MyService::OnDestroy
called.

12-17 09:25:44.462: INFO/System.out(314): MyActivity-OnDestroy Start
..
12-17 09:25:44.493: INFO/System.out(314): MyActivity-OnDestroy,
startservice called
..
12-17 09:25:51.102: INFO/System.out(314): MyActivity-OnDestroy, End
12-17 09:25:51.153: INFO/System.out(314): MyService Created
12-17 09:25:51.172: INFO/System.out(314): In
MyService::StartupHandler, Sending notification
12-17 09:25:51.273: INFO/System.out(314): In MyService::Thread::Run
12-17 09:25:51.302: INFO/System.out(314): In
MyService::Thread::Run::got update and loop for next update
..
..
..
12-17 09:25:59.903: INFO/System.out(314): MyActivity-onCreate Start
..
12-17 09:26:00.461: INFO/System.out(314): Stopping Service
12-17 09:26:00.471: INFO/System.out(314): Stopping Service,
Status=true
12-17 09:26:01.363: INFO/System.out(314): MyActivity-onCreate End
12-17 09:26:01.421: INFO/System.out(314): In MyService::OnDestroy
12-17 09:26:01.421: INFO/System.out(314): In MyService::OnDestroy,
updater interrupted
12-17 09:26:01.591: INFO/System.out(314): My Service Stopped
Have i implemented service code correctly?

How can i wait for service to destroy gracefully so that i can do
remaining process after service has destroyed, like reading data
service has stopped.

Thanks,
JRC

-- 

2. Apps Missing in Developers Console

I am missing some apps in my developer console.2 published and 2
unpublished.This just happened.

Any one else missing apps?Maybe they are updating the Market?

-- 

3. How to get a contact's facebook id or url from native contacts / content resolver?

4. Need Help Root magic

5. Upgrade Galaxy 2.1 eclairs to 2.2 froyo in india..

6. Who broadcast the MESSAGE_STATUS_RECEIVED_ACTION message?

7. Do new threads inherit the priority of the creating thread in native code?