11package com .otaliastudios .cameraview ;
22
33import android .Manifest ;
4+ import android .annotation .SuppressLint ;
45import android .annotation .TargetApi ;
56import android .app .Activity ;
67import android .content .Context ;
1112import android .graphics .PointF ;
1213import android .graphics .Rect ;
1314import android .graphics .YuvImage ;
15+ import android .media .MediaActionSound ;
1416import android .os .Build ;
1517import android .os .Handler ;
1618import 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 }
0 commit comments