r533 - in trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing: . flight/bar
Author: bpoussin Date: 2012-09-05 18:32:30 +0200 (Wed, 05 Sep 2012) New Revision: 533 Url: http://forge.codelutin.com/repositories/revision/sammoa/533 Log: ajout des heures sur le slide du reader audio Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/AudioCheck.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/bar/SoundPlayer.java Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/AudioCheck.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/AudioCheck.java 2012-09-05 16:25:45 UTC (rev 532) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/AudioCheck.java 2012-09-05 16:32:30 UTC (rev 533) @@ -12,6 +12,7 @@ import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; +import java.util.Date; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; import javax.swing.BorderFactory; @@ -170,7 +171,7 @@ public void actionPerformed( ActionEvent e){ File file = new File(filenameField.getText()); - playerPanel.loadFile(file); + playerPanel.getAudioReader().load(file, new Date()); } }); } Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/bar/SoundPlayer.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/bar/SoundPlayer.java 2012-09-05 16:25:45 UTC (rev 532) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/bar/SoundPlayer.java 2012-09-05 16:32:30 UTC (rev 533) @@ -24,6 +24,11 @@ import java.awt.event.MouseEvent; import java.io.File; import java.io.IOException; +import java.util.Date; +import java.util.Hashtable; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateUtils; +import org.apache.commons.lang3.time.FastDateFormat; /** * Composant qui permet de charge un fichier audio et de le jouer. On peut se @@ -44,13 +49,17 @@ */ public class SoundPlayer extends JComponent implements DeviceStateListener, AudioPositionListener { protected AudioReader reader; - protected boolean playing = false; // whether the sound is currently playing +// protected boolean playing = false; // whether the sound is currently playing + // le fichier charge acutellement au niveau de l'ui (config du slider + protected File currentFile; // The following fields are for the GUI - protected JButton play; // The Play/Stop button + protected JButton play; // The Play button + protected JButton stop; // The Stop button protected JSlider progress; // Shows and sets current position in sound protected JLabel time; // Displays audioPosition as a number protected JLabel maxTime; + protected int timePad; // le nombre de caratere a ajouter pour le temps // Create a SoundPlayer component for the specified file. public SoundPlayer() { @@ -64,19 +73,22 @@ protected void initUI() { // Now create the basic GUI - play = new JButton("Play"); // Play/stop button - progress = new JSlider(0, 0, 0); // Shows position in sound - progress.setMajorTickSpacing(60000); - progress.setMinorTickSpacing(10000); + play = new JButton("Play"); + play.setEnabled(false); + stop = new JButton("Stop"); + stop.setEnabled(false); + + progress = new JSlider(0, 0, 0); progress.setPaintTicks(true); progress.setPaintLabels(true); - + time = new JLabel("0"); // Shows position as a # maxTime = new JLabel("/0"); // Shows position as a # // put those controls in a row Box row = Box.createHorizontalBox( ); row.add(play); + row.add(stop); row.add(progress); row.add(time); row.add(maxTime); @@ -90,20 +102,23 @@ // When clicked, start or stop playing the sound play.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent e) { - if (playing) { - stop( ); - } else { - play( ); - } + play(); } }); + // When clicked, start or stop playing the sound + stop.addActionListener(new ActionListener( ) { + public void actionPerformed(ActionEvent e) { + stop(); + } + }); + progress.addMouseListener(new MouseAdapter() { boolean inPlay = false; @Override public void mousePressed(MouseEvent e) { - inPlay= playing; + inPlay= getAudioReader().getState() == DeviceState.RUNNING; if (inPlay) { reader.stop(); } @@ -113,7 +128,7 @@ public void mouseReleased(MouseEvent e) { int value = progress.getValue( ); // Update the time label - time.setText(value/1000 + "." + (value%1000)/100); +// time.setText(value/1000 + "." + (value%1000)/100); // If we're not already there, skip there. if (value >= 0 && value <= reader.getLength()) { reader.setPosition(value); @@ -128,8 +143,11 @@ // Whenever the slider value changes, update the time label. progress.addChangeListener(new ChangeListener( ) { public void stateChanged(ChangeEvent e) { + // changement du text de position int value = progress.getValue( ); - time.setText(value/1000 + "." + (value%1000)/100); + String timeString = value/1000 + "." + (value%1000)/100; + timeString = StringUtils.leftPad(timeString, timePad, '0'); + time.setText(timeString); } }); } @@ -137,41 +155,87 @@ @Override public void stateChanged(DeviceStateEvent event) { DeviceState state = event.getNewValue(); - if (state == DeviceState.RUNNING) { - this.playing = true; - play.setText("Stop"); - } else { - this.playing = false; - play.setText("Play"); + switch (state) { + case READY: // on vient de charger un fichier, ou arrete la lecture du fichier + play.setEnabled(true); + stop.setEnabled(false); + changeSliderInfo(); + break; + case RUNNING: // un fichier est en cours de lecture + play.setEnabled(false); + stop.setEnabled(true); + break; + case UNAVAILABLE: // aucun fichier charge + play.setEnabled(false); + stop.setEnabled(false); + break; + case ERROR: // erreur sur le lecture audio (erreur I/O) + play.setEnabled(false); + stop.setEnabled(false); + break; } } + FastDateFormat dateFormat = FastDateFormat.getInstance("HH:mm"); + /** + * On met a la seconde dans le slider + */ + protected void changeSliderInfo() { + Date date = getAudioReader().getRecordingDate(); + long start = date.getTime(); - @Override - public void positionChanged(AudioReader source, long audioPosition) { - progress.setValue((int) audioPosition); + long audioLength = getAudioReader().getLength(); + int max = (int)audioLength; + // si la longueur du morceau change, on change l'info affichee + if (progress.getMaximum() != max) { + progress.setMaximum(max); + + // Affichage du temps + String maxTimeString = (max/1000) + "." + (audioLength % 1000) / 100; + maxTime.setText("/" + maxTimeString); + timePad = maxTimeString.length(); + time.setText(StringUtils.leftPad("0.0", timePad, '0')); + + progress.setMajorTickSpacing(60000); // toutes les 60s + progress.setMinorTickSpacing(10000); // toutes les 10s + + // calcul de l'espace entre chaque affichage de l'heure en fonction de la longueur du slider + double width = progress.getSize().getWidth(); + long step = audioLength * 100 / (long)width; // on minimum tous les 100px + step = Math.max(step, 60000); // on prend pas en dessous de la minute + + //Create the label table + Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>(); + labelTable.put( 0, new JLabel(dateFormat.format(start)) ); + for (int i = (int)step; i+step<max; i+=step) { // on ne met pas l'avant dernier vu qu'on met max ensuite + labelTable.put( i, new JLabel(dateFormat.format(start + i)) ); + } + labelTable.put( max, new JLabel(dateFormat.format(start + audioLength)) ); + progress.setLabelTable( labelTable ); + } } - @Override - public void audioChanged(AudioReader source, long audioLength) { - progress.setMaximum((int) audioLength); - maxTime.setText("/" + audioLength / 1000 + "." + (audioLength % 1000) / 100); + public JButton getPlayButton() { + return play; } - /** - * Charge un nouveau fichier - * @param f le fichier a charger - */ - public void loadFile(File f) { - getAudioReader().load(f, null); + public JButton getStopButton() { + return stop; } + @Override + public void positionChanged(AudioReader source, long audioPosition) { + // changement de la position du slider + int pos = (int)audioPosition; + progress.setValue(pos); + } + public void setReader(AudioReader reader) { if (this.reader != null) { this.reader.removeAudioPositionListener(this); this.reader.removeDeviceStateListener(this); } this.reader = reader; - if (reader != null) { + if (this.reader != null) { reader.addDeviceStateListener(this); reader.addAudioPositionListener(this); } @@ -186,23 +250,31 @@ return reader; } - /** - * Donne la position actuelle de la lecture en milliseconde - * @return - */ - public long getPosition() { - return getAudioReader().getPosition(); - } +// /** +// * Charge un nouveau fichier +// * @param f le fichier a charger +// */ +// public void loadFile(File f) { +// getAudioReader().load(f); +// } +// +// /** +// * Donne la position actuelle de la lecture en milliseconde +// * @return +// */ +// public long getPosition() { +// return getAudioReader().getPosition(); +// } +// +// /** +// * Donne la longueur total du fichier en milliseconde +// * @return +// */ +// public long getLength() { +// return getAudioReader().getLength(); +// } /** - * Donne la longueur total du fichier en milliseconde - * @return - */ - public long getLength() { - return getAudioReader().getLength(); - } - - /** * Start playing the sound at the current position */ public void play() { @@ -232,7 +304,7 @@ player = new SoundPlayer(); AudioReader reader = new SammoaAudioReader(); player.setReader(reader); - player.loadFile(file); + reader.load(file, new Date()); // Put it in a window and play it JFrame f = new JFrame("SoundPlayer"); @@ -241,4 +313,8 @@ f.pack(); f.setVisible(true); } + + public void audioChanged(AudioReader source, long audioLength) { + // A SUPPRIMER + } } \ No newline at end of file
participants (1)
-
bpoussin@users.forge.codelutin.com