gpt4 book ai didi

android - android中的视频录制错误

转载 作者:行者123 更新时间:2023-11-30 01:40:42 27 4
gpt4 key购买 nike

我需要将视频文件录制成 mp4 格式。但是,当我运行并单击录制按钮时,它会抛出类似 FATAL EXCEPTION: main 的错误 java.lang.IllegalStateException 。这是我的全部代码

boolean recording = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_example);
initUI();
initRecorder();
buttonStopVid.setEnabled(false);
buttonPlayVid.setEnabled(false);
buttonStartVid.setOnClickListener(this);

holder = surfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

protected void initUI(){
buttonStartVid = (Button) findViewById(R.id.buttonStartRecordVid);
buttonPlayVid = (Button) findViewById(R.id.buttonPlayVid);
buttonStopVid = (Button) findViewById(R.id.buttonStopRecordVid);
videoView = (VideoView) findViewById(R.id.videoView);
surfaceView = (SurfaceView) findViewById(R.id.surface_camera_final);
}

@Override
public void onClick(View v) {
if(v.getId() == R.id.buttonStartRecordVid){
buttonStartVid.setEnabled(false);
buttonStopVid.setEnabled(true);

surfaceView.setVisibility(View.VISIBLE);
prepareRecorder();
recording = true;
recorder.start();

}
if(v.getId() == R.id.buttonStopRecordVid){
buttonPlayVid.setEnabled(true);
buttonStartVid.setEnabled(true);
buttonStopVid.setEnabled(false);
surfaceView.setVisibility(View.GONE);

recorder.stop();
recording = false;
initRecorder();
prepareRecorder();
}
if(v.getId() == R.id.buttonPlayVid){

}
}

protected void initRecorder(){
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

CamcorderProfile cp = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cp);

try{
File newFile = File.createTempFile("vid", ".mp4", Environment.getExternalStorageDirectory());
recorder.setOutputFile(newFile.getAbsolutePath());
}
catch (IOException e){
e.printStackTrace();
}
}

private void prepareRecorder() {

recorder.setPreviewDisplay(holder.getSurface());

try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
prepareRecorder();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(recording){
recorder.stop();
recording = false;
}
recorder.release();
}

关于可能导致此问题的原因以及如何解决它的任何想法?谢谢!

最佳答案

使用此链接 http://developer.android.com/reference/android/media/MediaRecorder.html

它将提供您需要的完整信息。

关于android - android中的视频录制错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34541985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com