我目前正试图在Android上创建一个非常简单的录像机而不使用意图,只是一个自定义应用程序。我的代码片段如下:
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.SurfaceView;
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(); }
}
但是当我在模拟器中部署此代码时,在LogCat中我遇到了一些错误。
java.lang.RuntimeException:无法启动活动ComponentInfo ...
java.lang.RuntimeException:setMaxDuration失败。 在android.media.MediaRecorder.setMaxDuration(原生方法)
我的代码出了什么问题?
答案 0 :(得分:0)
来自http://developer.android.com/reference/android/media/MediaRecorder.html的引用: “注意:目前,MediaRecorder无法在模拟器上运行。”