简体   繁体   English

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

[英]Music in Java (start/stop)

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. 好的,所以我在制作游戏,当您在不同地区或出现中断(例如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);
    }
}

Currently you're creating a new AudioStream in your stop branch and calling the stop method using this. 当前,您正在stop分支中创建一个新的AudioStream ,并使用此方法调用stop方法。 This is a different object to the one that is currently playing. 这是当前正在播放的对象不同的对象 Try making the AudioStream a class variable, and calling stop on that instead. 尝试将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 static identifier on your method - if you're calling this from within an instantiated class you don't need this. 您可能无法使用方法中的static标识符-如果您从实例化的类中调用此标识符,则不需要此标识符。

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? Java Sound库(javax.sound.sampled)可以处理您想做什么吗? Both Clip and SourceDataLine allow one to stop playback. Clip和SourceDataLine都允许停止播放。 That is a more usual way of playing sound, if you want to use native Java. 如果要使用本机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方法的问题,这里有很多人可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM