1

I want to be able to make either a GUI or console application where the user clicks a button to select an audio file from their computer (of a compatible format) and it plays, and as I'm completely inexperienced in GUIs, it would be nice if I could be given a hint as to how to implement a pause and play button, as well as a volume slide/dial and a stop button. All I know is I'm gonna have to import java.io.* and sun.audio.*.

EDIT My current code is thus:

import sun.audio.*; //import the sun.audio package
import java.awt.*;
import java.io.*;


public class Boombox extends Frame implements FilenameFilter {

/**
 * 
 */
private static final long serialVersionUID = 4914433234899026080L;
Button openButton = new Button("Open");  
Button playButton = new Button("Play");
Button loopButton = new Button("Loop");
Button stopButton = new Button("Stop");
Label filename = new Label("                   ");
File theFile = null;
@SuppressWarnings({ "restriction" })
AudioData theData = null;
InputStream nowPlaying = null;

@SuppressWarnings({ "deprecation" })
public Boombox() {
    super("Boombox");
    resize(300, 200);
    Panel north = new Panel();
    north.setLayout(new FlowLayout(FlowLayout.LEFT));
    north.add(new Label("File: "));
    north.add("North", filename);
    add("North", north);
    Panel south = new Panel();
    south.add(openButton);
    south.add(playButton);
    south.add(loopButton);
    south.add(stopButton);
    add("South", south);
}

@SuppressWarnings("deprecation")
public static void main(String[] args) {
    Boombox sp = new Boombox();
    sp.show();
}

@SuppressWarnings({ "deprecation", "restriction" })
public void open() {
    FileDialog fd = new FileDialog(this, "Please select a .au file:");
    fd.setFilenameFilter(this);
    fd.show();
    try {
        theFile = new File(fd.getDirectory() + "/" + fd.getFile());
        if (theFile != null) {
            filename.setText(theFile.getName());
            FileInputStream fis = new FileInputStream(theFile);
            AudioStream as = new AudioStream(fis);
            theData = as.getData();
        }
    }
    catch (IOException e) {
        System.err.println(e);
    }
}

@SuppressWarnings("restriction")
public void play() {
    stop();    
    if (theData == null) open();
    if (theData != null) {
        AudioDataStream ads = new AudioDataStream(theData);
        AudioPlayer.player.start(ads);
        nowPlaying = ads;
    }
}

@SuppressWarnings("restriction")
public void stop() {
    if (nowPlaying != null) {
        AudioPlayer.player.stop(nowPlaying);
        nowPlaying = null;
    }
}

@SuppressWarnings("restriction")
public void loop() {
    stop();
    if (theData == null) open();
    if (theData != null) {
        ContinuousAudioDataStream cads = new ContinuousAudioDataStream(theData);
        AudioPlayer.player.start(cads);
        nowPlaying = cads;
    }
}

public boolean action(Event e, Object what) {

    if (e.target == playButton) {
        play();
        return true;
    }
    else if (e.target == openButton) {
        open();
        return true;
    }
    else if (e.target == loopButton) {
        loop();
        return true;
    }
    else if (e.target == stopButton) {
        stop();
        return true;
    }

    return false;

}

public boolean accept(File dir, String name) {

    name = name.toLowerCase();
    if (name.endsWith(".au")) return true;
    if (name.endsWith(".wav")) return true;
    return false;

}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
yutsi
  • 17
  • 1
  • 6
  • 1) 'All you know' is wrong. 2) It is tricky to shoe-horn buttons & sliders into a (pure) console application. – Andrew Thompson Apr 20 '11 at 01:17
  • I was thinking if it was going to be a console application there would be commands to change volume, stop, and pause, but I think I've decided that that was not gonna work, and instead I'm gonna do a GUI – yutsi Apr 20 '11 at 01:30

2 Answers2

3

Here is a simple way to play a short clip.

import javax.sound.sampled.*;
import java.net.URL;
import javax.swing.JOptionPane;

class ClipTest {

