我正在使用 MediaRecorder 在我的相机应用中创建视频;在“准备方法”中,我设置了输出文件,recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
而 getOutputMediaFile 方法如下
private void observeVideo(String mediaFilePath){
observer = new FileObserver(mediaFilePath, FileObserver.CLOSE_WRITE) {
@Override
public void onEvent(int event, String path) {
stopWatching();
Toast.makeText(context, path + " saved correctly", Toast.LENGTH_LONG);
}
};
observer.startWatching();
}
private File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
return null;
}
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// 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("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
observeVideo(mediaFile.toString());
} else {
return null;
}
return mediaFile;
}
问题是没有调用 onEvent 。我检查了我传递给 FileObserver 的(录像的)路径是否为空,但不存在这个问题,并且日志显示了正确的视频路径名。为什么?