参考书,教程上都写的是Applet小程序的声音播放,那么在JFrame中怎么现实声音的播放呢?请高手帮忙,发点具体代码上来,也可以联系本人,本人QQ:9349599,谢谢了。本人急啊,星期5就要上讲台演试程序了。

解决方案 »

  1.   

    Applet小程序都能写出来,那JFrame也应该不成问题呀
      

  2.   

    用JMF的payer可以。Frame只是个显示用的框架,怎么可能播放声音?Applet是用它本身的方法去播放的,和Frame没有关系。
      

  3.   

    如下代码所示,详见
    http://bbs.netjava.org/index.php?topic=12.0import sun.audio.*;
    import java.awt.*;
    import java.io.*;
    public class SoundPlayer extends Frame implements FilenameFilter {  Button openButton = new Button("打开");  
      Button playButton = new Button("播放");
      Button loopButton = new Button("循环");
      Button stopButton = new Button("停止");
      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, "请选择一个要播放的文件:");
        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;  }}