• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package jme3test.audio;
34 
35 import com.jme3.asset.AssetInfo;
36 import com.jme3.asset.AssetLoader;
37 import com.jme3.audio.AudioNode.Status;
38 import com.jme3.audio.*;
39 import com.jme3.audio.plugins.OGGLoader;
40 import com.jme3.audio.plugins.WAVLoader;
41 import com.jme3.system.AppSettings;
42 import com.jme3.system.JmeSystem;
43 import java.io.*;
44 import javax.swing.JFileChooser;
45 
46 public class TestMusicPlayer extends javax.swing.JFrame {
47 
48     private AudioRenderer ar;
49     private AudioData musicData;
50     private AudioNode musicSource;
51     private float musicLength = 0;
52     private float curTime = 0;
53     private Listener listener = new Listener();
54 
TestMusicPlayer()55     public TestMusicPlayer() {
56         initComponents();
57         setLocationRelativeTo(null);
58         initAudioPlayer();
59     }
60 
initAudioPlayer()61     private void initAudioPlayer(){
62         AppSettings settings = new AppSettings(true);
63         settings.setRenderer(null); // disable rendering
64         settings.setAudioRenderer("LWJGL");
65         ar = JmeSystem.newAudioRenderer(settings);
66         ar.initialize();
67         ar.setListener(listener);
68         AudioContext.setAudioRenderer(ar);
69     }
70 
71     /** This method is called from within the constructor to
72      * initialize the form.
73      * WARNING: Do NOT modify this code. The content of this method is
74      * always regenerated by the Form Editor.
75      */
76     @SuppressWarnings("unchecked")
77     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
initComponents()78     private void initComponents() {
79 
80         pnlButtons = new javax.swing.JPanel();
81         sldVolume = new javax.swing.JSlider();
82         btnRewind = new javax.swing.JButton();
83         btnStop = new javax.swing.JButton();
84         btnPlay = new javax.swing.JButton();
85         btnFF = new javax.swing.JButton();
86         btnOpen = new javax.swing.JButton();
87         pnlBar = new javax.swing.JPanel();
88         lblTime = new javax.swing.JLabel();
89         sldBar = new javax.swing.JSlider();
90 
91         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
92         addWindowListener(new java.awt.event.WindowAdapter() {
93             public void windowClosing(java.awt.event.WindowEvent evt) {
94                 formWindowClosing(evt);
95             }
96         });
97 
98         pnlButtons.setLayout(new javax.swing.BoxLayout(pnlButtons, javax.swing.BoxLayout.LINE_AXIS));
99 
100         sldVolume.setMajorTickSpacing(20);
101         sldVolume.setOrientation(javax.swing.JSlider.VERTICAL);
102         sldVolume.setPaintTicks(true);
103         sldVolume.setValue(100);
104         sldVolume.addChangeListener(new javax.swing.event.ChangeListener() {
105             public void stateChanged(javax.swing.event.ChangeEvent evt) {
106                 sldVolumeStateChanged(evt);
107             }
108         });
109         pnlButtons.add(sldVolume);
110 
111         btnRewind.setText("<<");
112         pnlButtons.add(btnRewind);
113 
114         btnStop.setText("[  ]");
115         btnStop.addActionListener(new java.awt.event.ActionListener() {
116             public void actionPerformed(java.awt.event.ActionEvent evt) {
117                 btnStopActionPerformed(evt);
118             }
119         });
120         pnlButtons.add(btnStop);
121 
122         btnPlay.setText("II / >");
123         btnPlay.addActionListener(new java.awt.event.ActionListener() {
124             public void actionPerformed(java.awt.event.ActionEvent evt) {
125                 btnPlayActionPerformed(evt);
126             }
127         });
128         pnlButtons.add(btnPlay);
129 
130         btnFF.setText(">>");
131         btnFF.addActionListener(new java.awt.event.ActionListener() {
132             public void actionPerformed(java.awt.event.ActionEvent evt) {
133                 btnFFActionPerformed(evt);
134             }
135         });
136         pnlButtons.add(btnFF);
137 
138         btnOpen.setText("Open ...");
139         btnOpen.addActionListener(new java.awt.event.ActionListener() {
140             public void actionPerformed(java.awt.event.ActionEvent evt) {
141                 btnOpenActionPerformed(evt);
142             }
143         });
144         pnlButtons.add(btnOpen);
145 
146         getContentPane().add(pnlButtons, java.awt.BorderLayout.CENTER);
147 
148         pnlBar.setLayout(new javax.swing.BoxLayout(pnlBar, javax.swing.BoxLayout.LINE_AXIS));
149 
150         lblTime.setText("0:00-0:00");
151         lblTime.setBorder(javax.swing.BorderFactory.createEmptyBorder(3, 3, 3, 3));
152         pnlBar.add(lblTime);
153 
154         sldBar.setValue(0);
155         sldBar.addChangeListener(new javax.swing.event.ChangeListener() {
156             public void stateChanged(javax.swing.event.ChangeEvent evt) {
157                 sldBarStateChanged(evt);
158             }
159         });
160         pnlBar.add(sldBar);
161 
162         getContentPane().add(pnlBar, java.awt.BorderLayout.PAGE_START);
163 
164         pack();
165     }// </editor-fold>//GEN-END:initComponents
166 
btnOpenActionPerformed(java.awt.event.ActionEvent evt)167     private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenActionPerformed
168         JFileChooser chooser = new JFileChooser();
169         chooser.setAcceptAllFileFilterUsed(true);
170         chooser.setDialogTitle("Select OGG file");
171         chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
172         chooser.setMultiSelectionEnabled(false);
173         if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
174             btnStopActionPerformed(null);
175 
176             final File selected = chooser.getSelectedFile();
177             AssetLoader loader = null;
178             if(selected.getName().endsWith(".wav")){
179                 loader = new WAVLoader();
180             }else{
181                 loader = new OGGLoader();
182             }
183 
184             AudioKey key = new AudioKey(selected.getName(), true, true);
185             try{
186                 musicData = (AudioData) loader.load(new AssetInfo(null, key) {
187                     @Override
188                     public InputStream openStream() {
189                         try{
190                             return new FileInputStream(selected);
191                         }catch (FileNotFoundException ex){
192                             ex.printStackTrace();
193                         }
194                         return null;
195                     }
196                 });
197             }catch (IOException ex){
198                 ex.printStackTrace();
199             }
200 
201             musicSource = new AudioNode(musicData, key);
202             musicLength = musicData.getDuration();
203             updateTime();
204         }
205     }//GEN-LAST:event_btnOpenActionPerformed
206 
updateTime()207     private void updateTime(){
208         int max = (int) (musicLength * 100);
209         int pos = (int) (curTime * 100);
210         sldBar.setMaximum(max);
211         sldBar.setValue(pos);
212 
213         int minutesTotal = (int) (musicLength / 60);
214         int secondsTotal = (int) (musicLength % 60);
215         int minutesNow = (int) (curTime / 60);
216         int secondsNow = (int) (curTime % 60);
217         String txt = String.format("%01d:%02d-%01d:%02d", minutesNow, secondsNow,
218                                                       minutesTotal, secondsTotal);
219         lblTime.setText(txt);
220     }
221 
btnPlayActionPerformed(java.awt.event.ActionEvent evt)222     private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlayActionPerformed
223         if (musicSource == null){
224             btnOpenActionPerformed(evt);
225             return;
226         }
227 
228         if (musicSource.getStatus() == Status.Playing){
229             musicSource.setPitch(1);
230             ar.pauseSource(musicSource);
231         }else{
232             musicSource.setPitch(1);
233             musicSource.play();
234         }
235     }//GEN-LAST:event_btnPlayActionPerformed
236 
formWindowClosing(java.awt.event.WindowEvent evt)237     private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
238         ar.cleanup();
239     }//GEN-LAST:event_formWindowClosing
240 
sldVolumeStateChanged(javax.swing.event.ChangeEvent evt)241     private void sldVolumeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldVolumeStateChanged
242        listener.setVolume( (float) sldVolume.getValue() / 100f);
243        ar.setListener(listener);
244     }//GEN-LAST:event_sldVolumeStateChanged
245 
btnStopActionPerformed(java.awt.event.ActionEvent evt)246     private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
247         if (musicSource != null){
248             musicSource.setPitch(1);
249             ar.stopSource(musicSource);
250         }
251     }//GEN-LAST:event_btnStopActionPerformed
252 
btnFFActionPerformed(java.awt.event.ActionEvent evt)253     private void btnFFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFFActionPerformed
254         if (musicSource.getStatus() == Status.Playing){
255             musicSource.setPitch(2);
256         }
257     }//GEN-LAST:event_btnFFActionPerformed
258 
sldBarStateChanged(javax.swing.event.ChangeEvent evt)259     private void sldBarStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldBarStateChanged
260         if (musicSource != null && !sldBar.getValueIsAdjusting()){
261             curTime = sldBar.getValue() / 100f;
262             if (curTime < 0)
263                 curTime = 0;
264 
265             musicSource.setTimeOffset(curTime);
266 //            if (musicSource.getStatus() == Status.Playing){
267 //                musicSource.stop();
268 //                musicSource.play();
269 //            }
270             updateTime();
271         }
272     }//GEN-LAST:event_sldBarStateChanged
273 
274     /**
275     * @param args the command line arguments
276     */
main(String args[])277     public static void main(String args[]) {
278         java.awt.EventQueue.invokeLater(new Runnable() {
279             public void run() {
280                 new TestMusicPlayer().setVisible(true);
281             }
282         });
283     }
284 
285     // Variables declaration - do not modify//GEN-BEGIN:variables
286     private javax.swing.JButton btnFF;
287     private javax.swing.JButton btnOpen;
288     private javax.swing.JButton btnPlay;
289     private javax.swing.JButton btnRewind;
290     private javax.swing.JButton btnStop;
291     private javax.swing.JLabel lblTime;
292     private javax.swing.JPanel pnlBar;
293     private javax.swing.JPanel pnlButtons;
294     private javax.swing.JSlider sldBar;
295     private javax.swing.JSlider sldVolume;
296     // End of variables declaration//GEN-END:variables
297 
298 }
299