Skip to content

Commit

Permalink
Moved ScannerFragment to example so its not included in the module
Browse files Browse the repository at this point in the history
  • Loading branch information
ANEWGALAXI committed Dec 14, 2020
1 parent 35ee545 commit f41e876
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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 -> {
Expand Down Expand Up @@ -127,4 +149,5 @@ public MediaPlayer playBeepSound() {
return null;
}
}

}

0 comments on commit f41e876

Please sign in to comment.