簡體   English   中英

Android Media文件長度

[英]Android Media File Length

我需要能夠讀取一組文件並弄清它們的長度。 例如,我有10個ogg文件,每個文件的大小從30秒到3分鍾不等。 根據用戶的選擇,我可能會使用1到10個文件,並希望根據播放的每個文件顯示剩余時間的倒計時。 因此,如果選擇文件1、2和3,則剩余時間可能是7分鍾,但是如果選擇4 5和6,則剩余4分鍾,依此類推。

因此,我需要能夠讀取文件並確定其長度。

如果您可以將文件命名為file0.ogg,file1.ogg等,則可以使用。

//create an array to store the duration of each file.
int[] durations = new int[10];

//create a MediaPlayer
MediaPlayer mp =  new MediaPlayer();
for(int i = 0;i<durations.length;i++);
    try {
        //for each file, load it into MediaPlayer, get the file's duration, and then reset the MediaPlayer
        mp.setDataSource("file"+i+".ogg");
        mp.prepare();
        durations[i] = mp.getDuration();
        mp.reset();
        }
    catch(IOException ioe) {}
 }

暫無
暫無

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

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