【问题标题】:AudioInputStream from InputStream ( load from resource directory)InputStream 中的 AudioInputStream(从资源目录加载)
【发布时间】:2016-08-16 11:23:57
【问题描述】:

在 IDE 中试用我的应用程序时,我尝试从资源文件夹加载我的声音。

对于使用 InputStreams 的图像和其他内容,我使用以下方法:

@Override
public InputStream readAsset(String fileName) throws IOException {
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream is = classloader.getResourceAsStream(fileName);
    return is;
}

这让我可以打开一个输入流,我可以从中提取图像。

一旦我尝试将此 InputStream 转换为音频 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);
}

在运行之前也会抛出错误

【问题讨论】:

  • 我没有看到只接受输入流的AudioInputStream 的构造函数。 This Javadoc 表示选项为AudioInputStream(InputStream stream, AudioFormat format, long length)AudioInputStream(TargetDataLine line)AudioSystem.getAudioInputStream(InputStream stream) 方法似乎只采用输入流(如答案中所建议的那样)。但是,请注意 Javadocs 中的限制。
  • 大声笑我怎么知道帧的长度...这有多烦人? AudioSystem.getAudioInputStream(InputStream stream) 这不起作用!它会引发运行时错误

标签: java audio resources inputstream audioinputstream


【解决方案1】:

您不能将InputStream 转换为AudioInputStream(您可以反过来)。 Clip.open() 想要一个 AudioInputStream。

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

因此,请尝试:

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

【讨论】:

  • 我都试过了:见我上面的编辑,但它也给了我错误。
  • @railwanderer,好的。这就是我对音频系统的工作知识的限制。我会稍微删除这个答案,因为它没有帮助。抱歉,这两种方法都不起作用。
  • 没问题仍然非常感谢您尝试帮助我:) 当我弄清楚时我会发布解决方案
  • 我可以完全避免使用这个剪辑的东西,而只使用常规流和一些 .out 方法吗? :D
  • @railwanderer,问题是,刚刚进行了快速搜索,this post 似乎暗示使用AudioSystem.getAudioInputStream() and sending it the URL` 而不是InputStream 应该可以工作。
【解决方案2】:

这使它在我上面的代码中工作:

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

【讨论】:

  • 很高兴您找到了解决方案!
猜你喜欢
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-30
  • 2016-02-18
  • 2016-08-28
  • 2013-12-19
相关资源
最近更新 更多