How to get the duration of any audio format

hi,

I got stuck once again..

I want to get the duration of any format of song I am playing.. I am using a jtable to list the songs that will be played. The four columns of the jTable are

1. TitleTitle of the song

2. AlbumDirectory that the song exist

3. Length - the duration of the song (Does not work)

4. Bitrate - The bit rate of the songs (Dont know to do)

I have tried the following code to get the length, If I try that code, the player does not add the file to the jTable itself.., but without that code it works properlyy...

privatevoid ToolAddMediaActionPerformed(java.awt.event.ActionEvent evt){

// TODO add your handling code here:

JFileChooser chooser =new JFileChooser();

FileNameExtensionFilter filter =new FileNameExtensionFilter("MP3, WAV & JMP","mp3","wav","jmp");

chooser.setFileFilter(filter);

chooser.setAcceptAllFileFilterUsed(false);

chooser.setCurrentDirectory(new File("Z:/Jmedia/important"));

int returnVal = chooser.showOpenDialog(this);

File file = chooser.getSelectedFile();

try{

FileInputStream fis =new FileInputStream(file);

}catch (FileNotFoundException ex){

ex.printStackTrace();

}

Time len=p.getMediaTime();//to get the media time, also tried getDuration()

//System.out.println(len.getSeconds()/60);

if(returnVal == JFileChooser.APPROVE_OPTION){

System.out.println("You chose to open this file: " +

chooser.getSelectedFile().getAbsolutePath());

if(file.getName().endsWith(".jmp")){

try{

BufferedReader br=new BufferedReader(new FileReader(file));

String str="";

try{

while((str=br.readLine())!=null){

int tfname = str.lastIndexOf(File.separatorChar);

String fname = str.substring(tfname + 1);

if(fname.endsWith(".mp3")|| fname.endsWith(".wav")){

((DefaultTableModel)jTable1.getModel()).addRow(new String[]{fname, str,"",""});

((DefaultTableModel)jTable2.getModel()).addRow(new String[]{fname, str,"",""});

}

}

}catch (IOException ex){

ex.printStackTrace();

}

}catch (FileNotFoundException ex){

ex.printStackTrace();

}

}elseif(file.getName().endsWith(".mp3")|| file.getName().endsWith(".wav")){

((DefaultTableModel)jTable1.getModel()).addRow(new String[]{chooser.getSelectedFile().getName(), chooser.getSelectedFile().getAbsolutePath(),""+len,""});

((DefaultTableModel)jTable2.getModel()).addRow(new String[]{chooser.getSelectedFile().getName(), chooser.getSelectedFile().getAbsolutePath(),""+len,""});

}

}

}

[5066 byte] By [Spaniana] at [2007-11-26 21:52:12]
# 1
You have to use getDuration()
qUesT_foR_knOwLeDgea at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 2
if u try to mean that I have to use getduration() in the place of getMediaTime(),that too doesn't work..,Wait for a while.., I will try one more thing and inform u
Spaniana at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 3
Or else you can even get the file size and then divide it by it's bit rate to get the duration.
qUesT_foR_knOwLeDgea at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 4

If I use getDuration() in the place of get media time.. It too does not display anything in the jTable.., Also it displays the following error..

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at jmedia.NewJFrame.ToolAddMediaActionPerformed(NewJFrame.java:1106)

at jmedia.NewJFrame.access$000(NewJFrame.java:42)

at jmedia.NewJFrame$2.actionPerformed(NewJFrame.java:186)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:377)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:232)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)

at java.awt.Component.processMouseEvent(Component.java:5999)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3240)

at java.awt.Component.processEvent(Component.java:5764)

at java.awt.Container.processEvent(Container.java:1984)

at java.awt.Component.dispatchEventImpl(Component.java:4407)

at java.awt.Container.dispatchEventImpl(Container.java:2042)

at java.awt.Component.dispatchEvent(Component.java:4237)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4248)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3912)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3842)

at java.awt.Container.dispatchEventImpl(Container.java:2028)

at java.awt.Window.dispatchEventImpl(Window.java:2405)

at java.awt.Component.dispatchEvent(Component.java:4237)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:600)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Spaniana at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 5
Which means the player has not reached a state where it can determine the streams duration. If it is unable to determine the duration it would return Duration.DURATION_UNKNOWN reference.
qUesT_foR_knOwLeDgea at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 6
see, if the dusratiion is unknown, then it should print in the jtable that the duration is unknown right..,bcoz of this I am not able to proceed with the seek bar toooo
Spaniana at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 7
It's working for me. I will tell you how to do it in 5 minutes.
qUesT_foR_knOwLeDgea at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 8
hey, also if am trying to just print the duration in the console, it doesnot work...It gives the same big error
Spaniana at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 9
also have u tried it with the same code what I have posted
Spaniana at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 10

