Skip to content

Commit e7005ec

Browse files
Wu-cheng LiAndroid (Google) Code Review
authored andcommitted
Merge "Move Camera.Sound to a stand-alone class CameraSound." into ics-mr1
2 parents 07a2d83 + 0cac6aa commit e7005ec

File tree

2 files changed

+223
-203
lines changed

2 files changed

+223
-203
lines changed

core/java/android/hardware/Camera.java

Lines changed: 0 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@
2222
import android.graphics.Point;
2323
import android.graphics.Rect;
2424
import android.graphics.SurfaceTexture;
25-
import android.media.AudioManager;
26-
import android.media.MediaPlayer;
2725
import android.os.Handler;
2826
import android.os.Looper;
2927
import android.os.Message;
30-
import android.os.SystemProperties;
3128
import android.util.Log;
3229
import android.view.Surface;
3330
import android.view.SurfaceHolder;
@@ -157,7 +154,6 @@ public class Camera {
157154
private boolean mOneShot;
158155
private boolean mWithBuffer;
159156
private boolean mFaceDetectionRunning = false;
160-
private boolean mReleased = false;
161157

162158
/**
163159
* Broadcast Action: A new picture is taken by the camera, and the entry of
@@ -322,15 +318,6 @@ protected void finalize() {
322318
public final void release() {
323319
native_release();
324320
mFaceDetectionRunning = false;
325-
if (mCameraSoundPlayers != null) {
326-
for (CameraSoundPlayer csp: mCameraSoundPlayers) {
327-
if (csp != null) {
328-
csp.release();
329-
}
330-
}
331-
mCameraSoundPlayers = null;
332-
}
333-
mReleased = true;
334321
}
335322

336323
/**
@@ -3503,194 +3490,4 @@ private boolean same(String s1, String s2) {
35033490
return false;
35043491
}
35053492
};
3506-
3507-
/**
3508-
* <p>The set of default system sounds for camera actions. Use this with
3509-
* {@link #playSound} to play an appropriate sound when implementing a
3510-
* custom still or video recording mechanism through the preview
3511-
* callbacks.</p>
3512-
*
3513-
* <p>There is no need to play sounds when using {@link #takePicture} or
3514-
* {@link android.media.MediaRecorder} for still images or video,
3515-
* respectively, as these play their own sounds when needed.</p>
3516-
*
3517-
* @see #playSound
3518-
* @hide
3519-
*/
3520-
public static class Sound {
3521-
/**
3522-
* The sound used by {@link android.hardware.Camera#takePicture} to
3523-
* indicate still image capture.
3524-
*/
3525-
public static final int SHUTTER_CLICK = 0;
3526-
3527-
/**
3528-
* A sound to indicate that focusing has completed. Because deciding
3529-
* when this occurs is application-dependent, this sound is not used by
3530-
* any methods in the Camera class.
3531-
*/
3532-
public static final int FOCUS_COMPLETE = 1;
3533-
3534-
/**
3535-
* The sound used by {@link android.media.MediaRecorder#start} to
3536-
* indicate the start of video recording.
3537-
*/
3538-
public static final int START_VIDEO_RECORDING = 2;
3539-
3540-
/**
3541-
* The sound used by {@link android.media.MediaRecorder#stop} to
3542-
* indicate the end of video recording.
3543-
*/
3544-
public static final int STOP_VIDEO_RECORDING = 3;
3545-
3546-
private static final int NUM_SOUNDS = 4;
3547-
};
3548-
3549-
/**
3550-
* <p>Play one of the predefined platform sounds for camera actions.</p>
3551-
*
3552-
* <p>Use this method to play a platform-specific sound for various camera
3553-
* actions. The sound playing is done asynchronously, with the same behavior
3554-
* and content as the sounds played by {@link #takePicture takePicture},
3555-
* {@link android.media.MediaRecorder#start MediaRecorder.start}, and
3556-
* {@link android.media.MediaRecorder#stop MediaRecorder.stop}.</p>
3557-
*
3558-
* <p>Using this method makes it easy to match the default device sounds
3559-
* when recording or capturing data through the preview callbacks
3560-
* ({@link #setPreviewCallback setPreviewCallback},
3561-
* {@link #setPreviewTexture setPreviewTexture}).</p>
3562-
*
3563-
* @param soundId The type of sound to play, selected from the options in
3564-
* {@link android.hardware.Camera.Sound}
3565-
* @see android.hardware.Camera.Sound
3566-
* @see #takePicture
3567-
* @see android.media.MediaRecorder
3568-
* @hide
3569-
*/
3570-
public void playSound(int soundId) {
3571-
if (mReleased) return;
3572-
if (mCameraSoundPlayers == null) {
3573-
mCameraSoundPlayers = new CameraSoundPlayer[Sound.NUM_SOUNDS];
3574-
}
3575-
if (mCameraSoundPlayers[soundId] == null) {
3576-
mCameraSoundPlayers[soundId] = new CameraSoundPlayer(soundId);
3577-
}
3578-
mCameraSoundPlayers[soundId].play();
3579-
}
3580-
3581-
private CameraSoundPlayer[] mCameraSoundPlayers;
3582-
3583-
private static class CameraSoundPlayer implements Runnable {
3584-
private int mSoundId;
3585-
private int mAudioStreamType;
3586-
private MediaPlayer mPlayer;
3587-
private Thread mThread;
3588-
private boolean mExit;
3589-
private int mPlayCount;
3590-
3591-
private static final String mShutterSound =
3592-
"/system/media/audio/ui/camera_click.ogg";
3593-
private static final String mFocusSound =
3594-
"/system/media/audio/ui/camera_focus.ogg";
3595-
private static final String mVideoStartSound =
3596-
"/system/media/audio/ui/VideoRecord.ogg";
3597-
private static final String mVideoStopSound =
3598-
"/system/media/audio/ui/VideoRecord.ogg";
3599-
3600-
@Override
3601-
public void run() {
3602-
String soundFilePath;
3603-
switch (mSoundId) {
3604-
case Sound.SHUTTER_CLICK:
3605-
soundFilePath = mShutterSound;
3606-
break;
3607-
case Sound.FOCUS_COMPLETE:
3608-
soundFilePath = mFocusSound;
3609-
break;
3610-
case Sound.START_VIDEO_RECORDING:
3611-
soundFilePath = mVideoStartSound;
3612-
break;
3613-
case Sound.STOP_VIDEO_RECORDING:
3614-
soundFilePath = mVideoStopSound;
3615-
break;
3616-
default:
3617-
Log.e(TAG, "Unknown sound " + mSoundId + " requested.");
3618-
return;
3619-
}
3620-
mPlayer = new MediaPlayer();
3621-
try {
3622-
mPlayer.setAudioStreamType(mAudioStreamType);
3623-
mPlayer.setDataSource(soundFilePath);
3624-
mPlayer.setLooping(false);
3625-
mPlayer.prepare();
3626-
} catch(IOException e) {
3627-
Log.e(TAG, "Error setting up sound " + mSoundId, e);
3628-
return;
3629-
}
3630-
3631-
while(true) {
3632-
try {
3633-
synchronized (this) {
3634-
while(true) {
3635-
if (mExit) {
3636-
return;
3637-
} else if (mPlayCount <= 0) {
3638-
wait();
3639-
} else {
3640-
mPlayCount--;
3641-
break;
3642-
}
3643-
}
3644-
}
3645-
mPlayer.start();
3646-
} catch (Exception e) {
3647-
Log.e(TAG, "Error playing sound " + mSoundId, e);
3648-
}
3649-
}
3650-
}
3651-
3652-
public CameraSoundPlayer(int soundId) {
3653-
mSoundId = soundId;
3654-
if (SystemProperties.get("ro.camera.sound.forced", "0").equals("0")) {
3655-
mAudioStreamType = AudioManager.STREAM_MUSIC;
3656-
} else {
3657-
mAudioStreamType = AudioManager.STREAM_SYSTEM_ENFORCED;
3658-
}
3659-
}
3660-
3661-
public void play() {
3662-
if (mThread == null) {
3663-
mThread = new Thread(this);
3664-
mThread.start();
3665-
}
3666-
synchronized (this) {
3667-
mPlayCount++;
3668-
notifyAll();
3669-
}
3670-
}
3671-
3672-
public void release() {
3673-
if (mThread != null) {
3674-
synchronized (this) {
3675-
mExit = true;
3676-
notifyAll();
3677-
}
3678-
try {
3679-
mThread.join();
3680-
} catch (InterruptedException e) {
3681-
}
3682-
mThread = null;
3683-
}
3684-
if (mPlayer != null) {
3685-
mPlayer.release();
3686-
mPlayer = null;
3687-
}
3688-
}
3689-
3690-
@Override
3691-
protected void finalize() {
3692-
release();
3693-
}
3694-
}
3695-
36963493
}

0 commit comments

Comments
 (0)