Java 声音控制不工作
Java sound control not working
我的 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;
}
好的,检查你的代码后,到处都是小东西。
在我开始之前,也许你的代码之前巧合地工作了一次,然后就停止工作了,这通常是线程编程不好的时候。
问题是线程在音频文件有机会播放之前开始和结束,因为在这种情况下线程不知道文件有多长。
并且由于我不了解您的程序的行为方式的完整逻辑,因此我将保留您的代码逻辑并添加一些作为独立实例解决问题的内容。顺便说一句,我已经测试了修复的代码,它在我的机器中作为隔离 class 工作得很好。
我会提到解决方案的关键点。它是关于让线程继续播放音频时间,直到它被听者结束。
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();
}*/
}
}
}
我的 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;
}
好的,检查你的代码后,到处都是小东西。
在我开始之前,也许你的代码之前巧合地工作了一次,然后就停止工作了,这通常是线程编程不好的时候。
问题是线程在音频文件有机会播放之前开始和结束,因为在这种情况下线程不知道文件有多长。
并且由于我不了解您的程序的行为方式的完整逻辑,因此我将保留您的代码逻辑并添加一些作为独立实例解决问题的内容。顺便说一句,我已经测试了修复的代码,它在我的机器中作为隔离 class 工作得很好。
我会提到解决方案的关键点。它是关于让线程继续播放音频时间,直到它被听者结束。
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();
}*/
}
}
}