如何通过阅读构建context.getFilesDir记录MediaRecorder为民营应用程序存储目录视频长度()? [英] How to read length of video recorded with MediaRecorder into private app storage directory constructed via context.getFilesDir()?

本文介绍了如何通过阅读构建context.getFilesDir记录MediaRecorder为民营应用程序存储目录视频长度()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题通过阅读使用MediaRecorder与设备的摄像头记录的视频文件的长度/持续时间。该文件记录到应用程序的专用存储目录,这是设置像这样:

I am having a weird issue reading the length/duration of a video file recorded with a device's camera by using MediaRecorder. The file is recorded into the application's private storage directory, which is set like so:

mMediaRecorder.setOutputFile(context.getFilesDir() + "/recordings/webcam.3gpp");

记录完成后,我试图用这些方法来读出的视频的长度:

After recording is complete, I attempt to read the length of the video with these methods:

MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(context.getFilesDir() + "/recordings/webcam.3gpp");
String time = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
return Long.parseLong(time);

方法2:

MediaPlayer mp = MediaPlayer.create(context, Uri.parse(context.getFilesDir() + "/recordings/webcam.3gpp"));
long duration = mp.getDuration();
mp.release();
return duration;

方法的工作无。 mediaMetadataRetriever.extractMetadata 返回null, MediaPlayer.create 失败,并抛出IOException。我已经验证该文件存在。

None of the methods work. mediaMetadataRetriever.extractMetadata returns null and MediaPlayer.create fails with an IOException. I have verified that the file exists.

重要提示:如果我录制保存到/sdcard/recordings/webcam.3gpp这个问题不会发生。出于某种原因,我不能当文件是属于该应用程序的私有文件目录读取时间。此外,该问题只发生在我的三星Droid的充电,它运行Android 2.3的。它不会发生在一个三星Galaxy S4,它运行Android 4.2,和华硕的Nexus 7,运行Android 4.3的。

Important note: This issue DOES NOT happen if I save the recording to "/sdcard/recordings/webcam.3gpp". For some reason, I just cannot read the duration when the file is in the private files directory that belongs to the application. Also, this issue ONLY happens on my Samsung Droid Charge, which runs Android 2.3. It DOES NOT happen on a Samsung Galaxy S4, which runs Android 4.2, and Asus Nexus 7, which runs Android 4.3.

如果我采取相同的文件,并将其复制到SD卡,然后读取它的长度在那里,一切正常。是什么给了?

If I take the same file and copy it to the sdcard, then read the length of it there, everything works. What gives?

copy(new File(context.getFilesDir() + "/recordings/webcam.3gpp"), new File("/sdcard/wtfisthiscrap.3gpp"));
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource("/sdcard/wtfisthiscrap.3gpp");
String time = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
return Long.parseLong(time); // works!

我能做些什么来解决这个问题呢?

What can I do to solve this issue?

推荐答案

我能够通过设置来解决我的问题,一个的FileInputStream 作为数据源的MediaPlayer

I was able to solve my issue by setting a FileInputStream as the data source of MediaPlayer.

MediaPlayer mp = new MediaPlayer();
FileInputStream stream = new FileInputStream(context.getFilesDir() + "/recordings/webcam.3gpp");
mp.setDataSource(stream.getFD());
stream.close();
mp.prepare();
long duration = mp.getDuration();
mp.release();
return duration;

我的答案的来源是 http://stackoverflow.com/a/6383655/379245

这篇关于如何通过阅读构建context.getFilesDir记录MediaRecorder为民营应用程序存储目录视频长度()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