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

    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 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);
                System.out.println(e);
            }
    
            // run in new thread to play in background
            new Thread() {
                public void run() {
                    try { player.play(); 
    						
    
    				}
                    catch (Exception e) { System.out.println(e); }
                }
            }.start();
    
    
    
    
        }
       
        // test client
        public static void main(String[] args) {
    	  File fn = new File(args[0]);
            
    				String targetExtension=".mp3";  
        if(args.length >= 1 )
          	{//1  
            String ext =args[0].substring(args[0].indexOf("."));  
            System.out.println(ext);  
            if(ext.equalsIgnoreCase(".txt")) 
    		  {//2  
              File f = new File(args[0]);  
              //if the file exists  
              // then change the filename  
              if(f.exists()) 
    		     {//3    
    			 try{
    			  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(s);
    				}
    				
    		   
               args[0]=args[0].replace(ext,targetExtension);  
               System.out.println(" "+args[0]);  
               //File filename = new File(args[0]);  
               // f.renameTo(filename);  
    		   String filename = args[0];
    		   
    
    		   MP3 mp3 = new MP3(filename);
            mp3.play();
      mp3.getPosition();
            
            // when the computation is done, stop playing it
            mp3.close();
    	
    		        mp3 = new MP3(filename);
            mp3.play();
    		//mp3.getPosition();
    		//mp3.getDuration();
            
    
    } catch(IOException e)
      {System.out.println(" exception here"); }
            
                   }//3  
    			   
    			   
               
                }
    		
    		
    		
    		
    		
            
    		}
    
        }
    
    }

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

    Default

    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
    Posts
    36
    Rep Power
    0

    Default

    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, 01:59 PM
  2. Get Duration of mov ,wmv files
    By saurabh208 in forum New To Java
    Replies: 7
    Last Post: 06-18-2010, 03: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, 03:40 AM
  4. Replies: 4
    Last Post: 01-21-2010, 09: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, 12: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
  •