BeepManager.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2010 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.usai.redant.photo;
  17. import android.app.Activity;
  18. import android.content.Context;
  19. import android.content.SharedPreferences;
  20. import android.content.res.AssetFileDescriptor;
  21. import android.media.AudioManager;
  22. import android.media.MediaPlayer;
  23. import android.os.Vibrator;
  24. import android.preference.PreferenceManager;
  25. import android.util.Log;
  26. import java.io.IOException;
  27. /**
  28. * Manages beeps and vibrations for {@link CaptureActivity}.
  29. */
  30. final class BeepManager implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
  31. private static final String TAG = BeepManager.class.getSimpleName();
  32. private static final float BEEP_VOLUME = 0.10f;
  33. private static final long VIBRATE_DURATION = 200L;
  34. private final Activity activity;
  35. private MediaPlayer mediaPlayer;
  36. private boolean playBeep;
  37. private boolean vibrate;
  38. BeepManager(Activity activity) {
  39. this.activity = activity;
  40. this.mediaPlayer = null;
  41. updatePrefs();
  42. }
  43. synchronized void updatePrefs() {
  44. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
  45. playBeep = shouldBeep(prefs, activity);
  46. vibrate = prefs.getBoolean(PreferencesActivity.KEY_VIBRATE, false);
  47. if (playBeep && mediaPlayer == null) {
  48. // The volume on STREAM_SYSTEM is not adjustable, and users found it too loud,
  49. // so we now play on the music stream.
  50. activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
  51. mediaPlayer = buildMediaPlayer(activity);
  52. }
  53. }
  54. synchronized void playBeepSoundAndVibrate() {
  55. Log.i(TAG,"playBeep=========>"+playBeep);
  56. Log.i(TAG,"mediaPlayer=========>"+mediaPlayer==null?"null":"ok");
  57. Log.i(TAG,"vibrate=========>"+vibrate);
  58. if (playBeep && mediaPlayer != null) {
  59. mediaPlayer.start();
  60. }
  61. if (vibrate) {
  62. Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
  63. vibrator.vibrate(VIBRATE_DURATION);
  64. }
  65. }
  66. private static boolean shouldBeep(SharedPreferences prefs, Context activity) {
  67. boolean shouldPlayBeep = prefs.getBoolean(PreferencesActivity.KEY_PLAY_BEEP, true);
  68. if (shouldPlayBeep) {
  69. // See if sound settings overrides this
  70. AudioManager audioService = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
  71. if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
  72. shouldPlayBeep = false;
  73. }
  74. }
  75. return shouldPlayBeep;
  76. }
  77. private MediaPlayer buildMediaPlayer(Context activity) {
  78. MediaPlayer mediaPlayer = new MediaPlayer();
  79. mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  80. mediaPlayer.setOnCompletionListener(this);
  81. mediaPlayer.setOnErrorListener(this);
  82. AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
  83. try {
  84. mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
  85. file.close();
  86. mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
  87. mediaPlayer.prepare();
  88. } catch (IOException ioe) {
  89. Log.w(TAG, ioe);
  90. mediaPlayer = null;
  91. }
  92. return mediaPlayer;
  93. }
  94. @Override
  95. public void onCompletion(MediaPlayer mp) {
  96. // When the beep has finished playing, rewind to queue up another one.
  97. mp.seekTo(0);
  98. }
  99. @Override
  100. public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
  101. if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
  102. // we are finished, so put up an appropriate error toast if required and finish
  103. activity.finish();
  104. } else {
  105. // possibly media player error, so release and recreate
  106. mp.release();
  107. mediaPlayer = null;
  108. updatePrefs();
  109. }
  110. return true;
  111. }
  112. }