get video duration from URL in android app

后端 未结 2 1176
长发绾君心
长发绾君心 2021-01-13 10:02

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 i

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-13 10:12

    Maybe you are looking for the FFmpegMediaMetadataRetriever

    FFmpegMediaMetadataRetriever class provides a unified interface for the retrieving frame and metadata from an input media file.

    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());
    else
     retriever.setDataSource("Your video url");
    

    From your code.

    Hope it will help you. Best of luck.

提交回复
热议问题