来自 InputStream 的 AudioInputStream(从资源目录加载)
AudioInputStream from InputStream ( load from resource directory)
在 IDE 中试用我的应用程序时,我尝试从我的资源文件夹加载我的声音。
对于使用 InputStreams 的图像和其他内容,我使用此方法:
@Override
public InputStream readAsset(String fileName) throws IOException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(fileName);
return is;
}
这让我可以打开一个输入流,我可以从中提取图像。
当我尝试将此 InputStream 转换为 Audio InputStream 时,我收到了错误。另外,如果我尝试制作一个新的 AudioInputStream 将上述 InputStream 作为参数传递。
这是我目前从外部路径加载声音的方式:
public class JavaSound implements Sound {
private Clip clip;
public JavaSound(String fileName){
try {
File file = new File(fileName);
if (file.exists()) {
//for external storage Path
AudioInputStream sound = AudioSystem.getAudioInputStream(file);
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
}
else {
throw new RuntimeException("Sound: file not found: " + fileName);
}
}
catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Malformed URL: " + e);
}
catch (UnsupportedAudioFileException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Unsupported Audio File: " + e);
}
catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Input/Output Error: " + e);
}
catch (LineUnavailableException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
}
}
@Override
public void play(float volume) {
// Get the gain control from clip
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
// set the gain (between 0.0 and 1.0)
float gain = volume;
float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
clip.setFramePosition(0); // Must always rewind!
clip.start();
}
@Override
public void dispose() {
clip.close();
}
}
我如何交换 AudioInputStream 部分以像第一个代码一样工作,将文件从我的资源目录中拉出?
编辑:
这种通过传递 InputStream
创建新 AudioInputStream 的方式
File file = new File(fileName);
if (file.exists()) {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(fileName);
//for external storage Path
AudioInputStream sound = new AudioInputStream(is);
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
}
甚至在 运行 之前也会抛出错误
您不能将 InputStream
转换为 AudioInputStream
(您可以反过来)。 Clip.open()
想要一个 AudioInputStream。
by this answer here 建议的一种方法是使用 .getResource()
调用中的 URL,而不是尝试打开 InputStream 然后将其传入。
因此,尝试:
URL soundURL = classloader.getResource(fileName);
AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL);
这使它在我上面的代码中起作用:
public JavaSound(String fileName){
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(fileName);
AudioInputStream sound = AudioSystem.getAudioInputStream(new BufferedInputStream(is));
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
}
catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Malformed URL: " + e);
}
catch (UnsupportedAudioFileException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Unsupported Audio File: " + e);
}
catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Input/Output Error: " + e);
}
catch (LineUnavailableException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
}
}
只需用我的 inputStream 启动一个新的 bufferedInputStream 来获得 AudioInputStream...:D 仍然非常感谢 ;)
在 IDE 中试用我的应用程序时,我尝试从我的资源文件夹加载我的声音。
对于使用 InputStreams 的图像和其他内容,我使用此方法:
@Override
public InputStream readAsset(String fileName) throws IOException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(fileName);
return is;
}
这让我可以打开一个输入流,我可以从中提取图像。
当我尝试将此 InputStream 转换为 Audio InputStream 时,我收到了错误。另外,如果我尝试制作一个新的 AudioInputStream 将上述 InputStream 作为参数传递。
这是我目前从外部路径加载声音的方式:
public class JavaSound implements Sound {
private Clip clip;
public JavaSound(String fileName){
try {
File file = new File(fileName);
if (file.exists()) {
//for external storage Path
AudioInputStream sound = AudioSystem.getAudioInputStream(file);
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
}
else {
throw new RuntimeException("Sound: file not found: " + fileName);
}
}
catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Malformed URL: " + e);
}
catch (UnsupportedAudioFileException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Unsupported Audio File: " + e);
}
catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Input/Output Error: " + e);
}
catch (LineUnavailableException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
}
}
@Override
public void play(float volume) {
// Get the gain control from clip
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
// set the gain (between 0.0 and 1.0)
float gain = volume;
float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
clip.setFramePosition(0); // Must always rewind!
clip.start();
}
@Override
public void dispose() {
clip.close();
}
}
我如何交换 AudioInputStream 部分以像第一个代码一样工作,将文件从我的资源目录中拉出?
编辑: 这种通过传递 InputStream
创建新 AudioInputStream 的方式File file = new File(fileName);
if (file.exists()) {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(fileName);
//for external storage Path
AudioInputStream sound = new AudioInputStream(is);
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
}
甚至在 运行 之前也会抛出错误
您不能将 InputStream
转换为 AudioInputStream
(您可以反过来)。 Clip.open()
想要一个 AudioInputStream。
by this answer here 建议的一种方法是使用 .getResource()
调用中的 URL,而不是尝试打开 InputStream 然后将其传入。
因此,尝试:
URL soundURL = classloader.getResource(fileName);
AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL);
这使它在我上面的代码中起作用:
public JavaSound(String fileName){
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(fileName);
AudioInputStream sound = AudioSystem.getAudioInputStream(new BufferedInputStream(is));
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
}
catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Malformed URL: " + e);
}
catch (UnsupportedAudioFileException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Unsupported Audio File: " + e);
}
catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Input/Output Error: " + e);
}
catch (LineUnavailableException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
}
}
只需用我的 inputStream 启动一个新的 bufferedInputStream 来获得 AudioInputStream...:D 仍然非常感谢 ;)