Added audio on/off feature for video recording (#33)

pull/39/head
Andrew 7 years ago committed by Mattia Iavarone
parent 200816ca84
commit aeb0083352
  1. 13
      README.md
  2. 5
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraController.java
  3. 25
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  4. 5
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java
  5. 7
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java
  6. 24
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  7. 42
      cameraview/src/main/options/com/otaliastudios/cameraview/Audio.java
  8. 5
      cameraview/src/main/res/values/attrs.xml
  9. 1
      demo/src/main/res/layout/activity_main.xml

@ -295,7 +295,8 @@ Most camera parameters can be controlled through XML attributes or linked method
app:cameraJpegQuality="100" app:cameraJpegQuality="100"
app:cameraVideoQuality="480p" app:cameraVideoQuality="480p"
app:cameraWhiteBalance="auto" app:cameraWhiteBalance="auto"
app:cameraHdr="off" /> app:cameraHdr="off"
app:cameraAudio="on"/>
``` ```
|XML Attribute|Method|Values|Default Value| |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`| |[`cameraVideoQuality`](#cameravideoquality)|`setVideoQuality()`|`lowest` `highest` `maxQvga` `max480p` `max720p` `max1080p` `max2160p`|`max480p`|
|[`cameraWhiteBalance`](#camerawhitebalance)|`setWhiteBalance()`|`auto` `incandescent` `fluorescent` `daylight` `cloudy`|`auto`| |[`cameraWhiteBalance`](#camerawhitebalance)|`setWhiteBalance()`|`auto` `incandescent` `fluorescent` `daylight` `cloudy`|`auto`|
|[`cameraHdr`](#camerahdr)|`setHdr()`|`off` `on`|`off`| |[`cameraHdr`](#camerahdr)|`setHdr()`|`off` `on`|`off`|
|[`cameraAudio`](#cameraaudio)|`setAudio()`|`off` `on`|`on`|
#### cameraSessionType #### cameraSessionType
@ -404,6 +406,15 @@ cameraView.setHdr(Hdr.OFF);
cameraView.setHdr(Hdr.ON); cameraView.setHdr(Hdr.ON);
``` ```
#### cameraAudio
Turns on or off audio stream.
```java
cameraView.setAudio(Audio.OFF);
cameraView.setAudio(Audio.ON);
```
## Other APIs ## Other APIs
Other APIs not mentioned above are provided, and are well documented and commented in code. Other APIs not mentioned above are provided, and are well documented and commented in code.

@ -78,6 +78,11 @@ public class MockCameraController extends CameraController {
mHdr = hdr; mHdr = hdr;
} }
@Override
void setAudio(Audio audio) {
mAudio = audio;
}
@Override @Override
void setLocation(Location location) { void setLocation(Location location) {
mLocation = location; mLocation = location;

@ -291,6 +291,17 @@ class Camera1 extends CameraController {
return false; 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 @Override
void setFlash(Flash flash) { void setFlash(Flash flash) {
Flash old = mFlash; Flash old = mFlash;
@ -592,13 +603,25 @@ class Camera1 extends CameraController {
mMediaRecorder.setCamera(mCamera); mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile profile = getCamcorderProfile(mVideoQuality);
if (mAudio == Audio.ON) {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 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) { if (mLocation != null) {
mMediaRecorder.setLocation((float) mLocation.getLatitude(), mMediaRecorder.setLocation((float) mLocation.getLatitude(),
(float) mLocation.getLongitude()); (float) mLocation.getLongitude());
} }
mMediaRecorder.setProfile(getCamcorderProfile(mVideoQuality));
mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath()); mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath());
mMediaRecorder.setOrientationHint(computeExifRotation()); mMediaRecorder.setOrientationHint(computeExifRotation());
// Not needed. mMediaRecorder.setPreviewDisplay(mPreview.getSurface()); // Not needed. mMediaRecorder.setPreviewDisplay(mPreview.getSurface());

@ -151,6 +151,11 @@ class Camera2 extends CameraController {
} }
@Override
void setAudio(Audio audio) {
}
@Override @Override
void setLocation(Location location) { void setLocation(Location location) {

@ -19,6 +19,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
protected VideoQuality mVideoQuality; protected VideoQuality mVideoQuality;
protected SessionType mSessionType; protected SessionType mSessionType;
protected Hdr mHdr; protected Hdr mHdr;
protected Audio mAudio;
protected Size mCaptureSize; protected Size mCaptureSize;
protected Size mPreviewSize; protected Size mPreviewSize;
@ -101,6 +102,8 @@ abstract class CameraController implements Preview.SurfaceCallback {
abstract void setHdr(Hdr hdr); abstract void setHdr(Hdr hdr);
abstract void setAudio(Audio audio);
abstract void setLocation(Location location); abstract void setLocation(Location location);
//endregion //endregion
@ -160,6 +163,10 @@ abstract class CameraController implements Preview.SurfaceCallback {
return mHdr; return mHdr;
} }
final Audio getAudio() {
return mAudio;
}
final Size getCaptureSize() { final Size getCaptureSize() {
return mCaptureSize; return mCaptureSize;
} }

@ -103,6 +103,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()));
Hdr hdr = Hdr.fromValue(a.getInteger(R.styleable.CameraView_cameraHdr, Hdr.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 // 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()));
@ -143,6 +144,7 @@ public class CameraView extends FrameLayout {
setWhiteBalance(whiteBalance); setWhiteBalance(whiteBalance);
setGrid(grid); setGrid(grid);
setHdr(hdr); setHdr(hdr);
setAudio(audio);
// Apply gestures // Apply gestures
mapGesture(Gesture.TAP, tapGesture); 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 * Starts an autofocus process at the given coordinates, with respect
* to the view width and height. * to the view width and height.

@ -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;
}
}

@ -91,6 +91,11 @@
<attr name="cameraCropOutput" format="boolean" /> <attr name="cameraCropOutput" format="boolean" />
<attr name="cameraAudio" format="enum">
<enum name="off" value="0" />
<enum name="on" value="1" />
</attr>
<!-- deprecated attr name="cameraZoomMode" format="enum"> <!-- deprecated attr name="cameraZoomMode" format="enum">
<enum name="off" value="0" /> <enum name="off" value="0" />
<enum name="pinch" value="1" /> <enum name="pinch" value="1" />

@ -30,6 +30,7 @@
app:cameraCropOutput="false" app:cameraCropOutput="false"
app:cameraFacing="back" app:cameraFacing="back"
app:cameraFlash="off" app:cameraFlash="off"
app:cameraAudio="on"
app:cameraGestureTap="focusWithMarker" app:cameraGestureTap="focusWithMarker"
app:cameraGestureLongTap="none" app:cameraGestureLongTap="none"
app:cameraGesturePinch="zoom" app:cameraGesturePinch="zoom"

Loading…
Cancel
Save