我的Java声音播放以错误开始但不播放声音。
我已经在网上搜索了但它仍然没有工作D:
你能帮我理解我的问题+修复吗?
如果您需要更多详细信息,代码,转储,输出,数据,请告诉我。
Sound.java:
package com.itaysharon.questematic;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import com.itaysharon.questematic.enums.SoundOptions;
public class Sound {
public static Thread dj;
public static synchronized void playSound(final String url, SoundOptions mode) {
dj = new Thread(new Runnable() {
@Override
public void run() {
try {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("assets" + File.separator + url));
AudioSystem.getClip().open(inputStream);
AudioSystem.getClip().setFramePosition(0);
switch(mode) {
case Stop:
AudioSystem.getClip().stop();
break;
case Play:
AudioSystem.getClip().start();
break;
case Loop:
AudioSystem.getClip().loop(Clip.LOOP_CONTINUOUSLY);
break;
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
});
if (mode != SoundOptions.Stop) {
dj.start();
} else {
try {
AudioSystem.getClip().stop();
AudioSystem.getClip().close();
dj.interrupt();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
SoundOptions.java:
package com.itaysharon.questematic.enums;
public enum SoundOptions {
Play, Loop, Stop;
}
答案 0 :(得分:2)
检查完代码后确定,这里和那里都有很小的东西。
在我开始之前,也许你的代码曾经巧合地工作过一段时间并且之后停止工作,这通常是在线程没有很好地编程时。
问题是线程在音频文件有机会播放之前开始和结束,因为线程在这种情况下不知道文件有多长。
由于我没有关于程序行为的完整逻辑,我将保留你的代码逻辑并添加一些解决问题的东西作为一个独立的实例。顺便说一句,我已经测试了固定的代码,它在我的机器中作为隔离类很有用。
我会提到解决方案的关键点。它是关于让线程继续音频播放时间,直到它被听众结束。
audioLineClip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
... listen when audio is ended and close the line. to end the program.
}
});
我们让我们的线程一直等到音频结束
synchronized (dj) {
while (true) {
try {
dj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
理论上你可以单独使用Thread.sleep(sometime)
而不同步,但因为你不知道有多长时间睡觉,因为你不知道音频文件有多长!
所以你的最终代码看起来像这样,我把解决方案放在代码中,并附带说明包括额外的小改动:
import javax.sound.sampled.*;
import java.io.File;
/**
* By Maytham on 07-10-2016.
*/
public class Sound {
public static void main(String[] args) {
playSound("8k16bitpcm.wav", SoundOptions.Play);
}
// 1) make it private
private static Thread dj;
// 2) make it private and 3) SoundOptions should be final
private static synchronized void playSound(final String url, final SoundOptions mode) {
dj = new Thread(new Runnable() {
@Override
public void run() {
try {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
new File("assets" + File.separator + url));
// 4) declare AudioSystem in stead of using AudioSystem repeatedly
final Clip audioLineClip = (Clip) AudioSystem.getLine(
new Line.Info(Clip.class));
audioLineClip.open(inputStream);
audioLineClip.setFramePosition(0);
// 5) our line listener checks when audio is ended and stops the line
//this is full example, but you manipulated your way
audioLineClip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
LineEvent.Type type = event.getType();
if (type == LineEvent.Type.OPEN) {
} else if (type == LineEvent.Type.CLOSE) {
System.exit(0);
} else if (type == LineEvent.Type.START) {
} else if (type == LineEvent.Type.STOP) {
audioLineClip.close();
}
}
});
switch (mode) {
case Stop:
audioLineClip.stop();
break;
case Play:
audioLineClip.start();
break;
case Loop:
audioLineClip.loop(Clip.LOOP_CONTINUOUSLY);
break;
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
});
if (mode != SoundOptions.Stop) {
dj.start();
// 6) this keep the thread until some line listener change status
synchronized (dj) {
while (true) {
try {
dj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} else {
dj.interrupt();
// 7) you do not need this it is done by line listener
/*try {
AudioSystem.getClip().stop();
AudioSystem.getClip().close();
dj.interrupt();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}
}