Mike Eghtebas
asked on
how to read clip property in a directory
Please rename cartcar.pdf (attached) as cartcar.wav to test it with the attached code. This audio clip is about 3.5 seconds. When it plays it to the end, using following lines, it disable Stop button.
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEven t e) {
if (t<=0) stop();
}
}
The value t in millisecond is set at:
public void start() {
t =3650; //<----*********
timer.start();
audioClip.play();
play.setEnabled(false);
stop.setEnabled(true);
loop.setEnabled(true);
}
Above, I have entered t =3650 manually. But it will be good if I could read this property and enter it using some code (see the attached image).
Question: How can I read the length of video in seconds from the property of the clip using code?
Thank you.
property.png
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEven
if (t<=0) stop();
}
}
The value t in millisecond is set at:
public void start() {
t =3650; //<----*********
timer.start();
audioClip.play();
play.setEnabled(false);
stop.setEnabled(true);
loop.setEnabled(true);
}
Above, I have entered t =3650 manually. But it will be good if I could read this property and enter it using some code (see the attached image).
Question: How can I read the length of video in seconds from the property of the clip using code?
Thank you.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class Exercise18_20 extends JApplet {
private JButton play = new JButton("Play");
private JButton loop = new JButton("Loop");
private JButton stop = new JButton("Stop");
private AudioClip audioClip;
private int t =0;
private Timer timer = new Timer(0, new TimerListener());
public Exercise18_20() {
audioClip = Applet.newAudioClip(getClass().getResource("cartcar.wav"));
JPanel pane = new JPanel(new FlowLayout());
JPanel panel = new JPanel();
panel.add(play);
panel.add(loop);
panel.add(stop);
stop.setEnabled(false);
add(panel, BorderLayout.CENTER);
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start();
}
});
loop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loop();
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stop();
}
});
timer.start();
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println(t--);
if (t<=0) stop();
}
}
public void start() {
t =3650;
timer.start();
audioClip.play();
play.setEnabled(false);
stop.setEnabled(true);
loop.setEnabled(true);
}
public void loop() {
stop();
audioClip.loop();
loop.setEnabled(false);
play.setEnabled(true);
stop.setEnabled(true);
}
public void stop() {
timer.stop();
audioClip.stop();
stop.setEnabled(false);
loop.setEnabled(true);
play.setEnabled(true);
}
public static void main(String[] args) {
Exercise18_20 player = new Exercise18_20();
JFrame frame = new JFrame("Exercise18_20");
Exercise18_20 applet = new Exercise18_20();
applet.init();
frame.getContentPane().add(applet, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(250,75);
frame.setVisible(true);
}
}
cartcar.zipproperty.png
Clip does have this method
http://docs.oracle.com/javase/6/docs/api/javax/sound/sampled/Clip.html#getMicrosecondLength%28%29
the instance of this class:
http://docs.oracle.com/javase/6/docs/api/javax/sound/sampled/AudioFileFormat.html
can be obtained frfom file:
public static AudioFileFormat getAudioFileFormat(File file)
then getProperty("duration") should give the length in microseconds
Now Ill try but I don't have much hope frabnkly
AudioFileFormat aff = AudioSystem.getAudioFileFormat(File file)
System.out.println(aff.getProperty("duration"));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Unfortunately it does not support either .mid or .mp3 file - writes error - usnsupported file type
perhpas we can make it recognize MP3 if download MP3 plugin of the JMF:
http://www.oracle.com/technetwork/java/javase/download-137625.html
perhpas we can make it recognize MP3 if download MP3 plugin of the JMF:
http://www.oracle.com/technetwork/java/javase/download-137625.html
This is printout from your cartcar.wav
so I guess duration would be 71575/22050 = 3.24 sec
m: {}
WAVE (.wav) file, byte length: 71619, data format: PCM_UNSIGNED 22050.0 Hz, 8 bit, mono, 1 bytes/frame, , frame length: 71575
duration: null
so I guess duration would be 71575/22050 = 3.24 sec
so at least for .wav files we can do it
ASKER
I just back. Thank you for all the work. I am using wav if it supports it. I am basically limited to mid or wav (the two I know java supports).
Although you started with c++ (which I know nothing about); but it seems in the last part your solutions are in java.
I am going through your post now.
Although you started with c++ (which I know nothing about); but it seems in the last part your solutions are in java.
I am going through your post now.
yes, c++ is tough way, although it should give more properties and for any kind of windows files
The Java is much more limited for now only to wav's and allows limited properties but at least works for two wav's that i tried
and looks like allows to find the duration
and with my file tada.wav less than 2 sec seems also reasonable though I could not see it in windows properties
The Java is much more limited for now only to wav's and allows limited properties but at least works for two wav's that i tried
and looks like allows to find the duration
and with my file tada.wav less than 2 sec seems also reasonable though I could not see it in windows properties
ASKER
Now I am trying to copy your code piece to incorporate with what I have at:
public void start() {
t =3650; //<----*********
timer.start();
audioClip.play();
play.setEnabled(false);
stop.setEnabled(true);
loop.setEnabled(true);
}
public void start() {
t =3650; //<----*********
timer.start();
audioClip.play();
play.setEnabled(false);
stop.setEnabled(true);
loop.setEnabled(true);
}
ASKER
I guess I need to import some additional package. I am having problem with:
AudioFileFormat aff = AudioSystem.getAudioFileFo rmat(new File("cartcar.wav"));
Map<String, Object> m = aff.properties();
in:
public void start() {
try{
AudioFileFormat aff = AudioSystem.getAudioFileFo rmat(new File("cartcar.wav"));
Map<String, Object> m = aff.properties();
System.out.println("m: " + m);
System.out.println(aff.toS tring());
System.out.println("durati on: " + aff.getProperty("duration" ));
}catch(Exception ex){
ex.printStackTrace();
}
t =3650;
timer.start();
audioClip.play();
play.setEnabled(false);
stop.setEnabled(true);
loop.setEnabled(true);
}
Also, in the to, I have file name entered. It will be nice somehow to use that, see:
public Exercise18_20() {
audioClip = Applet.newAudioClip(getCla ss().getRe source("ca rtcar.wav" ));
JPanel pane = new JPanel(new FlowLayout());
AudioFileFormat aff = AudioSystem.getAudioFileFo
Map<String, Object> m = aff.properties();
in:
public void start() {
try{
AudioFileFormat aff = AudioSystem.getAudioFileFo
Map<String, Object> m = aff.properties();
System.out.println("m: " + m);
System.out.println(aff.toS
System.out.println("durati
}catch(Exception ex){
ex.printStackTrace();
}
t =3650;
timer.start();
audioClip.play();
play.setEnabled(false);
stop.setEnabled(true);
loop.setEnabled(true);
}
Also, in the to, I have file name entered. It will be nice somehow to use that, see:
public Exercise18_20() {
audioClip = Applet.newAudioClip(getCla
JPanel pane = new JPanel(new FlowLayout());
import javax.sound.sampled.AudioF
import javax.sound.sampled.AudioS
but it is part of standard java - my IDE recognized them automatically
ASKER
Thanks, that was good.
Now it is asking for File and Map classes used in
v--******
AudioFileFormat aff = AudioSystem.getAudioFileFo rmat(new File("cartcar.wav"));
Map<String, Object> m = aff.properties();
^---*****
Now it is asking for File and Map classes used in
v--******
AudioFileFormat aff = AudioSystem.getAudioFileFo
Map<String, Object> m = aff.properties();
^---*****
ASKER
it looks like:
AudioFileFormat aff = AudioSystem.getAudioFileFo rmat(getCl ass().getR esource("c artcar.wav "));
working for File, in place of
v--******
AudioFileFormat aff = AudioSystem.getAudioFileFo rmat(new File("cartcar.wav"));
AudioFileFormat aff = AudioSystem.getAudioFileFo
working for File, in place of
v--******
AudioFileFormat aff = AudioSystem.getAudioFileFo
You don't need anything actually about map - just
AudioFileFormat aff = AudioSystem.getAudioFileFo rmat(new File("cartcar.wav"));
and then to parse
aff.toString()
into two numbers and then divide one by anorther to get the seconds
Map contains noithing and is not useful in this case
AudioFileFormat aff = AudioSystem.getAudioFileFo
and then to parse
aff.toString()
into two numbers and then divide one by anorther to get the seconds
Map contains noithing and is not useful in this case
ASKER
Now, method: public void start() works nicely except that I have to enter file name "cartcar.wav" two times. Once at:
public Exercise18_20() {
audioClip = Applet.newAudioClip(getCla ss().getRe source("ca rtcar.wav" ));
and once again at:
public void start() {
try{
// AudioFileFormat aff = AudioSystem.getAudioFileFo rmat(audio Clip)
AudioFileFormat aff = AudioSystem.getAudioFileFo rmat(getCl ass().getR esource("c artcar.wav "));
public Exercise18_20() {
audioClip = Applet.newAudioClip(getCla
and once again at:
public void start() {
try{
// AudioFileFormat aff = AudioSystem.getAudioFileFo
AudioFileFormat aff = AudioSystem.getAudioFileFo
public void start() {
try{
// AudioFileFormat aff = AudioSystem.getAudioFileFormat(audioClip)
AudioFileFormat aff = AudioSystem.getAudioFileFormat(getClass().getResource("cartcar.wav"));
String[] temp = aff.toString().split(" ");
t=(Integer.parseInt(temp[temp.length - 1])/22050)*1000;
timer.start();
audioClip.play();
play.setEnabled(false);
stop.setEnabled(true);
loop.setEnabled(true);
}catch(Exception ex){
ex.printStackTrace();
}
}
You can introduce variable clipURL = getClass().getResource("ca rtcar.wav" );
and use it both times.
It does not actually matter
and use it both times.
It does not actually matter
These are in general windows specific properties.
In this trail
http://www.cplusplus.com/forum/windows/24603/
there is a suggestion how to do it through JNI call to native
C++ fuction and the function itself
This would not be easy though
Open in new window