PageRenderTime 41ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/core/java/android/hardware/CameraSound.java

https://github.com/Kali-/android_frameworks_base
Java | 217 lines | 135 code | 19 blank | 63 comment | 20 complexity | 4208d712f2f098e9710d1813898a2283 MD5 | raw file
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  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 android.hardware;
  17. import android.media.AudioManager;
  18. import android.media.MediaPlayer;
  19. import android.os.SystemProperties;
  20. import android.util.Log;
  21. import java.io.IOException;
  22. /**
  23. * <p>Use this class to play an appropriate sound when implementing a custom
  24. * still or video recording mechanism through the preview callbacks.</p>
  25. *
  26. * <p>There is no need to play sounds when using {@link #android.hardware.Camera#takePicture}
  27. * or {@link android.media.MediaRecorder} for still images or video,
  28. * respectively, as these play their own sounds when needed.</p>
  29. *
  30. * @hide
  31. */
  32. public class CameraSound {
  33. private static final String TAG = "CameraSound";
  34. /**
  35. * The sound used by {@link android.hardware.Camera#takePicture} to
  36. * indicate still image capture.
  37. */
  38. public static final int SHUTTER_CLICK = 0;
  39. /**
  40. * A sound to indicate that focusing has completed. Because deciding
  41. * when this occurs is application-dependent, this sound is not used by
  42. * any methods in the Camera class.
  43. */
  44. public static final int FOCUS_COMPLETE = 1;
  45. /**
  46. * The sound used by {@link android.media.MediaRecorder#start} to
  47. * indicate the start of video recording.
  48. */
  49. public static final int START_VIDEO_RECORDING = 2;
  50. /**
  51. * The sound used by {@link android.media.MediaRecorder#stop} to
  52. * indicate the end of video recording.
  53. */
  54. public static final int STOP_VIDEO_RECORDING = 3;
  55. private static final int NUM_SOUNDS = 4;
  56. private CameraSoundPlayer[] mCameraSoundPlayers;
  57. public CameraSound() {
  58. }
  59. /**
  60. * <p>Play one of the predefined platform sounds for camera actions.</p>
  61. *
  62. * <p>Use this method to play a platform-specific sound for various camera
  63. * actions. The sound playing is done asynchronously, with the same behavior
  64. * and content as the sounds played by {@link #takePicture takePicture},
  65. * {@link android.media.MediaRecorder#start MediaRecorder.start}, and
  66. * {@link android.media.MediaRecorder#stop MediaRecorder.stop}.</p>
  67. *
  68. * <p>Using this method makes it easy to match the default device sounds
  69. * when recording or capturing data through the preview callbacks.</p>
  70. *
  71. * @param soundId The type of sound to play, selected from SHUTTER_CLICK,
  72. * FOCUS_COMPLETE, START_VIDEO_RECORDING, or STOP_VIDEO_RECORDING.
  73. * @see android.hardware#takePicture
  74. * @see android.media.MediaRecorder
  75. * @see #SHUTTER_CLICK
  76. * @see #FOCUS_COMPLETE
  77. * @see #START_VIDEO_RECORDING
  78. * @see #STOP_VIDEO_RECORDING
  79. */
  80. public void playSound(int soundId) {
  81. if (mCameraSoundPlayers == null) {
  82. mCameraSoundPlayers = new CameraSoundPlayer[NUM_SOUNDS];
  83. }
  84. if (mCameraSoundPlayers[soundId] == null) {
  85. mCameraSoundPlayers[soundId] = new CameraSoundPlayer(soundId);
  86. }
  87. mCameraSoundPlayers[soundId].play();
  88. }
  89. public void release() {
  90. if (mCameraSoundPlayers != null) {
  91. for (CameraSoundPlayer csp: mCameraSoundPlayers) {
  92. if (csp != null) {
  93. csp.release();
  94. }
  95. }
  96. mCameraSoundPlayers = null;
  97. }
  98. }
  99. private static class CameraSoundPlayer implements Runnable {
  100. private int mSoundId;
  101. private MediaPlayer mPlayer;
  102. private Thread mThread;
  103. private boolean mExit;
  104. private int mPlayCount;
  105. private static final String mShutterSound =
  106. "/system/media/audio/ui/camera_click.ogg";
  107. private static final String mFocusSound =
  108. "/system/media/audio/ui/camera_focus.ogg";
  109. private static final String mVideoStartSound =
  110. "/system/media/audio/ui/VideoRecord.ogg";
  111. private static final String mVideoStopSound =
  112. "/system/media/audio/ui/VideoRecord.ogg";
  113. @Override
  114. public void run() {
  115. String soundFilePath;
  116. switch (mSoundId) {
  117. case SHUTTER_CLICK:
  118. soundFilePath = mShutterSound;
  119. break;
  120. case FOCUS_COMPLETE:
  121. soundFilePath = mFocusSound;
  122. break;
  123. case START_VIDEO_RECORDING:
  124. soundFilePath = mVideoStartSound;
  125. break;
  126. case STOP_VIDEO_RECORDING:
  127. soundFilePath = mVideoStopSound;
  128. break;
  129. default:
  130. Log.e(TAG, "Unknown sound " + mSoundId + " requested.");
  131. return;
  132. }
  133. mPlayer = new MediaPlayer();
  134. try {
  135. mPlayer.setAudioStreamType(AudioManager.STREAM_SYSTEM_ENFORCED);
  136. mPlayer.setDataSource(soundFilePath);
  137. mPlayer.setLooping(false);
  138. mPlayer.prepare();
  139. } catch(IOException e) {
  140. Log.e(TAG, "Error setting up sound " + mSoundId, e);
  141. return;
  142. }
  143. while(true) {
  144. try {
  145. synchronized (this) {
  146. while(true) {
  147. if (mExit) {
  148. return;
  149. } else if (mPlayCount <= 0) {
  150. wait();
  151. } else {
  152. mPlayCount--;
  153. break;
  154. }
  155. }
  156. }
  157. mPlayer.start();
  158. } catch (Exception e) {
  159. Log.e(TAG, "Error playing sound " + mSoundId, e);
  160. }
  161. }
  162. }
  163. public CameraSoundPlayer(int soundId) {
  164. mSoundId = soundId;
  165. }
  166. public void play() {
  167. if (mThread == null) {
  168. mThread = new Thread(this);
  169. mThread.start();
  170. }
  171. synchronized (this) {
  172. mPlayCount++;
  173. notifyAll();
  174. }
  175. }
  176. public void release() {
  177. if (mThread != null) {
  178. synchronized (this) {
  179. mExit = true;
  180. notifyAll();
  181. }
  182. try {
  183. mThread.join();
  184. } catch (InterruptedException e) {
  185. }
  186. mThread = null;
  187. }
  188. if (mPlayer != null) {
  189. mPlayer.release();
  190. mPlayer = null;
  191. }
  192. }
  193. @Override
  194. protected void finalize() {
  195. release();
  196. }
  197. }
  198. }