Question

I am trying to run audios one after another with a time gap of 5 seconds. However I dont really see that happening. My third audio in the list is played and others are skipped. This means while debugging only music3 is being played and others are not. I would love alternate methods of doing this. Moreover I used prepare method just like that. Makes no difference while running though.

for(int j=0;j<3;j++)
{

    if(j==0)
        {
        xnp = MediaPlayer.create(this,R.raw.ticktock);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                xnp.prepare();
                xnp.start();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        }, 5000);

        xnp.stop();
        }
    if(j==1)
        {
        xnp = MediaPlayer.create(this,R.raw.music2);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                xnp.prepare();
                xnp.start();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        }, 5000);

        xnp.stop();
        }
    else if (j==2)
        {
        xnp = MediaPlayer.create(this,R.raw.music3);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    xnp.prepare();
                    xnp.start();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            }, 5000);
        xnp.stop();
        intenter();
        }

}       

No correct solution

OTHER TIPS

Your current implementation does not work because the media playback is not done on the main thread, so you are immediately playing all 3 media resources, so only the third one is heard.

I think you might want to call MediaPlayer#setOnCompletionListener on your MediaPlayer object. In the completion listener, you can then postDelayed onto your Handler to queue up the next resource to play.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top