Changeset 51 in tmcsimulator for trunk/src/tmcsim/cadsimulator/SoundPlayer.java


Ignore:
Timestamp:
07/03/2016 06:25:46 AM (10 years ago)
Author:
jdalbey
Message:

SoundPlayer?.java modified to use Jaco mp3player instead of obsolete Sun libraries

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/cadsimulator/SoundPlayer.java

    r16 r51  
    11package tmcsim.cadsimulator; 
    22 
     3import jaco.mp3.player.MP3Player; 
     4import java.applet.*; 
    35import java.io.File; 
    4 import java.io.FileInputStream; 
    56import java.util.Timer; 
    67import java.util.TimerTask; 
     
    89import java.util.logging.Level; 
    910import java.util.logging.Logger; 
    10  
    11 import sun.audio.AudioPlayer; 
    12 import sun.audio.AudioStream; 
    1311import tmcsim.client.cadclientgui.data.IncidentEvent; 
    1412 
    1513/** 
    16  * SoundPlayer is used to play audio files associated with IncidentEvents that  
    17  * occur during a simulation.  When the SoundPlayer has been enabled audio  
    18  * clips are enqueued by calling the enqueueClip() method.  The audio files  
    19  * are played in order of receipt and when finished, the IncidentEvent is  
    20  * notified.  Audio playing may be disabled or enabled through the  
    21  * setAudioEnabled() method.  Disabling audio will cause a currently 
    22  * playing audio clip to stop and be requeued at the front of the queue.   
    23  * Re-enabling the audio will then continue playing all queued clips.  The 
    24  * deQueueAll() method is used to clear the list of audio clips that have  
    25  * been queued. 
     14 * SoundPlayer is used to play audio files associated with IncidentEvents that 
     15 * occur during a simulation. When the SoundPlayer has been enabled audio clips 
     16 * are enqueued by calling the enqueueClip() method. The audio files are played 
     17 * in order of receipt and when finished, the IncidentEvent is notified. Audio 
     18 * playing may be disabled or enabled through the setAudioEnabled() method. 
     19 * Disabling audio will cause a currently playing audio clip to stop and be 
     20 * requeued at the front of the queue. Re-enabling the audio will then continue 
     21 * playing all queued clips. The deQueueAll() method is used to clear the list 
     22 * of audio clips that have been queued. 
    2623 * 
    2724 * @author Matthew Cechini (mcechini@calpoly.edu) 
    28  * @version $Date: 2006/06/06 20:46:41 $ $Revision: 1.3 $ 
     25 * @version Date: 2006/06/06 20:46:41 
     26 * @author Nicolas Russo modified to play mp3 audio. 
     27 * @version 2016.07.03 
    2928 */ 
    30 public class SoundPlayer extends Thread { 
    31      
    32     /** Error Logger. */ 
     29public class SoundPlayer extends Thread 
     30{ 
     31 
     32    /** 
     33     * Error Logger. 
     34     */ 
    3335    private Logger soundLogger = Logger.getLogger("tmcsim.cadsimulator"); 
    34                                  
    35     /** The base location that wav files are referenced from */              
     36    /** 
     37     * The base location that wav files are referenced from 
     38     */ 
    3639    private String baseAudioDir = ""; 
    37                                  
    38     /** The private vector of audioClips used during a simulation */ 
     40    /** 
     41     * The private vector of audioClips used during a simulation 
     42     */ 
    3943    private Vector<AudioClipInfo> enqueuedClips = null; 
    40      
    41     /** The audio clip that is currently being played by the AudioPlayer. */ 
     44    /** 
     45     * The audio clip that is currently being played by the AudioPlayer. 
     46     */ 
    4247    private AudioClipInfo currentClip = null; 
    43      
    44     /** Audio stream for playing audio files. */ 
    45     private AudioStream audioStream = null; 
    46      
    47     /** 
    48      * Flag to designate when an audio file is playing. This prevents  
    49      * multiple audio files from being played simultaneously.  
    50      * Flag is initialized to false. 
    51      */ 
    52     private boolean audioPlaying = false;    
    53      
    54     /** Flag to designate whether audio playing is enabled.  If audio is disabled, 
    55      *  then enquque events will be ignored.  Flag is initialized to false. 
     48    /** 
     49     * Audio stream for playing audio files. 
     50     */ 
     51    private AudioClip clip = null; 
     52    private MP3Player mp3Player = null; 
     53    /** 
     54     * Flag to designate when an audio file is playing. This prevents multiple 
     55     * audio files from being played simultaneously. Flag is initialized to 
     56     * false. 
     57     */ 
     58    private boolean audioPlaying = false; 
     59    /** 
     60     * Flag to designate whether audio playing is enabled. If audio is disabled, 
     61     * then enquque events will be ignored. Flag is initialized to false. 
    5662     */ 
    5763    private boolean audioEnabled = false; 
    58      
    59     /** Timer used to pause audio player during an audio file. */ 
     64    /** 
     65     * Timer used to pause audio player during an audio file. 
     66     */ 
    6067    private Timer timer = null; 
    61      
    62     /** Inner class used to contain the audio file's name and duration. */ 
    63     private class AudioClipInfo { 
     68 
     69    /** 
     70     * Inner class used to contain the audio file's name and duration. 
     71     */ 
     72    private class AudioClipInfo 
     73    { 
     74 
    6475        String fileName; 
    6576        int duration; 
    6677        IncidentEvent theEvent; 
    67          
    68         AudioClipInfo(IncidentEvent ie) { 
     78 
     79        AudioClipInfo(IncidentEvent ie) 
     80        { 
    6981            fileName = ie.waveFile; 
    70             duration = ie.waveLength;    
     82            duration = ie.waveLength; 
    7183            theEvent = ie; 
    72         }        
    73                  
    74         AudioClipInfo(String name, int dur) { 
     84        } 
     85 
     86        AudioClipInfo(String name, int dur) 
     87        { 
    7588            fileName = name; 
    76             duration = dur;  
     89            duration = dur; 
    7790            theEvent = null; 
    78         }    
    79          
    80         public void wavePlayed() { 
    81             if(theEvent != null) 
     91        } 
     92 
     93        public void wavePlayed() 
     94        { 
     95            if (theEvent != null) 
     96            { 
    8297                theEvent.wavePlayed(); 
    83         }    
    84     } 
    85   
    86     /** 
    87      * Constructor.  Establish the base file path for wav file referencing. 
    88      * 
    89      * @param wavFilePath Pathname for where the simulation wav files will be referenced. 
    90      */ 
    91     public SoundPlayer(String baseFilePath) { 
    92          
    93         baseAudioDir  = baseFilePath; 
    94         audioPlaying  = false; 
    95         audioEnabled  = true; 
    96         timer         = new Timer();                         
     98            } 
     99        } 
     100    } 
     101 
     102    /** 
     103     * Constructor. Establish the base file path for wav file referencing. 
     104     * 
     105     * @param wavFilePath Pathname for where the simulation wav files will be 
     106     * referenced. 
     107     */ 
     108    public SoundPlayer(String baseFilePath) 
     109    { 
     110        baseAudioDir = baseFilePath; 
     111        audioPlaying = false; 
     112        audioEnabled = true; 
     113        timer = new Timer(); 
    97114        enqueuedClips = new Vector<AudioClipInfo>(); 
    98115    } 
    99116 
    100117    /** 
    101      * Add a audioClipInfo object to the audioClips Vector.  The enqueued audio file will play 
    102      * when all previously enqueued audio files have been played.  If audio is not enabled, the 
    103      * clip will not be queued, and the incident event will be notified that the audio clip  
    104      * has finished playing to allow the simulation to continue. 
     118     * Add a audioClipInfo object to the audioClips Vector. The enqueued audio 
     119     * file will play when all previously enqueued audio files have been played. 
     120     * If audio is not enabled, the clip will not be queued, and the incident 
     121     * event will be notified that the audio clip has finished playing to allow 
     122     * the simulation to continue. 
    105123     * 
    106124     * @param ie The Incident Event to enqueue to the current list of events. 
    107125     */ 
    108     public void enqueueClip (IncidentEvent ie) { 
    109  
    110         if(audioEnabled) { 
    111             synchronized(enqueuedClips)  { 
     126    public void enqueueClip(IncidentEvent ie) 
     127    { 
     128 
     129        if (audioEnabled) 
     130        { 
     131            synchronized (enqueuedClips) 
     132            { 
    112133                enqueuedClips.add(new AudioClipInfo(ie)); 
    113134            } 
    114135        } 
    115         else { 
     136        else 
     137        { 
    116138            ie.wavePlayed(); 
    117139        } 
    118     }  
    119      
    120     /** 
    121      * Method called when user presses reset in simulation.  All queued events need 
    122      * to be deQueued. 
    123      */ 
    124     public void deQueueAll() { 
    125         synchronized(enqueuedClips) { 
     140    } 
     141 
     142    /** 
     143     * Method called when user presses reset in simulation. All queued events 
     144     * need to be deQueued. 
     145     */ 
     146    public void deQueueAll() 
     147    { 
     148        synchronized (enqueuedClips) 
     149        { 
    126150            enqueuedClips.clear(); 
    127         }        
    128     }   
    129      
    130     /**  
     151        } 
     152    } 
     153 
     154    /** 
    131155     * Get the current audio enabled status. 
    132156     * 
    133157     * @return True if enabled, false if disabled. 
    134158     */ 
    135     public boolean getAudioEnabled() { 
     159    public boolean getAudioEnabled() 
     160    { 
    136161        return audioEnabled; 
    137     }    
    138      
    139     /** 
    140      * Set the current audio enabled status.  A false value will cause all 
    141      * enqueue events to be ignored.  A currently playing audio file will be  
    142      * stopped and requeued.  All other queued events will remain in the queue. 
     162    } 
     163 
     164    /** 
     165     * Set the current audio enabled status. A false value will cause all 
     166     * enqueue events to be ignored. A currently playing audio file will be 
     167     * stopped and requeued. All other queued events will remain in the queue. 
    143168     * 
    144169     * @param enable True if enabling, false if disabling. 
    145170     */ 
    146     public void setAudioEnabled(boolean enable) { 
    147          
    148         if(!enable) {            
     171    public void setAudioEnabled(boolean enable) 
     172    { 
     173 
     174        if (!enable) 
     175        { 
    149176            timer.cancel(); 
    150             AudioPlayer.player.stop(audioStream); 
    151              
    152             if(currentClip != null) { 
     177            if (clip != null) 
     178            { 
     179                clip.stop(); 
     180            } 
     181            if (mp3Player != null) 
     182            { 
     183                mp3Player.stop(); 
     184            } 
     185 
     186            if (currentClip != null) 
     187            { 
    153188                enqueuedClips.add(0, currentClip); 
    154             }            
    155              
     189            } 
     190 
    156191            audioPlaying = false; 
    157192        } 
    158          
     193 
    159194        audioEnabled = enable; 
    160195    } 
    161      
    162          
    163     /** 
    164      * Method declaration for the Thread.run() method.  While this thread is not  
    165      * interrupted, check if there are enqueued audio files.  If so, remove the  
    166      * first one(FIFO) create an AudioClip object, and play the audio file. 
    167      * A timer tis then started for the duration of the audio file.  When the 
    168      * timer expires the audioPlaying flag is reset to false and the corresponding 
     196 
     197    /** 
     198     * Method declaration for the Thread.run() method. While this thread is not 
     199     * interrupted, check if there are enqueued audio files. If so, remove the 
     200     * first one(FIFO) create an AudioClip object, and play the audio file. A 
     201     * timer tis then started for the duration of the audio file. When the timer 
     202     * expires the audioPlaying flag is reset to false and the corresponding 
    169203     * incident event is notified of the audio file's completion. 
    170204     */ 
    171     public void run() { 
     205    public void run() 
     206    { 
    172207        AudioClipInfo theClip = null; 
    173          
    174         while(!isInterrupted()) { 
    175              
    176             synchronized(enqueuedClips) { 
    177                 if(enqueuedClips.size() > 0 && !audioPlaying && audioEnabled) 
    178                     theClip = enqueuedClips.remove(0);                   
    179             } 
    180                  
     208 
     209        while (!isInterrupted()) 
     210        { 
     211 
     212            synchronized (enqueuedClips) 
     213            { 
     214                if (enqueuedClips.size() > 0 && !audioPlaying && audioEnabled) 
     215                { 
     216                    theClip = enqueuedClips.remove(0); 
     217                } 
     218            } 
     219 
    181220            //if nothing to play, wait a second 
    182             if(theClip == null) { 
    183                 try { Thread.sleep(1000);} catch (Exception e) {} 
    184             } 
    185             else { 
    186                          
    187                 try{ 
    188                      
    189                     if(theClip.duration > 0) {               
    190                          
    191                         File audioFile = new File(baseAudioDir + theClip.fileName); 
    192                          
    193                         if(audioFile.exists()) { 
    194                             audioStream = new AudioStream (new FileInputStream(audioFile));       
    195                             AudioPlayer.player.start(audioStream);   
    196                         } 
    197                         else { 
     221            if (theClip == null) 
     222            { 
     223                try 
     224                { 
     225                    Thread.sleep(1000); 
     226                } catch (Exception e) 
     227                { 
     228                } 
     229            } 
     230            else 
     231            { 
     232 
     233                try 
     234                { 
     235 
     236                    if (theClip.duration > 0) 
     237                    { 
     238                        String filename = baseAudioDir + theClip.fileName; 
     239                        String fileExt = filename.substring(filename.lastIndexOf('.')); 
     240                        File audioFile = new File(filename); 
     241 
     242                        if (audioFile.exists() && fileExt.equals(".mp3")) 
     243                        { 
     244                            mp3Player = new MP3Player(audioFile); 
     245                            mp3Player.play(); 
     246                        } 
     247                        else if (audioFile.exists() && fileExt.equals(".wav")) 
     248                        { 
     249                            clip = Applet.newAudioClip(audioFile.toURI().toURL()); 
     250                            clip.play(); 
     251                        } 
     252                        else 
     253                        { 
    198254                            throw new Exception(); 
    199                         }  
     255                        } 
    200256                    } 
    201                      
    202                     currentClip  = theClip;                                              
     257 
     258                    currentClip = theClip; 
    203259                    audioPlaying = true; 
    204                      
     260 
    205261                    timer = new Timer(); 
    206                     timer.schedule(new TimerTask () { 
    207                         public void run() { 
     262                    timer.schedule(new TimerTask() 
     263                    { 
     264                        public void run() 
     265                        { 
    208266                            clipPlayed(); 
    209                             audioPlaying = false;                        
     267                            audioPlaying = false; 
    210268                        } 
    211269                    }, theClip.duration * 1000); 
    212                      
    213                     theClip = null;                      
    214                 }  
    215                 catch (Exception e) { 
    216                     soundLogger.logp(Level.WARNING, "SoundPlayer", "run",  
     270 
     271                    theClip = null; 
     272                } catch (Exception e) 
     273                { 
     274                    soundLogger.logp(Level.WARNING, "SoundPlayer", "run", 
    217275                            "Unable to play audio file: " + baseAudioDir + theClip.fileName, e); 
    218      
     276 
    219277                    theClip.theEvent.wavePlayed(); 
    220278                    theClip = null; 
    221279                    timer.cancel(); 
    222                 }    
    223             }                                
    224         }                        
    225     } 
    226      
    227          
    228     /** 
    229      * Called when audio file finishes playing.  Notifies the corresponding  
     280                } 
     281            } 
     282        } 
     283    } 
     284 
     285    /** 
     286     * Called when audio file finishes playing. Notifies the corresponding 
    230287     * Incident Event that its audio file has finished playing. 
    231288     */ 
    232     private void clipPlayed() { 
     289    private void clipPlayed() 
     290    { 
    233291        currentClip.wavePlayed(); 
    234     }   
     292    } 
    235293} 
Note: See TracChangeset for help on using the changeset viewer.