使用 Sun.audio 在 java 中播放音频 [英] Playing audio in java using Sun.audio

查看:28
本文介绍了使用 Sun.audio 在 java 中播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想执行一个简单的任务.(我是 Java 新手).我想在单击按钮时播放音频剪辑.这是我的代码的一部分(我完全是通过从 Youtube 复制教程来完成的.)

I want just to perform a simple task. (I'm a java newbie). I want to play an audio clip when a button is clicked. here's the part of my code(which I did exactly by copying a tutorial from Youtube.)

private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {                                         
InputStream in;
try{
      in=new FileInputStream(new File("C:\Users\Matt\Documents\dong.wav"));
      AudioStream timeupsound=new AudioStream(in);
      AudioPlayer.player.start(timeupsound);

}
catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}

}                                     

但问题是,这行不通.它抛出 IOException 说:无法从输入流创建音频流".我的问题是,我做错了什么?(因为我清楚地看到这段代码在那个 youtube 视频中有效,我使用了相同的代码.请帮忙.再一次,我是新手);

But the problem is, this doesn't work. It throws and IOException saying: "could not create audio stream from input stream". My question is, what am I doing wrong? (as I clearly saw this code work in that youtube video, and I've used the same code. Please help. and once again, I'm a newbie);

推荐答案

sun 包类应该会在编译时引起一些信息性警告.听他们的.不要使用该包层次结构中的类.它们是未记录的,不能保证从一个 Java 版本到下一个版本,并且可能根本无法在非 Oracle JRE 中使用.

The sun package classes should be causing some informative warnings at compile time. Heed them. Don't use classes in that package hierarchy. They are undocumented, are not guaranteed from one Java version to the next, and will probably not be available in a non-Oracle JRE at all.

改为使用基于 Java Sound 的 Clip 来播放音频.请参阅信息.页面工作示例.

Instead use the Java Sound based Clip to play audio. See the info. page for working examples.

可能是 WAV 以 Java Sound 不支持的格式编码.媒体格式通常是容器格式",可以使用任意数量的不同编解码器进行编码.WAV 很可能在内部使用了更压缩的编解码器,例如 MP3 或 OGG.

It might be the WAV is encoded in a format that Java Sound does not support. Media formats are typically 'container formats' that might be encoded using any number of different Codecs. It is likely that WAV is using a more compressive Codec such as MP3 or OGG internally.

我不知道 OGG 格式的服务提供者接口,但如果它们被编码为 MP3,您可能能够使用 MP3 SPI 使其工作.查看信息.详情请见上方链接.

I don't know of a Service Provider Interface for the OGG format, but if they are encoded as MP3, you might be able to get it working using the MP3 SPI. See the info. page linked above for details.

还要更改表单的代码:

catch (Exception e) { .. 

catch (Exception e) { 
    e.printStackTrace(); // very informative! ... 

这篇关于使用 Sun.audio 在 java 中播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