Java 中的音乐(开始/停止)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9668361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:54:16  来源:igfitidea点击:

Music in Java (start/stop)

javajavasoundfileinputstreamaudiostreamer

提问by Austin

Ok, so I am making a game and the music changes when you are in different regions or if there is an interruption, like with an AI.

好的,所以我正在制作一个游戏,当你在不同的地区或出现中断时,音乐会发生变化,就像人工智能一样。

So I have JUST learned how to make music showup in my program, and now I am trying to make it stop, but I am unsure how to, below is a snippet of code where the music plays and then I try to overwite it with new music when an action occurs.

所以我刚刚学会了如何在我的程序中显示音乐,现在我试图让它停止,但我不确定如何,下面是播放音乐的代码片段,然后我尝试用新的代码覆盖它动作发生时的音乐。

public static void songs(String word) {
        String temp = word;
        if (temp.equals("start")) {

            try {

                try {
                    blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/battle.wav");

                } catch (Throwable e) {
                    e.printStackTrace();
                }
                AudioStream as = new AudioStream(blah);

                AudioPlayer.player.start(as);
                System.out.println("going");

            } catch (IOException e) {
                System.err.println(e);
            }
        }

        if (temp.equals("stop")) {

            try {

                try {
                    blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/silence.wav");

                } catch (Throwable e) {
                    e.printStackTrace();
                }
                AudioStream as = new AudioStream(blah);

                AudioPlayer.player.stop(as);
                System.out.println("stopping");

            } catch (IOException e) {
                System.err.println(e);
            }
        }
    }

This is the only method I have been able to find that has the music play, but if you guys have any other suggestions please let me know.

这是我能找到的唯一可以播放音乐的方法,但是如果你们有任何其他建议,请告诉我。

Again, I want to have sound affects and music going, and right now all that happens is one song will play, and it will not stop under any circumstance until it hits the very end of its length. I want to be able to stop songs whenever a new one should come on, and also allow sound affects to pop up.

再一次,我想要声音效果和音乐,现在发生的只是一首歌会播放,并且在任何情况下都不会停止,直到它的长度结束。我希望能够在有新歌曲出现时停止歌曲,并允许弹出声音效果。

Thanks! (since I am stuck on this and need an answer now I will probably repost on one or two more java sites so I can get a response ASAP, thank you though!!!!)

谢谢!(因为我被困在这个问题上,现在需要一个答案,我可能会在另外一两个 Java 站点上重新发布,以便我能尽快得到回复,不过还是谢谢你!!!!)

EDITED CODE: (still does not stop the current stream, any more suggestions appreciated)

编辑代码:(仍然没有停止当前的流,任何更多的建议表示赞赏)

public static void songs(String word) throws IOException {
    String temp = word;


    if (temp.equals("go")) {
        try {
            blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/battle.wav");
        } catch (Throwable e) {
            e.printStackTrace();
        }

        AudioStream as = new AudioStream(blah);
        AudioPlayer.player.start(as);
        System.out.println("going");
    }

    if (temp.equals("stop")) {

        //don't try and do things with a null object!
        if (as != null) {
            AudioPlayer.player.stop(as);
            System.out.println("stopping1");
        }
        System.out.println("stopping2");
        AudioPlayer.player.stop(as);
    }
}

采纳答案by raveturned

Currently you're creating a new AudioStreamin your stop branch and calling the stop method using this. This is a different objectto the one that is currently playing. Try making the AudioStreama class variable, and calling stop on that instead.

目前,您正在AudioStream停止分支中创建一个新的并使用它调用停止方法。这是当前正在播放的对象不同的对象。尝试创建AudioStream一个类变量,然后调用 stop 来代替。

EDIT: at the top of the class containing your code...

编辑:在包含您的代码的类的顶部...

class YourClass {
    //the class member variable
    private AudioStream as;
    //[etc...]

In your start branch:

在您的开始分支中:

// 'as' has already been defined above
as = new AudioStream(blah);
AudioPlayer.player.start(as);
System.out.println("going");

In your stop branch:

在您的停止分支中:

try
{
    //don't try and do things with a null object!
    if (as != null)
    {
        AudioPlayer.player.stop(as);
    }
    System.out.println("stopping");
}
catch (IOException e)
{
    System.err.println(e);
}

You may have trouble with the staticidentifier on your method - if you're calling this from within an instantiated class you don't need this.

static的方法上的标识符可能有问题- 如果您从实例化类中调用它,则不需要它。

回答by Phil Freihofner

I can't even access these sun.audio Objects on my Eclipse IDE--I know they are in rt.jar, but there is header info about them being proprietary and such.

我什至无法在我的 Eclipse IDE 上访问这些 sun.audio 对象——我知道它们在 rt.jar 中,但是有关于它们是专有的等等的标题信息。

Can the Java Sound library (javax.sound.sampled) handle what you want to do? Both Clip and SourceDataLine allow one to stop playback. That is a more usual way of playing sound, if you want to use native Java.

Java Sound 库 (javax.sound.sampled) 可以处理您想要做的事情吗?Clip 和 SourceDataLine 都允许停止播放。如果您想使用原生 Java,这是一种更常见的播放声音的方式。

Playback into is here:

回放在这里:

http://docs.oracle.com/javase/tutorial/sound/playing.html

http://docs.oracle.com/javase/tutorial/sound/playing.html

But the documentation, overall, is not exactly rich with examples. There's example code at this site

但总体而言,文档中的示例并不十分丰富。这个站点有示例代码

http://www.jsresources.org/

http://www.jsresources.org/

and plenty of people here who could help if you run into problems with the native Java approach.

如果您遇到原生 Java 方法的问题,这里有很多人可以提供帮助。