Yes, I did search for the answer before I posted this. :)
Made board game, each piece gets roll of die and then moves step by step on squares. Wanted to follow each step with short beep. Added method "beep();" and placed it in handler that moves position of piece on screen.
When I add beep(); game works couple of moves and then simply stops, without any report or error message. View closes on screen and dialog just says "Unfortunately, BoardLS has stopped [Ok]", and nothing else happens.
When I remove beep(); game works ok.
Please, if anyone knows how beep crashes the game, and how to avoid it, tell me as simple as you can. Thanks.
Here's how I made the beep:
private void beep() {
ToneGenerator beepSound = new ToneGenerator(AudioManager.STREAM_MUSIC, 60);
beepSound.startTone(ToneGenerator.TONE_PROP_BEEP, 75);
} // Short sound
and I call it like this:
private final Handler playMove = new Handler() {
@Override
public void handleMessage(Message playMsg) {
beep();
player.setX(pcX[playPos]);
player.setY(pcY[playPos]);
}
}; // Put Player piece to new coordinates
playPos is square number where the piece is or will be,
pcX and pcY are arrays with on-screen coordinates of squares.
handler is in MainActivity,
and called from AsyncTask with playMove.sendEmptyMessage(0);
AsyncTask works directly with global variables, making no problems here.
Just release created ToneGenerator
objects using release()
method.
private void beep() {
ToneGenerator beepSound = new ToneGenerator(AudioManager.STREAM_MUSIC, 60);
beepSound.startTone(ToneGenerator.TONE_PROP_BEEP, 75);
beepSound.release();
}