Java的声音类中没有循环 [英] Java sound class not looping
本文介绍了Java的声音类中没有循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想开发一个游戏,我已经找到2源的声音。
我下面贴全班,它似乎没有循环将甚至当 looped_forever
或 loop_times
是安装程序这样做的:
包com.jayitinc.ponygame;进口java.io. *;进口javax.sound.sampled中*。公共类Sound
{
线程t;
串读;
字符串名称;的AudioInputStream流= NULL;
AudioFormat的格式= NULL;//新的声音,字符串它是相对于你的项目文件夹的位置。
//我建议你在项目属性来访问添加一个类文件夹
//从那里的文件。
市民声音(读字符串,字符串名称)
{
声音=新的文件(音/+读);
尝试
{
流= AudioSystem.getAudioInputStream(音);
}赶上(例外五)
{
e.printStackTrace();
}
格式= stream.getFormat();
this.name =名称;
}//新的声音设定的音量
市民声音(字符串读,浮动量,字符串名称)
{
这个(读,姓名);
setVolume(体积);
尝试
{
流= AudioSystem.getAudioInputStream(音);
}赶上(例外五)
{
// TODO自动生成catch块
e.printStackTrace();
}
格式= stream.getFormat();
}公共无效播放()
{
T =新的Thread(播放);
//写你自己的文件位置在这里和知道它需要成为一个
// .wav文件
t.start();
}//只使用如果您已经开始线程首先
公共无效停止()
{
//停止播放声音线程
t.stop();
}//以防万一你想改变它
公共无效setVolume(浮动量)
{
this.volume =体积;
}文件的声音;
布尔静音= FALSE; //这应该有个交待
浮动量= 100.0f; //这是一个从0到100的容积
浮锅= 0.0; //扬声器0之间的平衡是两面和它
//进入从-1到1
布尔IsPlaying模块;双秒= 0.0D; //秒金额声音之前等待
//开始播放布尔looped_forever = FALSE; //它会永远保持循环,如果这是
//真INT loop_times = 0; //设置额外的倍量你想要的声音
//循环(你不需要有looped_forever设置
//真)
INT loops_done = 0; //当程序运行时这计数
//次声已经循环,所以它知道何时停止//这样做的其余部分是pretty复杂,你不必管它。最终的可运行游戏=新的Runnable()//此线程/ Runnable接口是用于播放
// 声音
{
公共无效的run()
{
尝试
{
//检查音频文件是.wav文件
如果(sound.getName()。与toLowerCase()。包含(WAV))
{ 如果(format.getEncoding()!= AudioFormat.Encoding.PCM_SIGNED)
{
格式=新AudioFormat的(AudioFormat.Encoding.PCM_SIGNED,format.getSampleRate(),format.getSampleSizeInBits()* 2,format.getChannels(),format.getFrameSize()* 2,format.getFrameRate(),真); 流= AudioSystem.getAudioInputStream(格式,流);
} SourceDataLine.Info信息=新DataLine.Info(SourceDataLine.class,stream.getFormat(),(INT)(stream.getFrameLength()* format.getFrameSize())); SourceDataLine的行=(SourceDataLine的)AudioSystem.getLine(信息);
line.open(stream.getFormat());
line.start(); //设置音量
FloatControl volume_control =(FloatControl)line.getControl(FloatControl.Type.MASTER_GAIN);
volume_control.setValue((浮点)(将Math.log(体积/ 100.0f)/将Math.log(10.0f)* 20.0f)); // 静音
BooleanControl mute_control =(BooleanControl)line.getControl(BooleanControl.Type.MUTE);
mute_control.setValue(静音); FloatControl pan_control =(FloatControl)line.getControl(FloatControl.Type.PAN);
pan_control.setValue(PAN); INT num_read = 0;
字节[] buf中=新的字节[line.getBufferSize()]; 而((num_read = stream.read(BUF,0,buf.length))GT; = 0)
{
INT偏移= 0; 而(偏移< num_read)
{
胶印+ = line.write(BUF,偏移,num_read - 偏移);
}
} line.drain();
line.stop(); 如果(looped_forever)
{
新的Thread(播放)。开始();
}否则如果(loops_done< loop_times)
{
loops_done ++;
新的Thread(播放)。开始();
}
}
}赶上(异常前)
{
ex.printStackTrace();
}
}
};
公众诠释的getLength()
{
长audioFileLength = sound.length();
INT框架尺寸= format.getFrameSize();
浮帧率= format.getFrameRate();
浮动durationInSeconds =(audioFileLength /(框架尺寸*帧率));
返回(INT)durationInSeconds;
}公共无效一个setPan(浮锅)
{
this.pan =锅;
}公众持股量getPan()
{
返回锅;
}公共无效setLoop(布尔环)
{
looped_forever =环路;
}公共布尔getLoop()
{
返回looped_forever;
}公共字符串的getName()
{
返回名称;
}公共无效setname可以(字符串名称)
{
this.name =名称;
}
}
编辑:我需要能够所以如果你有另一个类来控制音量,请确保它可以控制音量
!解决方案
(RE
剪辑
)..how你控制音量?
块引用>尝试上看到 Java的声音标签维基中的code的这种变异。
进口的java.net.URL;
进口javax.sound.sampled中*。
进口的javax.swing *。
javax.swing.event中导入*。公共类volume control还{ 公共静态无效的主要(字串[] args)抛出异常{
网址URL =新的URL(
的http://ps$c$c.org/media/leftright.wav);
夹夹= AudioSystem.getClip();
// getAudioInputStream()也接受文件或文件的InputStream
AIS的AudioInputStream = AudioSystem。
getAudioInputStream(URL);
clip.open(AIS);
控制[] C = clip.getControls();
FloatControl温度= NULL;
对于(控制控制:C){
的System.out.println(对照组);
如果(control.toString()。与toLowerCase()。包含(主增益)){
//我们找到了!
TEMP =(FloatControl)控制;
}
}
最后FloatControl体积=温度;
clip.loop(Clip.LOOP_CONTINUOUSLY);
SwingUtilities.invokeLater(Runnable的新(){
公共无效的run(){
JComponent的C = NULL;
如果(卷!= NULL){
最后JSlider的volControl = JSlider的新(
(中间体)(100 * vol.getMinimum()),
(中间体)(100 * vol.getMaximum()),
(中间体)(100 * vol.getValue())
);
的ChangeListener CL =新的ChangeListener(){ @覆盖
公共无效stateChanged(的ChangeEvent发送){
的System.out.println(音量+ volControl.getValue()/ 100°F);
vol.setValue(volControl.getValue()/ 100°F);
}
};
volControl.addChangeListener(CL);
C = volControl;
}其他{
C =新的JLabel(关闭退出!);
}
JOptionPane.showMessageDialog(NULL,C);
}
});
}
}I'm trying to develop a game and I've found 2 sources for sound.
I posted the whole class below, it won't seem to loop even when
looped_forever
orloop_times
is setup to do so: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; } }
Edit: I need to be able to control volume so if you have another class please make sure it can control volume!
解决方案(re
Clip
) ..how do you control volume?Try this variant of the code seen on the Java Sound tag Wiki.
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); } }); } }
这篇关于Java的声音类中没有循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文