MediaActionSound support

pull/1/head
Mattia Iavarone 7 years ago
parent 89d1573717
commit db23050ee3
  1. 1
      README.md
  2. 14
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  3. 8
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java
  4. 4
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java
  5. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java
  6. 45
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  7. 1
      cameraview/src/main/res/values/attrs.xml

@ -462,6 +462,7 @@ This is what was done since the library was forked. I have kept the original str
- *add multiple `CameraListener`s for events* - *add multiple `CameraListener`s for events*
- *gesture framework support* - *gesture framework support*
- *scroll gestures support* - *scroll gestures support*
- *MediaActionSound support*
These are still things that need to be done, off the top of my head: These are still things that need to be done, off the top of my head:

@ -359,7 +359,6 @@ class Camera1 extends CameraController {
final int sensorToDisplay = computeSensorToDisplayOffset(); final int sensorToDisplay = computeSensorToDisplayOffset();
synchronized (mLock) { synchronized (mLock) {
Camera.Parameters params = mCamera.getParameters(); Camera.Parameters params = mCamera.getParameters();
Log.e(TAG, "Setting exif rotation to "+exifRotation);
params.setRotation(exifRotation); params.setRotation(exifRotation);
mCamera.setParameters(params); mCamera.setParameters(params);
} }
@ -381,14 +380,14 @@ class Camera1 extends CameraController {
@Override @Override
void captureSnapshot() { boolean captureSnapshot() {
if (!isCameraOpened()) return; if (!isCameraOpened()) return false;
if (mIsCapturingImage) return; if (mIsCapturingImage) return false;
// This won't work while capturing a video. // This won't work while capturing a video.
// Switch to capturePicture. // Switch to capturePicture.
if (mIsCapturingVideo) { if (mIsCapturingVideo) {
capturePicture(); capturePicture();
return; return false;
} }
mIsCapturingImage = true; mIsCapturingImage = true;
mCamera.setOneShotPreviewCallback(new Camera.PreviewCallback() { mCamera.setOneShotPreviewCallback(new Camera.PreviewCallback() {
@ -420,6 +419,7 @@ class Camera1 extends CameraController {
}).start(); }).start();
} }
}); });
return true;
} }
@Override @Override
@ -554,7 +554,7 @@ class Camera1 extends CameraController {
} }
@Override @Override
void endVideo() { boolean endVideo() {
if (mIsCapturingVideo) { if (mIsCapturingVideo) {
mIsCapturingVideo = false; mIsCapturingVideo = false;
mMediaRecorder.stop(); mMediaRecorder.stop();
@ -564,7 +564,9 @@ class Camera1 extends CameraController {
mCameraCallbacks.dispatchOnVideoTaken(mVideoFile); mCameraCallbacks.dispatchOnVideoTaken(mVideoFile);
mVideoFile = null; mVideoFile = null;
} }
return true;
} }
return false;
} }

@ -167,8 +167,8 @@ class Camera2 extends CameraController {
} }
@Override @Override
void captureSnapshot() { boolean captureSnapshot() {
return true;
} }
@Override @Override
@ -177,8 +177,8 @@ class Camera2 extends CameraController {
} }
@Override @Override
void endVideo() { boolean endVideo() {
return false;
} }
@Override @Override

