Skip to content

Commit db23050

Browse files
committed
MediaActionSound support
1 parent 89d1573 commit db23050

File tree

7 files changed

+57
-20
lines changed

7 files changed

+57
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ This is what was done since the library was forked. I have kept the original str
462462
- *add multiple `CameraListener`s for events*
463463
- *gesture framework support*
464464
- *scroll gestures support*
465+
- *MediaActionSound support*
465466

466467
These are still things that need to be done, off the top of my head:
467468

cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ boolean capturePicture() {
359359
final int sensorToDisplay = computeSensorToDisplayOffset();
360360
synchronized (mLock) {
361361
Camera.Parameters params = mCamera.getParameters();
362-
Log.e(TAG, "Setting exif rotation to "+exifRotation);
363362
params.setRotation(exifRotation);
364363
mCamera.setParameters(params);
365364
}
@@ -381,14 +380,14 @@ public void onPictureTaken(byte[] data, Camera camera) {
381380

382381

383382
@Override
384-
void captureSnapshot() {
385-
if (!isCameraOpened()) return;
386-
if (mIsCapturingImage) return;
383+
boolean captureSnapshot() {
384+
if (!isCameraOpened()) return false;
385+
if (mIsCapturingImage) return false;
387386
// This won't work while capturing a video.
388387
// Switch to capturePicture.
389388
if (mIsCapturingVideo) {
390389
capturePicture();
391-
return;
390+
return false;
392391
}
393392
mIsCapturingImage = true;
394393
mCamera.setOneShotPreviewCallback(new Camera.PreviewCallback() {
@@ -420,6 +419,7 @@ public void run() {
420419
}).start();
421420
}
422421
});
422+
return true;
423423
}
424424

425425
@Override
@@ -554,7 +554,7 @@ boolean startVideo(@NonNull File videoFile) {
554554
}
555555

556556
@Override
557-
void endVideo() {
557+
boolean endVideo() {
558558
if (mIsCapturingVideo) {
559559
mIsCapturingVideo = false;
560560
mMediaRecorder.stop();
@@ -564,7 +564,9 @@ void endVideo() {
564564
mCameraCallbacks.dispatchOnVideoTaken(mVideoFile);
565565
mVideoFile = null;
566566
}
567+
return true;
567568
}
569+
return false;
568570
}
569571

570572

cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ boolean capturePicture() {
167167
}
168168

169169
@Override
170-
void captureSnapshot() {
171-
170+
boolean captureSnapshot() {
171+
return true;
172172
}
173173

174174
@Override
@@ -177,8 +177,8 @@ boolean startVideo(@NonNull File videoFile) {
177177
}
178178

179179
@Override
180-
void endVideo() {
181-
180+
boolean endVideo() {
181+
return false;
182182
}
183183

184184
@Override

cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ abstract class CameraController implements Preview.SurfaceCallback {
3939
abstract void setLocation(double latitude, double longitude);
4040

4141
abstract boolean capturePicture();
42-
abstract void captureSnapshot();
42+
abstract boolean captureSnapshot();
4343
abstract boolean startVideo(@NonNull File file);
44-
abstract void endVideo();
44+
abstract boolean endVideo();
4545

4646
abstract Size getCaptureSize();
4747
abstract Size getPreviewSize();

cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void onVideoTaken(File video) {
6262

6363
/**
6464
* Notifies that the device was tilted or the window offset changed.
65-
* The orientation passed is exactly the rotation that a View should have,
65+
* The orientation passed is exactly the counter-clockwise rotation that a View should have,
6666
* in order to appear correctly oriented to the user, considering the way she is
6767
* holding the device, and the native activity orientation.
6868
*

cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.otaliastudios.cameraview;
22

33
import android.Manifest;
4+
import android.annotation.SuppressLint;
45
import android.annotation.TargetApi;
56
import android.app.Activity;
67
import android.content.Context;
@@ -11,6 +12,7 @@
1112
import android.graphics.PointF;
1213
import android.graphics.Rect;
1314
import android.graphics.YuvImage;
15+
import android.media.MediaActionSound;
1416
import android.os.Build;
1517
import android.os.Handler;
1618
import android.os.HandlerThread;
@@ -95,16 +97,19 @@ public CameraView(@NonNull Context context, @Nullable AttributeSet attrs) {
9597
@SuppressWarnings("WrongConstant")
9698
private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
9799
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0);
98-
mJpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, DEFAULT_JPEG_QUALITY);
99-
mCropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, DEFAULT_CROP_OUTPUT);
100+
// Self managed
101+
int jpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, DEFAULT_JPEG_QUALITY);
102+
boolean cropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, DEFAULT_CROP_OUTPUT);
100103

104+
// Camera controller params
101105
Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Facing.DEFAULT.value()));
102106
Flash flash = Flash.fromValue(a.getInteger(R.styleable.CameraView_cameraFlash, Flash.DEFAULT.value()));
103107
Grid grid = Grid.fromValue(a.getInteger(R.styleable.CameraView_cameraGrid, Grid.DEFAULT.value()));
104108
WhiteBalance whiteBalance = WhiteBalance.fromValue(a.getInteger(R.styleable.CameraView_cameraWhiteBalance, WhiteBalance.DEFAULT.value()));
105109
VideoQuality videoQuality = VideoQuality.fromValue(a.getInteger(R.styleable.CameraView_cameraVideoQuality, VideoQuality.DEFAULT.value()));
106110
SessionType sessionType = SessionType.fromValue(a.getInteger(R.styleable.CameraView_cameraSessionType, SessionType.DEFAULT.value()));
107111

112+
// Gestures
108113
GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, GestureAction.DEFAULT_TAP.value()));
109114
GestureAction longTapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureLongTap, GestureAction.DEFAULT_LONG_TAP.value()));
110115
GestureAction pinchGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGesturePinch, GestureAction.DEFAULT_PINCH.value()));
@@ -125,12 +130,20 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
125130
addView(mScrollGestureLayout);
126131

127132
mIsStarted = false;
133+
134+
// Self managed
135+
setCropOutput(cropOutput);
136+
setJpegQuality(jpegQuality);
137+
138+
// Camera controller params
128139
setFacing(facing);
129140
setFlash(flash);
130141
setSessionType(sessionType);
131142
setVideoQuality(videoQuality);
132143
setWhiteBalance(whiteBalance);
133144
setGrid(grid);
145+
146+
// Gestures
134147
mapGesture(Gesture.TAP, tapGesture);
135148
// mapGesture(Gesture.DOUBLE_TAP, doubleTapGesture);
136149
mapGesture(Gesture.LONG_TAP, longTapGesture);
@@ -997,7 +1010,9 @@ public void clearCameraListeners() {
9971010
* @see #captureSnapshot()
9981011
*/
9991012
public void capturePicture() {
1000-
mCameraController.capturePicture();
1013+
if (mCameraController.capturePicture() && mUseSounds) {
1014+
// TODO: sound
1015+
}
10011016
}
10021017

10031018

@@ -1012,7 +1027,10 @@ public void capturePicture() {
10121027
* @see #capturePicture()
10131028
*/
10141029
public void captureSnapshot() {
1015-
mCameraController.captureSnapshot();
1030+
if (mCameraController.captureSnapshot() && mUseSounds) {
1031+
//noinspection all
1032+
sound(MediaActionSound.SHUTTER_CLICK);
1033+
}
10161034
}
10171035

10181036

@@ -1079,8 +1097,9 @@ public void run() {
10791097
* This will fire {@link CameraListener#onVideoTaken(File)}.
10801098
*/
10811099
public void stopCapturingVideo() {
1082-
mCameraController.endVideo();
1083-
if (getKeepScreenOn() != mKeepScreenOn) setKeepScreenOn(mKeepScreenOn);
1100+
if (mCameraController.endVideo()) {
1101+
if (getKeepScreenOn() != mKeepScreenOn) setKeepScreenOn(mKeepScreenOn);
1102+
}
10841103
}
10851104

10861105

@@ -1144,6 +1163,17 @@ private void requestPermissions(boolean requestCamera, boolean requestAudio) {
11441163
// ----------------------
11451164

11461165

1166+
private MediaActionSound mSound;
1167+
private final boolean mUseSounds = Build.VERSION.SDK_INT >= 16;
1168+
1169+
@SuppressWarnings("all")
1170+
private void sound(int soundType) {
1171+
if (mUseSounds) {
1172+
if (mSound == null) mSound = new MediaActionSound();
1173+
mSound.play(soundType);
1174+
}
1175+
}
1176+
11471177
class CameraCallbacks {
11481178

11491179
private ArrayList<CameraListener> mListeners;
@@ -1289,6 +1319,11 @@ public void dispatchOnFocusEnd(@Nullable final Gesture gesture, final boolean su
12891319
uiHandler.post(new Runnable() {
12901320
@Override
12911321
public void run() {
1322+
if (success && mUseSounds) {
1323+
//noinspection all
1324+
sound(MediaActionSound.FOCUS_COMPLETE);
1325+
}
1326+
12921327
if (gesture != null && mGestureMap.get(gesture) == GestureAction.FOCUS_WITH_MARKER) {
12931328
mTapGestureLayout.onFocusEnd(success);
12941329
}

cameraview/src/main/res/values/attrs.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<enum name="exposureCorrection" value="5" />
4141
</attr>
4242

43-
4443
<attr name="cameraFacing" format="enum">
4544
<enum name="back" value="0" />
4645
<enum name="front" value="1" />

0 commit comments

Comments
 (0)