> see, if the dusratiion is unknown, then it should

> print in the jtable that the duration is unknown

No it shouldn't. DURATION_UNKNOWN is a static field of the type Time in the Duration interface

> right..,

>

> bcoz of this I am not able to proceed with the seek

> bar toooo

You have to call getVisualComponent( ) on the Player to get the seek bar.

qUesT_foR_knOwLeDgea at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 11

It doesn't. Run this code. Set the path to some .mp3 in your system.

import javax.media.*;

import java.io.*;

import java.net.*;

public class JMF2

{

public static void main(String args[]) throws Exception

{

Player player=Manager.createRealizedPlayer(new URL("file:///I:/January 26th 2006/Sum41/All Killer No Filler/02 Sum 41 - Nothing on my Back.mp3"));

System.out.println("Before start()"+player.getDuration().getSeconds());

player.start();

System.out.println("After start()"+player.getDuration().getSeconds());

}

}

Message was edited by:

qUesT_foR_knOwLeDge

qUesT_foR_knOwLeDgea at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 12
As you see after i call getDuration( ) i get the file duration, but one thing you have to observe is that the player is realized.
qUesT_foR_knOwLeDgea at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 13
You are getting NullPointerException because you haven't even created a Player and you are calling getMediaTime( )
qUesT_foR_knOwLeDgea at 2007-7-10 3:45:59 > top of Java-index,Security,Cryptography...
# 14
You are getting NullPointerException because you haven't even created a Player and you are calling getMediaTime( )
qUesT_foR_knOwLeDgea at 2007-7-10 3:46:00 > top of Java-index,Security,Cryptography...
# 15
hey,In the given code P is the player and I have tried it with the following code too.,,p = Manager.createPlayer(mediaURL);and with the following code too...p = Manager.createRealizedPlayer(mediaURL);both it gives the same issue..
Spaniana at 2007-7-21 18:28:12 > top of Java-index,Security,Cryptography...
# 16

> hey,

> In the given code P is the player and I have tried it

> with the following code too.,,

>

> p = Manager.createPlayer(mediaURL);

>

> and with the following code too...

>

> p = Manager.createRealizedPlayer(mediaURL);

>

> both it gives the same issue..

Look at this part of the code you posted in this thread

Time len=p.getMediaTime();//to get the media time, also tried getDuration()

//System.out.println(len.getSeconds()/60);

if(returnVal == JFileChooser.APPROVE_OPTION) {

System.out.println("You chose to open this file: " +

chooser.getSelectedFile().getAbsolutePath());

Here you see that you still haven't loaded the playlist so obviously you still haven't selected the file to play. But you are calling p.getMediaTime( ), which results in a NullPointerException.

qUesT_foR_knOwLeDgea at 2007-7-21 18:28:12 > top of Java-index,Security,Cryptography...
# 17
It doesn't work man..,I tried it to print in the console immediately after the p.start() method get invokes..,the old big error was gone, but it does not print...Also can u tell me the difference between createPlayer() and createRealizedPlayer()
Spaniana at 2007-7-21 18:28:12 > top of Java-index,Security,Cryptography...
# 18
yesss..i got it to print in the console after making it as createRealizedPlayer()but now how to add it to the jTable...that too in the length column
Spaniana at 2007-7-21 18:28:12 > top of Java-index,Security,Cryptography...
# 19
hey Quest,,Thats working now properly and I could display the length in jTable tooo.. Can u help in adding the Bit rate..,, to the jTable
Spaniana at 2007-7-21 18:28:12 > top of Java-index,Security,Cryptography...
# 20

> hey Quest,,

> Thats working now properly and I could display the

> length in jTable tooo.. Can u help in adding the Bit

> rate..,, to the jTable

Fine. Right now i am a bit busy. When i am free i will get back to the forum later. Meanwhile, others here can help you with your questions.

And also note that if you have received the solution for a particular question i sugget you start a new thread for your next question ( As always suggested at the forum ) for this reason

-Any new member who has just come to the forum to find the solution to a problem can search the forum and if a solution is already present he can go through it than again post his question. Just a general suggsetion in the interest of the forum.

null

qUesT_foR_knOwLeDgea at 2007-7-21 18:28:12 > top of Java-index,Security,Cryptography...