在构造函数中播放音频而不是播放方法
因此,我一直在游戏库上工作,并且刚刚开始研究它的声音方面。但是问题在于,声音开始从构造函数而不是播放方法播放,而停止也不适用于构造函数,而仅适用于播放方法。
我尝试调试代码,但没有从中获得任何结果。我还尝试在执行播放方法之前尝试使用停止方法,但是在
下面的播放方法之前,这是代码,
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);
}
这是日志表单中发生的事情的示例,
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
如果有人知道为什么会发生这种情况回复这个。谢谢
编辑:我将夹子和FC更改为不静态,因为我使用静态测试,然后忘了将其更改为正常
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.
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我解决了。这只是我编码时我涂黑的情况,而我在项目的主要方法中播放声音。我很高兴剪辑班不是问题
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