我在网上找到了这个,并尽我所能地操纵它,但我无法让它工作。
import java.io.*;
import sun.audio.*;
/**
* A simple Java sound file example (i.e., Java code to play a sound file).
* AudioStream and AudioPlayer code comes from a javaworld.com example.
* @author alvin alexander, devdaily.com.
*/
public class SoundTest
{
public static void main(String[] args)
throws IOException
{
// open the sound file as a Java input stream
String gongFile = "C:/Users/jd186856/Desktop/SoundTest/IMALEMON.au";
InputStream in = new FileInputStream(gongFile);
// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);
// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
}
}
以下是错误代码:
Exception in thread "main" java.io.IOException: could not create audio stream from input stream at sun.audio.AudioStream.(AudioStream.java:80) at SoundTest.main(SoundTest.java:23)
感谢您提前提供的任何帮助!!
最佳答案
除了 1997 年的列表之外,我找不到对 Java 类 AudioStream 的引用!
当前的做法是使用 AudioInputStream。这可以在 Java 7 和 8 的 API 中找到。
Java Sound Tutorials是一本很难读的书,但涵盖了当前的实践。请参阅“播放音频”部分。导入声音时,我总是使用 URL,并避免创建 InputStream 作为中间步骤。将InputStreams 转换为AudioInputStreams 的代码可能会对InputStream 应用“标记/重置”测试,这可能会失败。直接从 URL 制作 AudioInputStream 可以避免此测试。
事实上,如果 AudioStream 允许您使用 URL 作为源,将其更改为使用源文件的 URL 可能会修复您的代码,但我不会指望它可以在每个 Java 系统上工作,因为许多 sun 库已被弃用。 (我不确定这个是否已被弃用,但在当前 API 中没有找到它就说明了这一点。)
另一个有时有效的方法是将InputStream包装在BufferedInputStream中,因为此类实现了标记和重置方法,这些方法有时会在尝试使用InputStream时导致错误。
关于javascript - 如何向 jgrasp 代码添加声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37144634/