Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new MediaPlayer everytime when need a beep. For fixing issue #155 #221

Merged
merged 1 commit into from
Oct 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ public final class BeepManager implements
private static final long VIBRATE_DURATION = 200L;

private final Activity activity;
private MediaPlayer mediaPlayer;
private boolean playBeep;

private boolean beepEnabled = true;
private boolean vibrateEnabled = false;

public BeepManager(Activity activity) {
this.activity = activity;
this.mediaPlayer = null;
updatePrefs();
}

Expand Down Expand Up @@ -81,17 +79,16 @@ public void setVibrateEnabled(boolean vibrateEnabled) {

public synchronized void updatePrefs() {
playBeep = shouldBeep(beepEnabled, activity);
if (playBeep && mediaPlayer == null) {
if (playBeep) {
// The volume on STREAM_SYSTEM is not adjustable, and users found it too loud,
// so we now play on the music stream.
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = buildMediaPlayer(activity);
}
}

public synchronized void playBeepSoundAndVibrate() {
if (playBeep && mediaPlayer != null) {
mediaPlayer.start();
if (playBeep) {
buildMediaPlayer(activity).start();
}
if (vibrateEnabled) {
Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
Expand Down Expand Up @@ -136,7 +133,8 @@ private MediaPlayer buildMediaPlayer(Context activity) {
@Override
public void onCompletion(MediaPlayer mp) {
// When the beep has finished playing, rewind to queue up another one.
mp.seekTo(0);
mp.stop();
mp.release();
}

@Override
Expand All @@ -147,17 +145,12 @@ public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
} else {
// possibly media player error, so release and recreate
mp.release();
mediaPlayer = null;
updatePrefs();
}
return true;
}

@Override
public synchronized void close() {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}