簡體   English   中英

如何使用MediaStore獲得特定時長(或小於該時長)的視頻路徑

[英]How to get a specific duration ( or less than that ) video paths using MediaStore

我可以使用MediaStore獲取文件系統中所有視頻的路徑。 有沒有任何方法可以使用MediaStore獲得特定時長(或少於該時長)的視頻。 例如視頻時長少於15分鍾。

這些是我獲取視頻路徑的代碼

public void getVideoPaths(Context context) {
    Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
    String[] projection = {MediaStore.Video.VideoColumns.DATA};
    Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
    if (c != null) {
        while (c.moveToNext()) {
            filesNames.add(c.getString(0));
        }
        c.close();
    }
}
String duration = Integer.toString(15*60*1000);
Cursor c = context.getContentResolver().query(uri, projection, MediaStore.Video.VideoColumns.DURATION + " < ?", new String[] {duration}, null);

這只會查詢少於15分鍾的文件

MediaStore是一個媒體提供程序,其中包含內部和外部存儲設備上所有可用媒體的元數據。 您只需要擁有該視頻文件的uri,就可以將以下代碼用於持續時間

MediaPlayer mp = MediaPlayer.create(this, Uri.parse(uriOfYourFile));
int duration = mp.getDuration();

然后檢查特定的持續時間。 您可以再使用一個選項,如下所述

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//use one of overloaded setDataSource() functions to set your data source
retriever.setDataSource(context, Uri.fromFile(videoFile));
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInMillisec = Long.parseLong(time );

retriever.release()

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM