diff --git a/src/android/AudioHandler.java b/src/android/AudioHandler.java index 9e734c44..f8cd4c3d 100644 --- a/src/android/AudioHandler.java +++ b/src/android/AudioHandler.java @@ -196,7 +196,7 @@ public void onDestroy() { onLastPlayerReleased(); } for (AudioPlayer audio : this.players.values()) { - audio.destroy(); + audio.onHandlerDestroyed(); } this.players.clear(); } @@ -273,7 +273,7 @@ private boolean release(String id) { if (players.isEmpty()) { onLastPlayerReleased(); } - audio.destroy(); + audio.destroy(true); return true; } diff --git a/src/android/AudioPlayer.java b/src/android/AudioPlayer.java index b40e281e..1af8d966 100644 --- a/src/android/AudioPlayer.java +++ b/src/android/AudioPlayer.java @@ -118,13 +118,18 @@ private String generateTempFile() { /** * Destroy player and stop audio playing or recording. + * @param broadcastStateChange - if false, setting the state of this oject will not send a status change to the client. */ - public void destroy() { + public void destroy(boolean broadcastStateChange) { // Stop any play or record if (this.player != null) { if ((this.state == STATE.MEDIA_RUNNING) || (this.state == STATE.MEDIA_PAUSED)) { - this.player.stop(); + this.player.stop(); + } + if (broadcastStateChange) { this.setState(STATE.MEDIA_STOPPED); + } else { // Do not call the success callback. + this.state = STATE.MEDIA_STOPPED; } this.player.release(); this.player = null; @@ -522,19 +527,26 @@ public boolean onError(MediaPlayer player, int arg1, int arg2) { LOG.d(LOG_TAG, "AudioPlayer.onError(" + arg1 + ", " + arg2 + ")"); // we don't want to send success callback - // so we don't call setState() here - this.state = STATE.MEDIA_STOPPED; - this.destroy(); + this.destroy(false); // Send error notification to JavaScript sendErrorStatus(arg1); return false; } + /** + * Lifecyle method called by handler whenever it has been destroyed. + */ + public void onHandlerDestroyed() { + destroy(false); + sendErrorStatus(MEDIA_ERR_ABORTED); + } + /** * Set the state and send it to JavaScript. * - * @param state + * @param state - state to transition to + */ private void setState(STATE state) { if (this.state != state) {