我正在使用Media recorder编写视频录制功能的代码。但是它会抛出Io Exception,非法状态异常我在谷歌搜索了很多次但没有正确解释这个我的代码是
public class Simple extends Activity {
/** Called when the activity is first created. */
Button button,button2;
MediaRecorder recorder;
Camera camera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
camera=Camera.open();
Camera.Parameters params = camera.getParameters();
camera.setParameters(params);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Video Start", Toast.LENGTH_LONG).show();
String mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/temp.3mp4";
recorder=new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoFrameRate(10);
recorder.setVideoSize(200, 300);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setMaxDuration(50000);
recorder.setMaxFileSize(5000000);
recorder.setOutputFile(mFileName);
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(),e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(),e.toString(), Toast.LENGTH_LONG).show();
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Video Stop", Toast.LENGTH_LONG).show();
recorder.stop();
}
});
}
}
答案 0 :(得分:1)
以下是录制视频的代码......
recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mPreview = new Preview(VideoRecorder.this,recorder);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(mPreview);
recorder.start();
这是预览类....
class Preview extends SurfaceView implements SurfaceHolder.Callback
{
SurfaceHolder mHolder;
MediaRecorder tempRecorder;
public Preview(Context context,MediaRecorder recorder) {
super(context);
tempRecorder=recorder;
mHolder=getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// TODO Auto-generated constructor stub
}
public Surface getSurface()
{
return mHolder.getSurface();
}
public void surfaceCreated(SurfaceHolder holder){
tempRecorder.setOutputFile("/sdcard/recordvideooutput.3gpp");
tempRecorder.setPreviewDisplay(mHolder.getSurface());
try{
tempRecorder.prepare();
} catch (Exception e) {
String message = e.getMessage();
tempRecorder.release();
tempRecorder = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
if(tempRecorder!=null)
{
tempRecorder.release();
tempRecorder = null;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
}
}