从android应用中的URL获取视频时长 [英] get video duration from URL in android app
问题描述
我正在开发一个应用程序,用户可以看到存储在SERVER上的所有视频信息和标题.我几乎完成了所有工作,除了无论如何编码,我都无法从给定的URL获得视频时长.让我们从互联网上的某个地方观看此演示视频:点击此处获取视频路径我希望该应用无需打开视频本身即可获得视频时长.
I'm working on an app where the user can see all the video's information and title that are stored on a SERVER. I'm almost done with it except that no matter how I code it, I cannot get the video duration from a given URL. Lets take this demo video from somewhere on the internet: CLICK HERE FOR VIDEO PATH I want the app to get the video duration without the need to open the video itself.
我要在android上使用的代码是这样的:
The Code that I'm trying to use on android is this:
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource("https://12-lvl3-pdl.vimeocdn.com/01/1386/0/6932347/10573836.mp4?expires=1461047937&token=037972137fdfc4c2d9902");
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInmillisec = Long.parseLong( time );
long duration = timeInmillisec / 1000;
long hours = duration / 3600;
long minutes = (duration - hours * 3600) / 60;
long seconds = duration - (hours * 3600 + minutes * 60);
Toast.makeText(context,Long.toString(timeInmillisec),Toast.LENGTH_SHORT).show();
但是我得到的结果是:java.lang.IllegalArgumentException在第2行的""retricer.setDataSource()".谁能帮我找到我在做错什么,还是android提供了获取所需信息的另一种方式?
But the result that i'm getting is an: java.lang.IllegalArgumentException at line 2 which is " retriever.setDataSource() ". Can anyone help me find what i'm doing wrong or does android provide another way to get the required information?
推荐答案
也许您正在寻找 FFmpegMediaMetadataRetriever
FFmpegMediaMetadataRetriever 类为从输入媒体文件中检索帧和元数据提供了统一的接口.
FFmpegMediaMetadataRetriever class provides a unified interface for the retrieving frame and metadata from an input media file.
通过使用 FFmpegMediaMetadataRetriever 的 METADATA_KEY_DURATION 常量,您可以获取视频的时长.它将返回字符串给您,然后您可以将其转换为LONG以获取TIME.
By using METADATA_KEY_DURATION constant of FFmpegMediaMetadataRetriever you can get the duration of your video.It will return the string to you then you can convert it into LONG to get TIME.
这是您应该使用的代码:
Here is the code you should use:
FFmpegMediaMetadataRetriever mFFmpegMediaMetadataRetriever = new MediaMetadataRetriever();
mFFmpegMediaMetadataRetriever .setDataSource("Your video url");
String mVideoDuration = mFFmpegMediaMetadataRetriever .extractMetadata(FFmpegMediaMetadataRetriever .METADATA_KEY_DURATION);
long mTimeInMilliseconds= Long.parseLong(mVideoDuration);
如果上述方法仍然无效,请使用
if above still not work then use
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14)
retriever.setDataSource("Your video url", new HashMap<String, String>());
else
retriever.setDataSource("Your video url");
根据您的代码.
希望它会对您有所帮助.祝你好运.
Hope it will help you. Best of luck.
这篇关于从android应用中的URL获取视频时长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!