duration of mp3 file

hi ..
I want to get the duration of the mp3 audio file in java.
I ve my code below..
if i give a txt file as input my program converts it to ".mp3" file and plays the mp3 file. in the java bin i have both the txt and the corresponding audio(mp3) file for the txt doc. now i also want to display the duration of the mp3 audio file that is played. I ve written code for it. but it always gives the duartion as 0.
kindly help me..
import java.io.BufferedInputStream;
import java.io.*;
import java.io.FileInputStream;
// import javax.sound.sampled;
import java.lang.Object;
import javazoom.jl.player.Player;
import javazoom.jl.player.JavaSoundAudioDevice;

public class MP3 {
private String filename;
private Player player;
private int lastPosition = 0;
// private AudioDevice audio;
// constructor that takes the name of an MP3 file
public MP3(String filename) {
this.filename = filename;

public void close() { if (player != null) player.close(); }
public int getPosition()
int i = this.lastPosition;
//AudioDevice localAudioDevice = this.filename;
if (player != null)
i = player.getPosition();}
System.out.println("duration : "+i);
return i;
// play the MP3 file to the sound card
public void play() {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
catch (Exception e) {
System.out.println("Problem playing file " + filename);

// run in new thread to play in background
new Thread() {
public void run() {
try { player.play();

catch (Exception e) { System.out.println(e); }


// test client
public static void main(String[] args) {
File fn = new File(args[0]);

String targetExtension=".mp3";
if(args.length >= 1 )
String ext =args[0].substring(args[0].indexOf("."));
File f = new File(args[0]);
//if the file exists
// then change the filename
FileInputStream fstream = new FileInputStream(fn);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(
new InputStreamReader(in));

// Read File by char
char[] buffer = new char[4096];
int len=0;
while((len = br.read(buffer)) != -1) { // Read a batch of chars
String s = new String(buffer, 0, len);

System.out.println(" "+args[0]);
//File filename = new File(args[0]);
// f.renameTo(filename);
String filename = args[0];

MP3 mp3 = new MP3(filename);

// when the computation is done, stop playing it

mp3 = new MP3(filename);

} catch(IOException e)
{System.out.println(" exception here"); }






Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!