Results 1 to 3 of 3
  1. #1
    Saran185 is offline Member
    Join Date
    Jan 2011
    Rep Power

    Question duration of the audio 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..
    Java Code:
    // 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 {; 
                    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 = != -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"); }

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Rep Power


    Maybe an exception is being thrown when or before the Player is instantiated (and thus it will remain null), in which case the check in close() will use lastPosition instead, and lastPosition is never set to anything other than 0.

    If that's not it, then I would check the documentation for Player.

  3. #3
    Saran185 is offline Member
    Join Date
    Jan 2011
    Rep Power


    i am not getting any exception.. and i think the close() function works fine..
    pls help me in finding the duration of the audio file..

Similar Threads

  1. Play Audio File
    By YapperNico in forum New To Java
    Replies: 0
    Last Post: 12-30-2010, 02:59 PM
  2. Get Duration of mov ,wmv files
    By saurabh208 in forum New To Java
    Replies: 7
    Last Post: 06-18-2010, 04:42 PM
  3. Is it possible to insert recorded audio data into an existing file?
    By evan_earnest in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-31-2010, 04:40 AM
  4. Replies: 4
    Last Post: 01-21-2010, 10:31 AM
  5. How to calculate the sample rate of an audio file?
    By arnab321 in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-28-2008, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts