Kevin Scheidt
Kevin Scheidt

Reputation: 95

Android Media File Length

I need to be able to read a group of files and figure out their length. For example, I have 10 ogg files ranging from 30 seconds to 3 minutes each. Depending on user choices, I may be using between 1 to 10 of the files and would like to display a countdown of the time remaining based on each file playing. So if file 1, 2 and 3 were chosen, then the time remaining might be 7 minutes, but if 4 5 and 6 were chosen, 4 minutes remaining, etc.

So I need to be able to read the files and determine their length.

Upvotes: 0

Views: 445

Answers (1)

geokavel
geokavel

Reputation: 619

If you can name your files as file0.ogg, file1.ogg, etc., this can work.

//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) {}
 }

Upvotes: 1

Related Questions