简体   繁体   English

在构造函数中播放音频而不是播放方法

[英]audio playing in constructor instead of play method

so I've been working on my game library and I just got working on the sound aspect of it.所以我一直在研究我的游戏库,我刚刚开始研究它的声音方面。 But the problems are that the Sound starts playing from the constructor instead of the play method and also the stop doesn't work for the constructor and only the play method.但问题是 Sound 从构造函数而不是 play 方法开始播放,而且停止对构造函数不起作用,只对 play 方法起作用。

I tried debugging the code but I didn't get any results from it.我尝试调试代码,但没有得到任何结果。 I also tried using the stop method before doing the play method but that didn't work either我也尝试在执行 play 方法之前使用 stop 方法,但这也不起作用

below is the code,下面是代码,

import java.io.*;
import java.io.File;

import java.io.IOException;

import javax.sound.sampled.*;

// class stuff

    private Clip clip;
    private FloatControl fc;
    
    public SoundLoader(File file) {
        try {
            InputStream audioSource = new FileInputStream(file);
            InputStream bufferedInput = new BufferedInputStream(audioSource);
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(bufferedInput);
            AudioFormat baseFormat = audioInputStream.getFormat();
            AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(),
                    baseFormat.getSampleSizeInBits(),
                    baseFormat.getChannels(),
                    baseFormat.getFrameSize(),
                    baseFormat.getFrameRate(),
                    false
                    );
            AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(decodedFormat, audioInputStream);
            clip = AudioSystem.getClip();
            clip.open(decodedAudioInputStream);
            fc = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void play(boolean loop){

        if(clip == null || isRunning())return;
        stop();
        while(!clip.isRunning())
            clip.start();

        if(loop)clip.loop(Clip.LOOP_CONTINUOUSLY);
            
    }

and here is an example of what's happening in log form,这是一个日志形式的例子,

clip starts running from constructor
same clip starts running from play method
stop method stops the clip from running from the play method
constructor keeps on playing

If anyone knows why this is happening it would be nice if you could reply to this.如果有人知道为什么会发生这种情况,那么如果您能回复此消息就好了。 Thanks谢谢

edit: I changed clip and fc to not static because I use testing something out with static and then I forgot to change it back to normal编辑:我将 clip 和 fc 更改为不是 static 因为我用 static 测试了一些东西然后我忘了把它改回正常

okay I solved it.好的,我解决了。 It was just a case of me blacking out while I was coding and I was playing the sound in the main method of the project.这只是我在编码时突然停电的情况,我正在项目的主要方法中播放声音。 I'm glad that it wasn't a problem with the Clip class我很高兴 Clip class 没有问题

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

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