如何将声音添加到jjump代码中

How do i add sound to jgrasp code?

本文关键字:jjump 代码 添加 声音      更新时间:2023-09-26

我在网上找到了这个,并尽我所能对它进行了操作,但我无法使它发挥作用。

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

以下是错误代码:

线程"main"java.io.io异常:无法创建音频来自处输入流的流sun.audio.AudioStream。(AudioStream.java:80)SoundTest.main(SoundTest.java:23)

感谢您提前提供的帮助!!

除了1997年的列表中,我找不到对Java类AudioStream的引用!

目前的做法是使用AudioInputStream。这可以在用于Java 7的API&8.

Java声音教程很难阅读,但涵盖了当前的实践。请参阅"播放音频"部分。导入声音时,我总是使用URL,并避免将创建InputStream作为中间步骤。将InputStreams转换为AudioInputStreams的代码可能会对可能失败的InputStream应用"标记/重置"测试。直接从URL生成AudioInputStream可以避免此测试。

事实上,如果AudioStream允许您使用URL作为源,那么将其更改为使用源文件的URL可能会修复您的代码,但考虑到许多sun库已经被弃用,我不认为它能在所有Java系统上运行。(我不确定这个是否已经被弃用,但在当前的API中找不到它就说明了问题。)

另一种有时有效的方法是将InputStream封装在BufferedInputStream中,因为此类实现了标记和重置方法,这些方法在尝试使用InputStream时有时会导致错误。