0

I have an application with the following activities:

A->B->C

On C I am using the following code to play an mp3 file in loop when my web service returns true (which we check for periodically):

void PlayBeepLoop()
{

    try {
        if(player.isPlaying())
            return;

        AssetFileDescriptor descriptor = getAssets().openFd("beep.mp3");
        player.reset();
        player.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
        descriptor.close();

        player.prepare();
        player.setVolume(1f, 1f);
        player.setLooping(true);
        player.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The problem is that if I go from C->B on pressing back, I believe the loop keeps on running and it beeps when it returns true.

Following code on C also does not work:

public void onBackPressed() {
    super.onBackPressed();
    finish();
}

Please help PS: I need the beep to stop only when the back button is pressed. Pressing home button while app is still showing C should allow the beeps to happen, when the app is in background.

Akshay J
  • 5,362
  • 13
  • 68
  • 105

2 Answers2

0

Remove super.onBackPressed() and add @Override above your onBackPressed() method.

raasesh
  • 161
  • 11
0

Instead of calling

finish();

use

player.stop();
finish();