gpt4 book ai didi

java - MediaRecorder 不保存文件

转载 作者:行者123 更新时间:2023-11-30 02:08:41 28 4
gpt4 key购买 nike

我查看了其他人遇到的这个问题,但没有找到合适的解决方案。像他们一样,我遵循了相机功能教程:http://developer.android.com/guide/topics/media/camera.html .下面列出的所有内容都非常完美,以至于我认为该程序已按照我的预期录制了视频。然而,在查看画廊中的视频时,它并没有出现。我很困惑,因为在连接 USB 调试时没有出现 IOExceptions 或其他错误。奇怪的是,在移除 USB 并再次插入时,无论是立即还是在未来几天的某个时候,所有以前录制的视频都会出现在图库中。显然我遗漏了一些东西,或者我不知道录制视频的某些方面。非常感谢任何帮助或指导,谢谢。

相关代码如下,有需要的我再贴上。

相机 Activity :

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);

mCamera = MainActivity.getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
button_capture = (Button) findViewById(R.id.button_capture);
button_capture.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from MediaRecorder

// inform the user that recording has stopped
button_capture.setText("Capture");
isRecording = false;
} else {
// initialize video camera
if (prepareVideoRecorder()) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mMediaRecorder.start();

// inform the user that recording has started
button_capture.setText("Stop");
isRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
});
}

private boolean prepareVideoRecorder(){

mMediaRecorder = new MediaRecorder();

// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);

// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

// Step 4: Set output file
mMediaRecorder.setOutputFile(MediaCapture.getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}

媒体捕捉:

public static Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(MEDIA_TYPE_VIDEO));
}

/**
* Create a File for saving an image or video
*/
public static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES), "MyApplication");

// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.

// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyApplication", "failed to create directory");
return null;
}
}

// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HH:mm:ss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
}

最佳答案

我也对这种行为感到非常困惑,直到我意识到拔下 USB 导致文件出现。这似乎是 Android 中的一个错误,可能与此有关:

https://code.google.com/p/android/issues/detail?id=195362

很明显,文件已经正确写入,但由于某些原因,新文件不可见。无论如何,上面链接中提供的解决方案/解决方法是强制对您的文件进行媒体扫描。例如

MediaScannerConnection.scanFile(getContext(), new String[]{this.mediaFile.getAbsolutePath()}, null, null);

“mediaFile”是您的 mediaRecorder 刚刚完成写入的文件。有了这个,我不需要拔下 USB 电缆来查看文件,它会在录制完成后立即出现。

我在三星 Galaxy A5 上运行 Android 5.0.2。这感觉更像是一种解决方法而不是修复,我不能确定它是否适用于其他设备或 Android 版本,但我希望它对某人有所帮助。

关于java - MediaRecorder 不保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30411435/

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