  public static void main(String[] args) throws Exception {
    String clipName = null;
    if (args.length==1) {
      clipName = args[0];
    } else {
      clipName = "http://pscode.org/media/leftright.wav";
    }
    System.out.println("Looping '" + clipName + "'.");
    URL url = new URL(clipName);
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    Clip clip = AudioSystem.getClip();
    clip.open( ais );
    clip.loop(2);
    clip.start();
    JOptionPane.showMessageDialog(null, "Close to end..");
  }
}

Sample input/output.

F:\proj>java ClipTest http://pscode.org/media/100_2817-linear.wav
Looping 'http://pscode.org/media/100_2817-linear.wav'.

F:\proj>java ClipTest
Looping 'http://pscode.org/media/leftright.wav'.

F:\proj>
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Yeah but I want to be able to accept a user selected audio file – yutsi Apr 20 '11 at 01:50
  • See edits. Added said functionality to CLI app. Note that you should separate the GUI from the 'play sound' (and ask separate questions specific to each). They are largely different things. – Andrew Thompson Apr 20 '11 at 02:25
1

This java uses sun classes:

import sun.audio.*; //import the sun.audio package
import java.awt.*;
import java.io.*;


public class SoundPlayer extends Frame implements FilenameFilter {

  Button openButton = new Button("Open");  
  Button playButton = new Button("Play");
  Button loopButton = new Button("Loop");
  Button stopButton = new Button("Stop");
  Label filename = new Label("                   ");
  File theFile = null;
  AudioData theData = null;
  InputStream nowPlaying = null;

  public SoundPlayer() {
    super("Sound Player");
    resize(300, 200);
    Panel north = new Panel();
    north.setLayout(new FlowLayout(FlowLayout.LEFT));
    north.add(new Label("File: "));
    north.add("North", filename);
    add("North", north);
    Panel south = new Panel();
    south.add(openButton);
    south.add(playButton);
    south.add(loopButton);
    south.add(stopButton);
    add("South", south);
  }

  public static void main(String[] args) {
    SoundPlayer sp = new SoundPlayer();
    sp.show();
  }

  public void open() {
    FileDialog fd = new FileDialog(this, "Please select a .au file:");
    fd.setFilenameFilter(this);
    fd.show();
    try {
      theFile = new File(fd.getDirectory() + "/" + fd.getFile());
      if (theFile != null) {
        filename.setText(theFile.getName());
        FileInputStream fis = new FileInputStream(theFile);
        AudioStream as = new AudioStream(fis);
        theData = as.getData();
      }
    }
    catch (IOException e) {
      System.err.println(e);
    }
  }

  public void play() {
    stop();    
    if (theData == null) open();
    if (theData != null) {
      AudioDataStream ads = new AudioDataStream(theData);
      AudioPlayer.player.start(ads);
      nowPlaying = ads;
    }
  }

  public void stop() {
    if (nowPlaying != null) {
      AudioPlayer.player.stop(nowPlaying);
      nowPlaying = null;
    }
  }

  public void loop() {
    stop();
    if (theData == null) open();
    if (theData != null) {
      ContinuousAudioDataStream cads = new ContinuousAudioDataStream(theData);
      AudioPlayer.player.start(cads);
      nowPlaying = cads;
    }
  }

  public boolean action(Event e, Object what) {

    if (e.target == playButton) {
      play();
      return true;
    }
    else if (e.target == openButton) {
      open();
      return true;
    }
    else if (e.target == loopButton) {
      loop();
      return true;
    }
    else if (e.target == stopButton) {
      stop();
      return true;
    }

    return false;

  }

  public boolean accept(File dir, String name) {

    name = name.toLowerCase();
    if (name.endsWith(".au")) return true;
    if (name.endsWith(".wav")) return true;
    return false;

  }

}
edgarmtze
  • 24,683
  • 80
  • 235
  • 386
  • Some of the code, such as AudioData, are being marked as an error because they are "not accessible due to restriction on required library". – yutsi Apr 20 '11 at 00:30
  • Okay, now that I've fixed that, it says: `java.lang.NoClassDefFoundError: SoundPlayer Caused by: java.lang.ClassNotFoundException: SoundPlayer at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Exception in thread "main" ` – yutsi Apr 20 '11 at 01:38