From f41e876a71e2c27d8aa71bcd31d4497882cc8362 Mon Sep 17 00:00:00 2001 From: ANewGalaxy Date: Mon, 14 Dec 2020 03:20:34 -0500 Subject: [PATCH] Moved ScannerFragment to example so its not included in the module --- .../zxing/ContinuousCaptureActivity.java | 4 ++- .../zxing/client/android/BeepManager.java | 27 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/sample/src/main/java/example/zxing/ContinuousCaptureActivity.java b/sample/src/main/java/example/zxing/ContinuousCaptureActivity.java index 6a6d37d3d..f222bc92f 100644 --- a/sample/src/main/java/example/zxing/ContinuousCaptureActivity.java +++ b/sample/src/main/java/example/zxing/ContinuousCaptureActivity.java @@ -63,7 +63,9 @@ protected void onCreate(Bundle savedInstanceState) { barcodeView.initializeFromIntent(getIntent()); barcodeView.decodeContinuous(callback); - beepManager = new BeepManager(this); + if (beepManager != null) + + beepManager = new BeepManager(this); } @Override diff --git a/zxing-android-embedded/src/com/google/zxing/client/android/BeepManager.java b/zxing-android-embedded/src/com/google/zxing/client/android/BeepManager.java index b140eae7d..a828c9c9d 100644 --- a/zxing-android-embedded/src/com/google/zxing/client/android/BeepManager.java +++ b/zxing-android-embedded/src/com/google/zxing/client/android/BeepManager.java @@ -43,6 +43,8 @@ public final class BeepManager { private boolean beepEnabled = true; private boolean vibrateEnabled = false; + private MediaPlayer currentMediaPlayer; + public BeepManager(Activity activity) { activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); @@ -81,7 +83,14 @@ public void setVibrateEnabled(boolean vibrateEnabled) { @SuppressLint("MissingPermission") public synchronized void playBeepSoundAndVibrate() { if (beepEnabled) { - playBeepSound(); + if (currentMediaPlayer != null) { + if (currentMediaPlayer.isPlaying()) + currentMediaPlayer.stop(); + currentMediaPlayer.reset(); + currentMediaPlayer.release(); + } + + currentMediaPlayer = playBeepSound(); } if (vibrateEnabled) { Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); @@ -93,7 +102,20 @@ public synchronized void playBeepSoundAndVibrate() { public MediaPlayer playBeepSound() { - MediaPlayer mediaPlayer = new MediaPlayer(); + MediaPlayer mediaPlayer = new MediaPlayer() { + + @Override + protected void finalize() { + if (isPlaying()) + stop(); + reset(); + release(); + + super.finalize(); + } + + }; + mediaPlayer.setAudioAttributes(new AudioAttributes.Builder().setContentType( AudioAttributes.CONTENT_TYPE_MUSIC).build()); mediaPlayer.setOnCompletionListener(mp -> { @@ -127,4 +149,5 @@ public MediaPlayer playBeepSound() { return null; } } + }