gpt4 book ai didi

Java声音控制不起作用

转载 作者:行者123 更新时间:2023-11-30 02:47:00 25 4
gpt4 key购买 nike

我的 Java 声音播放开始时出现错误,但不播放声音。

我在网上搜索过,但还是不行 D:

你能帮我理解我的问题并解决吗?

如果您需要更多详细信息、代码、转储、输出、数据,请告诉我。

声音.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;
}

最佳答案

好吧,检查你的代码后,到处都有一些小问题。

在我开始之前,也许您的代码之前曾巧合地工作过一次,然后停止工作,这通常是在线程编程不好的情况下发生的。

问题在于线程在音频文件有机会播放之前开始和结束,因为在这种情况下线程不知道文件有多长。

由于我不知道您的程序行为方式的完整逻辑,因此我将保留您的代码逻辑并添加一些可以作为独立实例解决问题的内容。顺便说一句,我已经测试了修复后的代码,它在我的机器中作为独立类运行得很好。

我会提到解决方案的要点。它是让线程继续音频播放时间,直到被监听器结束。

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)而不使用synchronized,但是因为你不知道要 hibernate 多长时间,因为你不知道音频文件有多长!

所以你的最终代码将如下所示,我已将解决方案放入代码中并附有解释,包括带有注释的额外细微更改:

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声音控制不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39921923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com