I develop an android app which streams video over TokBox. I want to record the streaming video. In order to do this, I tried to use MediaRecorder sample. It did great job on video recording, however I lost my stream. There are two main java classes, just say A and B. The class B implements PreviewCallback
. Here are onPreviewFrame
method. If you are interested in TokBox, the class B extends BaseVideoCapturer
.
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
mPreviewBufferLock.lock();
if (isCaptureRunning) {
if (data.length == mExpectedFrameSize) {
// Get the rotation of the camera
int currentRotation = compensateCameraRotation(mCurrentDisplay
.getRotation());
// Send frame to OpenTok
provideByteArrayFrame(data, NV21, mCaptureWidth,
mCaptureHeight, currentRotation, isFrontCamera());
// Reuse the video buffer
camera.addCallbackBuffer(data);
}
}
mPreviewBufferLock.unlock();
}
The class A is an activity that manages recording. There is a method to start video record and stop after 5 seconds.
public static boolean prepareAndStartMediaRecorder(){
if(CustomVideoCapturer.isCaptureStarted){
// BEGIN_INCLUDE (configure_media_recorder)
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
B.mCamera.unlock();
mediaRecorder.setCamera(B.mCamera);
// Step 2: Set sources
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(profile);
// Step 4: Set output file
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
//configure preview
mediaRecorder.setPreviewDisplay(mPreview.getSurfaceTexture());
// Step 5: Prepare configured MediaRecorder
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
mediaRecorder.start();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mediaRecorder.stop(); // stop the recording
}
}, 5000);
}
After recording is started, onPreviewFrame
method is not called. Do you have any solution about this problem or another approach to record and stream video at the same time?
Edit 1: I tried to apply this solution, but it did not work. If you claim that this is the right solution, please inform me.
Edit 2: The archiving API records the streaming media, so it has noisy sometimes. I need to record frames from camera directly in order to get high-quality video.