7

I'm learning how to use threads in Android, and to do that I've made a small application that plays a series of notes. The idea is that there is a start button and an end button and that (obviously) if you press the start button it starts playing music, and if you press the end button, it stops. The start button works just fine, but the problem is that the end button doesn't. I'm having trouble figuring out why, so maybe some of you can help me out. This is the code:

public class PressAndPlay extends Activity {
    private volatile Thread initBkgdThread;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button startButton = (Button) findViewById(R.id.trigger);
        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                // create thread
                initBkgdThread = new Thread(new Runnable() {
                    public void run() {
                        play_music();
                    }
                });
                initBkgdThread.start();
            }
        });

        Button endButton = (Button) findViewById(R.id.end);
        endButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                end_music();
            }
        });
    }

    int[] notes = {R.raw.c5, R.raw.b4, R.raw.a4, R.raw.g4};
    int NOTE_DURATION = 400;
    MediaPlayer m_mediaPlayer;

    private void play_music() {
        for(int ii=0; ii<12; ii++) {
            //check to ensure main activity is not paused
            if(!paused) {
                if (m_mediaPlayer != null) {m_mediaPlayer.release();}
                m_mediaPlayer = MediaPlayer.create(this, notes[ii%4]);
                m_mediaPlayer.start();
                try {
                    Thread.sleep(NOTE_DURATION);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void end_music() {
         if(initBkgdThread != null) {
             initBkgdThread.interrupt();
             initBkgdThread = null;
         }
    }

    boolean paused = false;
    @Override
    protected void onPause() {
        paused = true;
        super.onPause();
    }
    @Override
    protected void onResume() {
        super.onResume();
        paused = false;
    }
}
Gray
  • 115,027
  • 24
  • 293
  • 354
Zero
  • 1,864
  • 3
  • 21
  • 39

1 Answers1

10

You are calling interrupt() on the playing thread but it is probably waiting in sleep at the time. This will cause sleep to throw a InterruptedException. You need to catch that exception and exit the loop to stop the playing:

try {
    Thread.sleep(NOTE_DURATION);
} catch (InterruptedException e) {
    // XXX need to stop playing here, maybe return or break?
    return;
}

Since the interrupt() can also come at a different time, you need to test for interrupt status and quit your loop:

if (!paused && !Thread.currentThread().isInterrupted()) {
   ...

Also, all variables that are shared between two threads either need to be synchronized or be marked volatile. The paused flag should probably be volatile here:

volatile boolean paused = false

Lastly, for posterity, when you catch InterruptedException, it clears the interrupt status of the thread. It is usually good practice to immediately set the interrupt flag on the thread so others can test for it:

try {
    Thread.sleep(NOTE_DURATION);
} catch (InterruptedException e) {
    // re-establish the interrupt condition
    Thread.currentThread.interrupt();
    ...
}
Gray
  • 115,027
  • 24
  • 293
  • 354
  • I didn't get the last point, what is the difference between executing `return;` and re-establishing the interrupt condition. – Muhammed Refaat Oct 19 '15 at 13:53
  • 1
    Whenever you catch `InterruptedException` the interrupt status on the thread is cleared so you should re-interrupt the thread. This is an important pattern so that if you code is in a library, the caller will be notified that the thread was interrupted. It's just a pattern I always mention @MuhammedRefaat. – Gray Oct 19 '15 at 14:38
  • Great, like if code handling would be change when interrupting, thanks. – Muhammed Refaat Oct 20 '15 at 07:36