package tk.amberide.ide.gui.misc; import tk.amberide.ide.swing.UIUtil; import java.io.File; import java.io.IOException; import tk.amberide.engine.al.Audio; import tk.amberide.engine.al.Audio.State; import tk.amberide.engine.al.AudioIO; import tk.amberide.ide.data.res.Resource; import tk.amberide.ide.os.OS; import tk.amberide.ide.swing.Dialogs; import java.awt.Desktop; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; /** * * @author Tudor */ public class AudioPlayerPanel extends javax.swing.JPanel { private File file; private Audio sound; private javax.swing.Timer timer; private final ImageIcon PLAY_ICON = new ImageIcon(ClassLoader.getSystemResource("icon/Player.Play.png")); private final ImageIcon PAUSE_ICON = new ImageIcon(ClassLoader.getSystemResource("icon/Player.Pause.png")); public AudioPlayerPanel(final Resource<Audio> resource) { initComponents(); sound = resource.get(); file = resource.getSource(); openNativeButton.setIcon(UIUtil.getFileSystemIcon("mid")); timer = new javax.swing.Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { if (sound.getState() == State.STOPPED) { playButton.setIcon(PLAY_ICON); } positionSlider.setValue(getPercentProgress()); } }); positionSlider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { int value = positionSlider.getValue(); if (value != getPercentProgress()) { sound.setMillisecondPosition((int) (sound.getMillisecondLength() * ((double) value / (double) 100))); } } }); } /** * Creates new form AudioPlayerPanel */ public AudioPlayerPanel(File file) throws Exception { this(new Resource<Audio>(AudioIO.read(file), null, file, Resource.AUDIO)); } @Override public void removeNotify() { super.removeNotify(); timer.stop(); sound.stop(); } private int getPercentProgress() { return (int) ((((double) sound.getMillisecondPosition()) / ((double) sound.getMillisecondLength())) * 100); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { playButton = new javax.swing.JButton(); stopButton = new javax.swing.JButton(); openNativeButton = new javax.swing.JButton(); positionSlider = new javax.swing.JSlider(); soundSlider = new javax.swing.JSlider(); setMaximumSize(new java.awt.Dimension(144, 79)); setMinimumSize(new java.awt.Dimension(144, 79)); playButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Player.Play.png"))); // NOI18N playButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { playButtonActionPerformed(evt); } }); stopButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Player.Stop.png"))); // NOI18N stopButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { stopButtonActionPerformed(evt); } }); openNativeButton.setMaximumSize(new java.awt.Dimension(24, 24)); openNativeButton.setMinimumSize(new java.awt.Dimension(24, 24)); openNativeButton.setPreferredSize(new java.awt.Dimension(24, 24)); openNativeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { openNativeButtonActionPerformed(evt); } }); positionSlider.setValue(0); positionSlider.setAutoscrolls(true); soundSlider.setOrientation(javax.swing.JSlider.VERTICAL); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(positionSlider, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(stopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(playButton, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(openNativeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(soundSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 0, 0)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(0, 0, 0) .addComponent(positionSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(playButton, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(stopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(openNativeButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(soundSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 0, 0)) ); }// </editor-fold>//GEN-END:initComponents private void openNativeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openNativeButtonActionPerformed try { if (Desktop.isDesktopSupported()) { try { Desktop.getDesktop().open(file); return; } catch (Exception ignored) { } } switch (OS.getPlatform()) { case WINDOWS: Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file.getAbsolutePath()); return; } } catch (IOException ex) { } Dialogs.errorDialog().setMessage("An error occured. File cannot be opened.").setTitle("Sorry I failed.").show(); }//GEN-LAST:event_openNativeButtonActionPerformed private void playButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playButtonActionPerformed switch (sound.getState()) { case PLAYING: timer.stop(); sound.pause(); playButton.setIcon(PLAY_ICON); break; case STOPPED: positionSlider.setValue(0); sound.start(); timer.start(); playButton.setIcon(PAUSE_ICON); break; case PAUSED: sound.start(); timer.start(); playButton.setIcon(PAUSE_ICON); break; } }//GEN-LAST:event_playButtonActionPerformed private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed timer.stop(); sound.stop(); }//GEN-LAST:event_stopButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton openNativeButton; private javax.swing.JButton playButton; private javax.swing.JSlider positionSlider; private javax.swing.JSlider soundSlider; private javax.swing.JButton stopButton; // End of variables declaration//GEN-END:variables }