A Java-Based Music Player for MP3, Ogg, & WAV
by Sing Li

Listing One
JButton pauseButton;
 ...
addButtonIconText(pauseButton, "Pause", "Pause24.gif");
     playButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        playClicked(e);
      }
    });

Listing Two
void playClicked(ActionEvent e) {
   String fileToPlay = (String) filenamesList.getSelectedValue();
   if (fileToPlay != null) {
        pBase.playFile(searchDir + 
            System.getProperty("file.separator") + fileToPlay);
   }
  }

Listing Three
void stopClicked(ActionEvent e) {
   pBase.stop();
  }
void pauseClicked(ActionEvent e) {
   pBase.pause();
  }

Listing Four
  void prevClicked(ActionEvent e) {
    pBase.stop();
    filenamesList.setSelectedIndex(   filenamesList.getSelectedIndex() - 1);
    playClicked(e);
  }
  void nextClicked(ActionEvent e) {
    pBase.stop();
    filenamesList.setSelectedIndex((filenamesList.getSelectedIndex()+1) 
                                                     % curPlayListLength);
   playClicked(e);
  }

Listing Five
public void play() {
   if ((!stopped) || (paused)) return;
   if (playerThread == null)  {
     playerThread = new Thread(this);
     playerThread.start();
     try { Thread.sleep(500); 
               } catch (Exception ex) {}
     }
    synchronized(synch) {
              stopped = false;
              synch.notifyAll();
      }
  }

Listing Six
public void run() {
    while (! threadExit)  {
       waitforSignal();
       if (! stopped)
           playMusic();
       }
  }

Listing Seven
if (ais != null)  {
  baseFormat = ais.getFormat();
line = getLine(baseFormat);
  ...
  }

Listing Eight
AudioFormat  decodedFormat = new AudioFormat(
    AudioFormat.Encoding.PCM_SIGNED,
    baseFormat.getSampleRate(),
    16,
    baseFormat.getChannels(),
    baseFormat.getChannels() * 2,
    baseFormat.getSampleRate(),
    false);
ais = AudioSystem.getAudioInputStream(decodedFormat, ais);
line = getLine(decodedFormat);

Listing Nine     
private SourceDataLine getLine(AudioFormat audioFormat)  {
        SourceDataLine res = null;
            DataLine.Info info = new DataLine.Info(SourceDataLine.class,
                                                           audioFormat);
    try  {
       res = (SourceDataLine) AudioSystem.getLine(info);
       res.open(audioFormat);
      }
    catch (Exception e) {
     }
      return res;
  }

2


