简体   繁体   English

音频-剪辑不会停止

[英]audio - Clip won't stop

I have a function that is called every time the user clicks which will start the music if the screen has changed. 我有一个函数,每次用户单击时都会调用该函数,如果屏幕已更改,它将启动音乐。 The only problem with it right now is that it does not stop. 现在唯一的问题是它不会停止。 I have figured out that this is because clip = AudioSystem.getClip(); 我发现这是因为clip = AudioSystem.getClip(); won't return the currently playing clip, it returns a random location every time. 不会返回当前播放的剪辑,而是每次都返回一个随机位置。 How do I make sure that clip gets the value of the currently playing clip so that I can stop it? 如何确保该剪辑获得当前正在播放的剪辑的值,以便可以停止播放?

The "StartBGMusic" function “ StartBGMusic”功能

public void startBGMusic() throws LineUnavailableException, IOException
    {   
        clip = AudioSystem.getClip();
        System.out.println(clip);
        try
        {
            if(SCREEN_CHECK != Screen)
            {
                clip.stop();

                clip.close();

                if(Screen != 14 && Screen != 19 && Screen != 16 && Screen != 15 && Screen != 17)
                {
                    clip.open(AudioSystem.getAudioInputStream(getClass().getResource("UpbeatFunk.wav")));
                    clip.loop(99999);
                }
                else
                {
                    clip.open(AudioSystem.getAudioInputStream(getClass().getResource("Background Music.mid")));
                    clip.loop(99999);
                }
            } 

            SCREEN_CHECK = Screen;
        }catch (Exception e)
        {
            e.printStackTrace(System.out);
        }
    }

According to oracle documentation, getClip() does not return the current playing sound, but a clip object that can be used to play back an audio file. 根据oracle文档, getClip()不会返回当前的播放声音,而是一个可用于播放音频文件的剪辑对象。

You will need to pass a reference to the clip you used to start the audio file. 您将需要传递对用于启动音频文件的剪辑的引用。 This can be done by returning it/passing it as an argument or by using global variables/getter methods 这可以通过将其返回/作为参数传递或使用全局变量/ getter方法来完成

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

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