From aeb0083352cfe0ba11072eb702ec48679be36b28 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 11 Sep 2017 01:04:01 +0300 Subject: [PATCH] Added audio on/off feature for video recording (#33) --- README.md | 13 +++++- .../cameraview/MockCameraController.java | 5 +++ .../com/otaliastudios/cameraview/Camera1.java | 27 +++++++++++- .../com/otaliastudios/cameraview/Camera2.java | 5 +++ .../cameraview/CameraController.java | 7 ++++ .../otaliastudios/cameraview/CameraView.java | 24 +++++++++++ .../com/otaliastudios/cameraview/Audio.java | 42 +++++++++++++++++++ cameraview/src/main/res/values/attrs.xml | 5 +++ demo/src/main/res/layout/activity_main.xml | 1 + 9 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 cameraview/src/main/options/com/otaliastudios/cameraview/Audio.java diff --git a/README.md b/README.md index d123e79a..beb3a9aa 100644 --- a/README.md +++ b/README.md @@ -295,7 +295,8 @@ Most camera parameters can be controlled through XML attributes or linked method app:cameraJpegQuality="100" app:cameraVideoQuality="480p" app:cameraWhiteBalance="auto" - app:cameraHdr="off" /> + app:cameraHdr="off" + app:cameraAudio="on"/> ``` |XML Attribute|Method|Values|Default Value| @@ -309,6 +310,7 @@ Most camera parameters can be controlled through XML attributes or linked method |[`cameraVideoQuality`](#cameravideoquality)|`setVideoQuality()`|`lowest` `highest` `maxQvga` `max480p` `max720p` `max1080p` `max2160p`|`max480p`| |[`cameraWhiteBalance`](#camerawhitebalance)|`setWhiteBalance()`|`auto` `incandescent` `fluorescent` `daylight` `cloudy`|`auto`| |[`cameraHdr`](#camerahdr)|`setHdr()`|`off` `on`|`off`| +|[`cameraAudio`](#cameraaudio)|`setAudio()`|`off` `on`|`on`| #### cameraSessionType @@ -404,6 +406,15 @@ cameraView.setHdr(Hdr.OFF); cameraView.setHdr(Hdr.ON); ``` +#### cameraAudio + +Turns on or off audio stream. + +```java +cameraView.setAudio(Audio.OFF); +cameraView.setAudio(Audio.ON); +``` + ## Other APIs Other APIs not mentioned above are provided, and are well documented and commented in code. diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraController.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraController.java index ce150fd2..4c0b30b2 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraController.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraController.java @@ -78,6 +78,11 @@ public class MockCameraController extends CameraController { mHdr = hdr; } + @Override + void setAudio(Audio audio) { + mAudio = audio; + } + @Override void setLocation(Location location) { mLocation = location; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index a039ada2..61883a10 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -291,6 +291,17 @@ class Camera1 extends CameraController { return false; } + + @Override + void setAudio(Audio audio) { + if (mAudio != audio) { + if (mIsCapturingVideo) { + LOG.w("Changing audio mode while recording. Changes will take place starting from next video"); + } + mAudio = audio; + } + } + @Override void setFlash(Flash flash) { Flash old = mFlash; @@ -592,13 +603,25 @@ class Camera1 extends CameraController { mMediaRecorder.setCamera(mCamera); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); - mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); + + CamcorderProfile profile = getCamcorderProfile(mVideoQuality); + if (mAudio == Audio.ON) { + mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); + mMediaRecorder.setProfile(profile); + } else { + // Set all values contained in profile except audio settings + mMediaRecorder.setOutputFormat(profile.fileFormat); + mMediaRecorder.setVideoEncoder(profile.videoCodec); + mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate); + mMediaRecorder.setVideoFrameRate(profile.videoFrameRate); + mMediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight); + } + if (mLocation != null) { mMediaRecorder.setLocation((float) mLocation.getLatitude(), (float) mLocation.getLongitude()); } - mMediaRecorder.setProfile(getCamcorderProfile(mVideoQuality)); mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath()); mMediaRecorder.setOrientationHint(computeExifRotation()); // Not needed. mMediaRecorder.setPreviewDisplay(mPreview.getSurface()); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java index 18a2d487..58190e85 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java @@ -151,6 +151,11 @@ class Camera2 extends CameraController { } + @Override + void setAudio(Audio audio) { + + } + @Override void setLocation(Location location) { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 0432fcbc..f50fe27b 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -19,6 +19,7 @@ abstract class CameraController implements Preview.SurfaceCallback { protected VideoQuality mVideoQuality; protected SessionType mSessionType; protected Hdr mHdr; + protected Audio mAudio; protected Size mCaptureSize; protected Size mPreviewSize; @@ -101,6 +102,8 @@ abstract class CameraController implements Preview.SurfaceCallback { abstract void setHdr(Hdr hdr); + abstract void setAudio(Audio audio); + abstract void setLocation(Location location); //endregion @@ -160,6 +163,10 @@ abstract class CameraController implements Preview.SurfaceCallback { return mHdr; } + final Audio getAudio() { + return mAudio; + } + final Size getCaptureSize() { return mCaptureSize; } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index e6c22a26..1b6506e7 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -103,6 +103,7 @@ public class CameraView extends FrameLayout { 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())); Hdr hdr = Hdr.fromValue(a.getInteger(R.styleable.CameraView_cameraHdr, Hdr.DEFAULT.value())); + Audio audio = Audio.fromValue(a.getInteger(R.styleable.CameraView_cameraAudio, Audio.DEFAULT.value())); // Gestures GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, GestureAction.DEFAULT_TAP.value())); @@ -143,6 +144,7 @@ public class CameraView extends FrameLayout { setWhiteBalance(whiteBalance); setGrid(grid); setHdr(hdr); + setAudio(audio); // Apply gestures mapGesture(Gesture.TAP, tapGesture); @@ -865,6 +867,28 @@ public class CameraView extends FrameLayout { } + /** + * Controls the audio + * + * @see Audio#OFF + * @see Audio#ON + * + * @param audio desired audio value + */ + public void setAudio(Audio audio) { + mCameraController.setAudio(audio); + } + + + /** + * Gets the current audio value. + * @return the current audio value + */ + public Audio getAudio() { + return mCameraController.getAudio(); + } + + /** * Starts an autofocus process at the given coordinates, with respect * to the view width and height. diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/Audio.java b/cameraview/src/main/options/com/otaliastudios/cameraview/Audio.java new file mode 100644 index 00000000..e1f4f6af --- /dev/null +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/Audio.java @@ -0,0 +1,42 @@ +package com.otaliastudios.cameraview; + + +/** + * Audio values indicate whether to record audio stream when record video. + * + * @see CameraView#setAudio(Audio) + */ +public enum Audio { + + /** + * No Audio. + */ + OFF(0), + + /** + * With Audio. + */ + ON(1); + + final static Audio DEFAULT = ON; + + private int value; + + Audio(int value) { + this.value = value; + } + + int value() { + return value; + } + + static Audio fromValue(int value) { + Audio[] list = Audio.values(); + for (Audio action : list) { + if (action.value() == value) { + return action; + } + } + return null; + } +} diff --git a/cameraview/src/main/res/values/attrs.xml b/cameraview/src/main/res/values/attrs.xml index b2f41d80..30050a1e 100644 --- a/cameraview/src/main/res/values/attrs.xml +++ b/cameraview/src/main/res/values/attrs.xml @@ -91,6 +91,11 @@ + + + + +