我可以使用什么而不是AudioPlayer.player.stop();?

时间:2014-01-03 05:05:42

标签: java swing audio javasound

我已成功编码在GUI中播放音频。

但是当我使用时:

AudioPlayer.player.stop();

单词"停止"被删除,并发出警告,说“stop(), suspend() and resume() java.lang.Thread的方法很危险,不应使用。”我正在使用Netbeans IDE 6.9.1

我读过的所有网站使用此方法都没有问题。是否有其他方法可以停止播放音频?

这是我的代码:

String song = songList.getSelectedValue().toString();//determines the string of the selected value in the list box
    if(song.equals("Matt Nathanson - Come On Get Higher")){//if this song is selected...
        InputStream in;
        try{
        in = new FileInputStream(new File("C:\\Stuff\\Grade Twelve\\Computer Programming\\Unit 4\\Step 3\\GuitarForBeginners\\src\\guitarforbeginners\\resources\\Come On Get Higher.wav"));
        AudioStream audio = new AudioStream(in);
        AudioPlayer.player.start(audio);
        }
        catch(Exception e){
        System.out.println("File not found");
        }
        if (cancelButton.isSelected()){
            AudioPlayer.player.stop(audio);
        }
    }

(回复greg-449)

使用您建议的链接以及http://www.javadocexamples.com/java_source/__/co/CoreJavaSound.java.html。该程序运行但打印null。想法?感谢..

if (song.equals("Matt Nathanson - Come On Get Higher")){//if this song is selected...
        try{
            stopPlay();
            AudioInputStream audio = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("C:\\Stuff\\Grade Twelve\\Computer Programming\\Unit 4\\Step 3\\GuitarForBeginners\\src\\guitarforbeginners\\resources\\Come On Get Higher.wav"));
            clip = AudioSystem.getClip();
            clip.open(audio);
            clip.start();
        } catch(Exception e){
            stopPlay();
            System.err.println(e.getMessage());
}
    }

2 个答案:

答案 0 :(得分:0)

试试这个

    String song = songList.getSelectedValue().toString();//determines the string of the selected value in the list box
        if(song.equals("Matt Nathanson - Come On Get Higher")){//if this song is selected...
            InputStream in;
            InputStream closetrick;
            try{
            in = new FileInputStream(new File("C:\\Stuff\\Grade Twelve\\Computer Programming\\Unit 4\\Step 3\\GuitarForBeginners\\src\\guitarforbeginners\\resources\\Come On Get Higher.wav"));
 closetrick =new FileInputStream(new File("C:\\Stuff\\Grade Twelve\\Computer Programming\\Unit 4\\Step 3\\GuitarForBeginners\\src\\guitarforbeginners\\resources\\Zerosec.wav"));
            AudioStream audio = new AudioStream(in);
            AudioPlayer.player.start(audio);
            }
            catch(Exception e){
            System.out.println("File not found");
            }
            if (cancelButton.isSelected()){
                 AudioStream audio2 = new AudioStream(closetrick);
            AudioPlayer.player.start(audio2);
            }
        }

其中Zerosec波是长度为零秒的波

答案 1 :(得分:0)

**

你应该播放一个AudioPlayer对象然后调用他的方法。

**