用户如何选择音频文件并使用Java播放它

|| 我希望能够制作一个GUI或控制台应用程序,在该应用程序中,用户单击一个按钮以从其计算机中选择一个音频文件(具有兼容格式)并可以播放,并且由于我对GUI完全没有经验,所以它将如果可以提示我如何实现暂停和播放按钮,以及音量滑动/拨号和停止按钮,那就太好了。我所知道的是,我将不得不输入
java.io.*
sun.audio.*
。 编辑 因此,我当前的代码是:
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;

}

}
    
已邀请:
这是播放短片的简单方法。
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..\");
  }
}
样本输入/输出。
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>
    
此Java使用sun类:
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;

  }

}
    

要回复问题请先登录注册