Java声音类没有循环
我正在尝试开发一款游戏,我找到了两种声音来源 我在下面发布了整个类,即使设置了Java声音类没有循环,java,class,audio,javasound,Java,Class,Audio,Javasound,我正在尝试开发一款游戏,我找到了两种声音来源 我在下面发布了整个类,即使设置了loop\u ever或loop\u times也不会循环: package com.jayitinc.ponygame; import java.io.*; import javax.sound.sampled.*; public class Sound { Thread t; String read; String name; AudioInputStream stream = null; AudioFor
loop\u ever
或loop\u times
也不会循环:
package com.jayitinc.ponygame;
import java.io.*;
import javax.sound.sampled.*;
public class Sound
{
Thread t;
String read;
String name;
AudioInputStream stream = null;
AudioFormat format = null;
// New sound, the String it it's location relative to your project folder.
// I suggest you add a class folder in the project properties to access the
// files from there.
public Sound(String read, String name)
{
sound = new File("sound/" + read);
try
{
stream = AudioSystem.getAudioInputStream(sound);
} catch (Exception e)
{
e.printStackTrace();
}
format = stream.getFormat();
this.name = name;
}
// New sound with set volume
public Sound(String read, float volume, String name)
{
this(read, name);
setVolume(volume);
try
{
stream = AudioSystem.getAudioInputStream(sound);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
format = stream.getFormat();
}
public void play()
{
t = new Thread(play);
// Write you own file location here and be aware that it need to be an
// .wav file
t.start();
}
// ONLY USE IF YOU HAVE STARTED THE THREAD FIRST
public void stop()
{
// stops the sound thread from playing
t.stop();
}
// Just in case you would like to change it
public void setVolume(float volume)
{
this.volume = volume;
}
File sound;
boolean muted = false; // This should explain itself
float volume = 100.0f; // This is the volume that goes from 0 to 100
float pan = 0.0f; // The balance between the speakers 0 is both sides and it
// goes from -1 to 1
boolean isPlaying;
double seconds = 0.0d; // The amount of seconds to wait before the sound
// starts playing
boolean looped_forever = false; // It will keep looping forever if this is
// true
int loop_times = 0; // Set the amount of extra times you want the sound to
// loop (you don't need to have looped_forever set to
// true)
int loops_done = 0; // When the program is running this is counting the
// times the sound has looped so it knows when to stop
// The rest of this is pretty complicated, you don't need to bother with it.
final Runnable play = new Runnable() // This Thread/Runnable is for playing
// the sound
{
public void run()
{
try
{
// Check if the audio file is a .wav file
if (sound.getName().toLowerCase().contains(".wav"))
{
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED)
{
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits() * 2, format.getChannels(), format.getFrameSize() * 2, format.getFrameRate(), true);
stream = AudioSystem.getAudioInputStream(format, stream);
}
SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat(), (int) (stream.getFrameLength() * format.getFrameSize()));
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(stream.getFormat());
line.start();
// Set Volume
FloatControl volume_control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
volume_control.setValue((float) (Math.log(volume / 100.0f) / Math.log(10.0f) * 20.0f));
// Mute
BooleanControl mute_control = (BooleanControl) line.getControl(BooleanControl.Type.MUTE);
mute_control.setValue(muted);
FloatControl pan_control = (FloatControl) line.getControl(FloatControl.Type.PAN);
pan_control.setValue(pan);
int num_read = 0;
byte[] buf = new byte[line.getBufferSize()];
while ((num_read = stream.read(buf, 0, buf.length)) >= 0)
{
int offset = 0;
while (offset < num_read)
{
offset += line.write(buf, offset, num_read - offset);
}
}
line.drain();
line.stop();
if (looped_forever)
{
new Thread(play).start();
} else if (loops_done < loop_times)
{
loops_done++;
new Thread(play).start();
}
}
} catch (Exception ex)
{
ex.printStackTrace();
}
}
};
public int getLength()
{
long audioFileLength = sound.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));
return (int) durationInSeconds;
}
public void setPan(float pan)
{
this.pan = pan;
}
public float getPan()
{
return pan;
}
public void setLoop(boolean loop)
{
looped_forever = loop;
}
public boolean getLoop()
{
return looped_forever;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
package com.jayitinc.ponygame;
导入java.io.*;
导入javax.sound.sampled.*;
公共级音响
{
螺纹t;
字符串读取;
字符串名;
AudioInputStream=null;
AudioFormat=null;
//新声音,字符串,它是相对于项目文件夹的位置。
//我建议您在项目属性中添加一个类文件夹以访问
//那里的文件。
公共声音(字符串读取,字符串名称)
{
声音=新文件(“声音/”+读取);
尝试
{
stream=AudioSystem.getAudioInputStream(声音);
}捕获(例外e)
{
e、 printStackTrace();
}
format=stream.getFormat();
this.name=名称;
}
//设置音量的新声音
公共声音(字符串读取、浮动音量、字符串名称)
{
这个(读,名字);
设置音量(音量);
尝试
{
stream=AudioSystem.getAudioInputStream(声音);
}捕获(例外e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
format=stream.getFormat();
}
公共游戏
{
t=新螺纹(间隙);
//在此处写入您自己的文件位置,并注意它需要是一个
//.wav文件
t、 start();
}
//仅在先启动线程时使用
公共停车场()
{
//停止播放声音线程
t、 停止();
}
//以防万一你想改变它
公共void设置卷(浮动卷)
{
这个。体积=体积;
}
文件声音;
boolean muted=false;//这应该解释一下
float volume=100.0f;//这是从0到100的卷
浮动盘=0.0f;//扬声器之间的平衡0是两侧,它
//从-1变为1
布尔显示;
double seconds=0.0d;//在发出声音之前等待的秒数
//开始演奏
boolean looped_forever=false;//如果这是
//真的
int loop_times=0;//设置希望声音播放的额外次数
//loop(您不需要将loop_永久设置为
//(对)
int loops_done=0;//当程序运行时,这是计算
//多次声音循环,以便知道何时停止
//剩下的部分相当复杂,你不必费心了。
final Runnable play=new Runnable()//此线程/Runnable用于播放
//声音
{
公开募捐
{
尝试
{
//检查音频文件是否为.wav文件
if(sound.getName().toLowerCase()包含(“.wav”))
{
if(format.getEncoding()!=AudioFormat.Encoding.PCM\u签名)
{
格式=新的音频格式(AudioFormat.Encoding.PCM_SIGNED,format.getSampleRate(),format.getSampleSizeInBits()*2,format.getChannels(),format.getFrameSize()*2,format.getFrameRate(),true);
stream=AudioSystem.getAudioInputStream(格式,流);
}
SourceDataLine.Info=newdataline.Info(SourceDataLine.class,stream.getFormat(),(int)(stream.getFrameLength()*format.getFrameSize());
SourceDataLine=(SourceDataLine)AudioSystem.getLine(info);
line.open(stream.getFormat());
line.start();
//定容
FloatControl volume\u control=(FloatControl)line.getControl(FloatControl.Type.MASTER\u增益);
音量控制设定值((浮点)(数学日志(音量/100.0f)/数学日志(10.0f)*20.0f));
//哑巴
BooleanControl mute_control=(BooleanControl)line.getControl(BooleanControl.Type.mute);
静音控制。设置值(静音);
FloatControl pan\u control=(FloatControl)line.getControl(FloatControl.Type.pan);
pan_控制设定值(pan);
int num_read=0;
byte[]buf=新字节[line.getBufferSize()];
而((num_read=stream.read(buf,0,buf.length))>=0)
{
整数偏移=0;
while(偏移量
编辑:我需要能够控制音量,所以如果您有其他课程,请确保它可以控制音量
(reClip
)…如何控制音量
请尝试上看到的代码的此变体
您是否尝试使用调试器?为什么每次都要重新启动一个新线程,而不是将数据流多次发送到音频输出?这太多了
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.event.*;
public class VolumeControl {
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://pscode.org/media/leftright.wav");
Clip clip = AudioSystem.getClip();
// getAudioInputStream() also accepts a File or InputStream
AudioInputStream ais = AudioSystem.
getAudioInputStream( url );
clip.open(ais);
Control[] c = clip.getControls();
FloatControl temp = null;
for (Control control : c) {
System.out.println(control);
if (control.toString().toLowerCase().contains("master gain")) {
// we found it!
temp = (FloatControl)control;
}
}
final FloatControl vol = temp;
clip.loop(Clip.LOOP_CONTINUOUSLY);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JComponent c = null;
if (vol!=null) {
final JSlider volControl = new JSlider(
(int)(100*vol.getMinimum()),
(int)(100*vol.getMaximum()),
(int)(100*vol.getValue())
);
ChangeListener cl = new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println( "Vol: " + volControl.getValue()/100f );
vol.setValue(volControl.getValue()/100f);
}
};
volControl.addChangeListener(cl);
c = volControl;
} else {
c = new JLabel("Close to exit!");
}
JOptionPane.showMessageDialog(null, c);
}
});
}
}