I'm currently trying to create a very simple video recorder on Android without using intent, just a custom app. My code snippet is following
import java.io.IOException;import android.app.Activity; import android.media.MediaRecorder; import android.net.NetworkInfo; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceVie开发者_C百科w;
public class CameraActivity extends Activity implements SurfaceHolder.Callback { public enum State { UNKNOWN, CONNECTED, NOT_CONNECTED }
SurfaceView cameraView; SurfaceHolder holder; public boolean mListening; public Object mState; public NetworkInfo mNetworkInfo; public boolean mSending; boolean recording = false; public static final String TAG = "VIDEOCAPTURE"; private MediaRecorder recorder; protected void captureVideo() { if (recording) { recorder.stop(); // recorder.release(); recording = false; Log.v(TAG, "Recording Stopped"); // Let's initRecorder so we can record again initRecorder(); prepareRecorder(); } else { recording = true; recorder.start(); Log.v(TAG, "Recording Started"); } } private void initRecorder() { recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); recorder.setOutputFile("/sdcard/videocapture_example.mp4"); recorder.setMaxDuration(50000); // 50 seconds recorder.setMaxFileSize(5000000); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); recorder = new MediaRecorder(); initRecorder(); setContentView(R.layout.main); cameraView = (SurfaceView) this.findViewById(R.id.CameraView); holder = cameraView.getHolder(); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.addCallback(this); } private void prepareRecorder() { recorder.setPreviewDisplay(holder.getSurface()); try { recorder.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); finish(); } catch (IOException e) { e.printStackTrace(); finish(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { } @Override public void surfaceCreated(SurfaceHolder holder) { Log.v(TAG, "surfaceCreated"); prepareRecorder(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // camera.stopPreview(); // camera.release(); Log.v(TAG, "surfaceDestroyed"); if (recording) { recorder.stop(); recording = false; } recorder.release(); finish(); }
}
But when i deployed this code within emulator, in the LogCat i got some errors like this.
java.lang.RuntimeException: Unable to start activity ComponentInfo...
java.lang.RuntimeException: setMaxDuration failed. at android.media.MediaRecorder.setMaxDuration(Native Method)
What wrong with my code?
Quote from http://developer.android.com/reference/android/media/MediaRecorder.html: "Note: Currently, MediaRecorder does not work on the emulator."
精彩评论