@ -39,9 +39,9 @@ abstract class CameraController implements Preview.SurfaceCallback {
abstract void setLocation(double latitude, double longitude); abstract void setLocation(double latitude, double longitude);
abstract boolean capturePicture(); abstract boolean capturePicture();
abstract void captureSnapshot(); abstract boolean captureSnapshot();
abstract boolean startVideo(@NonNull File file); abstract boolean startVideo(@NonNull File file);
abstract void endVideo(); abstract boolean endVideo();
abstract Size getCaptureSize(); abstract Size getCaptureSize();
abstract Size getPreviewSize(); abstract Size getPreviewSize();

@ -62,7 +62,7 @@ public abstract class CameraListener {
/** /**
* Notifies that the device was tilted or the window offset changed. * Notifies that the device was tilted or the window offset changed.
* The orientation passed is exactly the rotation that a View should have, * The orientation passed is exactly the counter-clockwise rotation that a View should have,
* in order to appear correctly oriented to the user, considering the way she is * in order to appear correctly oriented to the user, considering the way she is
* holding the device, and the native activity orientation. * holding the device, and the native activity orientation.
* *

@ -1,6 +1,7 @@
package com.otaliastudios.cameraview; package com.otaliastudios.cameraview;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
@ -11,6 +12,7 @@ import android.content.res.TypedArray;
import android.graphics.PointF; import android.graphics.PointF;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.YuvImage; import android.graphics.YuvImage;
import android.media.MediaActionSound;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
@ -95,9 +97,11 @@ public class CameraView extends FrameLayout {
@SuppressWarnings("WrongConstant") @SuppressWarnings("WrongConstant")
private void init(@NonNull Context context, @Nullable AttributeSet attrs) { private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0);
mJpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, DEFAULT_JPEG_QUALITY); // Self managed
mCropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, DEFAULT_CROP_OUTPUT); int jpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, DEFAULT_JPEG_QUALITY);
boolean cropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, DEFAULT_CROP_OUTPUT);
// Camera controller params
Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Facing.DEFAULT.value())); Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Facing.DEFAULT.value()));
Flash flash = Flash.fromValue(a.getInteger(R.styleable.CameraView_cameraFlash, Flash.DEFAULT.value())); Flash flash = Flash.fromValue(a.getInteger(R.styleable.CameraView_cameraFlash, Flash.DEFAULT.value()));
Grid grid = Grid.fromValue(a.getInteger(R.styleable.CameraView_cameraGrid, Grid.DEFAULT.value())); Grid grid = Grid.fromValue(a.getInteger(R.styleable.CameraView_cameraGrid, Grid.DEFAULT.value()));
@ -105,6 +109,7 @@ public class CameraView extends FrameLayout {
VideoQuality videoQuality = VideoQuality.fromValue(a.getInteger(R.styleable.CameraView_cameraVideoQuality, VideoQuality.DEFAULT.value())); VideoQuality videoQuality = VideoQuality.fromValue(a.getInteger(R.styleable.CameraView_cameraVideoQuality, VideoQuality.DEFAULT.value()));
SessionType sessionType = SessionType.fromValue(a.getInteger(R.styleable.CameraView_cameraSessionType, SessionType.DEFAULT.value())); SessionType sessionType = SessionType.fromValue(a.getInteger(R.styleable.CameraView_cameraSessionType, SessionType.DEFAULT.value()));
// Gestures
GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, GestureAction.DEFAULT_TAP.value())); GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, GestureAction.DEFAULT_TAP.value()));
GestureAction longTapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureLongTap, GestureAction.DEFAULT_LONG_TAP.value())); GestureAction longTapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureLongTap, GestureAction.DEFAULT_LONG_TAP.value()));
GestureAction pinchGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGesturePinch, GestureAction.DEFAULT_PINCH.value())); GestureAction pinchGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGesturePinch, GestureAction.DEFAULT_PINCH.value()));
@ -125,12 +130,20 @@ public class CameraView extends FrameLayout {
addView(mScrollGestureLayout); addView(mScrollGestureLayout);
mIsStarted = false; mIsStarted = false;
// Self managed
setCropOutput(cropOutput);
setJpegQuality(jpegQuality);
// Camera controller params
setFacing(facing); setFacing(facing);
setFlash(flash); setFlash(flash);
setSessionType(sessionType); setSessionType(sessionType);
setVideoQuality(videoQuality); setVideoQuality(videoQuality);
setWhiteBalance(whiteBalance); setWhiteBalance(whiteBalance);
setGrid(grid); setGrid(grid);
// Gestures
mapGesture(Gesture.TAP, tapGesture); mapGesture(Gesture.TAP, tapGesture);
// mapGesture(Gesture.DOUBLE_TAP, doubleTapGesture); // mapGesture(Gesture.DOUBLE_TAP, doubleTapGesture);
mapGesture(Gesture.LONG_TAP, longTapGesture); mapGesture(Gesture.LONG_TAP, longTapGesture);
@ -997,7 +1010,9 @@ public class CameraView extends FrameLayout {
* @see #captureSnapshot() * @see #captureSnapshot()
*/ */
public void capturePicture() { public void capturePicture() {
mCameraController.capturePicture(); if (mCameraController.capturePicture() && mUseSounds) {
// TODO: sound
}
} }
@ -1012,7 +1027,10 @@ public class CameraView extends FrameLayout {
* @see #capturePicture() * @see #capturePicture()
*/ */
public void captureSnapshot() { public void captureSnapshot() {
mCameraController.captureSnapshot(); if (mCameraController.captureSnapshot() && mUseSounds) {
//noinspection all
sound(MediaActionSound.SHUTTER_CLICK);
}
} }
@ -1079,9 +1097,10 @@ public class CameraView extends FrameLayout {
* This will fire {@link CameraListener#onVideoTaken(File)}. * This will fire {@link CameraListener#onVideoTaken(File)}.
*/ */
public void stopCapturingVideo() { public void stopCapturingVideo() {
mCameraController.endVideo(); if (mCameraController.endVideo()) {
if (getKeepScreenOn() != mKeepScreenOn) setKeepScreenOn(mKeepScreenOn); if (getKeepScreenOn() != mKeepScreenOn) setKeepScreenOn(mKeepScreenOn);
} }
}
/** /**
@ -1144,6 +1163,17 @@ public class CameraView extends FrameLayout {
// ---------------------- // ----------------------
private MediaActionSound mSound;
private final boolean mUseSounds = Build.VERSION.SDK_INT >= 16;
@SuppressWarnings("all")
private void sound(int soundType) {
if (mUseSounds) {
if (mSound == null) mSound = new MediaActionSound();
mSound.play(soundType);
}
}
class CameraCallbacks { class CameraCallbacks {
private ArrayList<CameraListener> mListeners; private ArrayList<CameraListener> mListeners;
@ -1289,6 +1319,11 @@ public class CameraView extends FrameLayout {
uiHandler.post(new Runnable() { uiHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (success && mUseSounds) {
//noinspection all
sound(MediaActionSound.FOCUS_COMPLETE);
}
if (gesture != null && mGestureMap.get(gesture) == GestureAction.FOCUS_WITH_MARKER) { if (gesture != null && mGestureMap.get(gesture) == GestureAction.FOCUS_WITH_MARKER) {
mTapGestureLayout.onFocusEnd(success); mTapGestureLayout.onFocusEnd(success);
} }

@ -40,7 +40,6 @@
<enum name="exposureCorrection" value="5" /> <enum name="exposureCorrection" value="5" />
</attr> </attr>
<attr name="cameraFacing" format="enum"> <attr name="cameraFacing" format="enum">
<enum name="back" value="0" /> <enum name="back" value="0" />
<enum name="front" value="1" /> <enum name="front" value="1" />

Loading…
Cancel
Save