Added audio on/off feature for video recording

pull/33/head
Andrii Miroshnychenko 8 years ago committed by Andrew Miroshnychenko
parent 200816ca84
commit 55d874beae
  1. 23
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  2. 5
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java
  3. 7
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java
  4. 24
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  5. 42
      cameraview/src/main/options/com/otaliastudios/cameraview/Audio.java
  6. 5
      cameraview/src/main/res/values/attrs.xml
  7. 1
      demo/src/main/res/layout/activity_main.xml

@ -291,6 +291,15 @@ class Camera1 extends CameraController {
return false; return false;
} }
@Override
void setAudio(Audio audio) {
if (mIsCapturingVideo) {
throw new IllegalStateException("Can't change audio while recording a video.");
}
mAudio = audio;
}
@Override @Override
void setFlash(Flash flash) { void setFlash(Flash flash) {
Flash old = mFlash; Flash old = mFlash;
@ -592,13 +601,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