来自InputStream的AudioInputStream(从资源目录加载)

时间:2016-04-22 17:57:01

标签: java audio resources inputstream audioinputstream

在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);
}

在运行之前也会抛出错误

2 个答案:

答案 0 :(得分:0)

您无法将InputStream投射到AudioInputStream(您可以执行相反的操作)。 Clip.open()想要一个AudioInputStream。

建议by this answer here的方法是使用.getResource()调用中的URL,而不是尝试打开InputStream然后将其传入。

因此,请尝试:

URL soundURL = classloader.getResource(fileName);
AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL);

答案 1 :(得分:0)

这使它适用于我的上述代码:

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仍然非常感谢;)