gpt4 book ai didi

android - MediaRecorder 输出 0 字节文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:49 27 4
gpt4 key购买 nike

我想使用 MediaRecorder 录制视频和音频。

这是工作。

但是当我检查输出文件时。

我无法播放输出的视频文件。

因为,

输出文件大小为 0 字节。

视频时间也是0秒..

请检查我的代码。

public class Talk extends Activity implements SurfaceHolder.Callback{

Context context;


SurfaceView sfv_Preview;
private SurfaceHolder holder;
private MediaRecorder recorder = null;
boolean recording = false;

private static final String OUTPUT_FILE = "/sdcard/videooutput.mp4";
private static final String OUTPUT_FILE_NAME = "videooutput.mp4";
private static final int RECORDING_TIME = 10000;

//Uri fileUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_talk);


recorder = new MediaRecorder();
initRecorder();

sfv_Preview = (SurfaceView) findViewById(R.id.sfv_Preview);


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

context = this;


((ImageButton)findViewById(R.id.ibt_Record)).setOnTouchListener(new OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
recorder.start();
Log.d("recod", "start");
}
else if (MotionEvent.ACTION_UP == event.getAction() || event.getAction() == MotionEvent.ACTION_CANCEL) {
Log.d("recod", "stop");
recorder.stop();
initRecorder();
prepareRecorder();
}
return false;
}
});

}


@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
prepareRecorder();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if (recording) {
recorder.stop();
recording = false;
}
recorder.release();
finish();
}

private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile(OUTPUT_FILE);

recorder.setMaxDuration(RECORDING_TIME);
recorder.setMaxFileSize(10485760); // Approximately 5 megabytes
}

private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());
//recorder.setOrientationHint(90);

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

}

有什么问题?

最佳答案

在您正在执行的代码的 else 部分中:

recorder.stop();

initRecorder(); -->> "问题出在这里"

问题在这里:完成录制后调用方法 initRecorder();

其中有以下代码行

recorder.setOutputFile(OUTPUT_FILE);

它有相同的文件路径。它用新的空文件重写旧文件。

要避免这种情况,请使用时间戳和这样的文件名:

private File getVideoFile() {
File videoDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "VideoList");
// Create parent directory
if (!videoDir.exists()) {
if (!videoDir.mkdirs()) {
Log.d("ZZZ", "failed to create directory");
return null;
}
}

// Create a video file
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File videoFile;
videoFile = new File(videoDir.getPath() + File.separator +
"REC_" + timeStamp + ".mp4");
return videoFile;
}

现在方法 initRecorder() 看起来像这样:

private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
// recorder.setOutputFile(OUTPUT_FILE);//OLD
recorder.setOutputFile(getVideoFile());// NEW

recorder.setMaxDuration(RECORDING_TIME);
recorder.setMaxFileSize(10485760); // Approximately 5 megabytes
}

关于android - MediaRecorder 输出 0 字节文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36722936/

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