我在Android的编程初学者。
我一直在编写一个应用程序列出文件夹中的所有视频文件,并显示该文件夹中的所有视频信息。但是,当我试图让视频的持续时间就返回null,我找不到的方式得到它。
任何人能帮助我吗?
下面是我的code:
开放的URI = Uri.parse(内容://媒体/外部/视频/媒体/ 9);
光标光标= MediaStore.Video.query(RES,data.getData(),新的String [] {MediaStore.Video.VideoColumns.DURATION});
如果(cursor.moveToFirst()){
串持续时间= cursor.getString(0);
的System.out.println(持续时间:+时间);
}
解决方案
我觉得最简单的方法是:
MediaPlayer的MP = MediaPlayer.create(这一点,Uri.parse(uriOfFile));
INT持续时间= mp.getDuration();
mp.release();
/ *转换米利斯适当的时间* /
返回的String.Format(%D分,%D秒,
TimeUnit.MILLISECONDS.toMinutes(持续时间),
TimeUnit.MILLISECONDS.toSeconds(持续时间) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(持续时间))
);
I'm a beginner in Android programming.
I have been write an application to list all video file in a folder and display information of all videos in the folder. But when i try to get the video duration it return null and i can't find the way to get it.
Any one can help me?
Below is my code:
Uri uri = Uri.parse("content://media/external/video/media/9");
Cursor cursor = MediaStore.Video.query(res, data.getData(), new String[]{MediaStore.Video.VideoColumns.DURATION});
if(cursor.moveToFirst()) {
String duration = cursor.getString(0);
System.out.println("Duration: " + duration);
}
解决方案
I think the easiest way is:
MediaPlayer mp = MediaPlayer.create(this, Uri.parse(uriOfFile));
int duration = mp.getDuration();
mp.release();
/*convert millis to appropriate time*/
return String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(duration),
TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration))
);