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

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

问题描述

我只想完成一个简单的任务。 (我是一个Java新手)。我想要一个按钮被点击时播放音频剪辑。
这里是我的code的部分(我确实做到了通过复制从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视频这个code的工作,我已经使用了相同的code请大家帮帮忙,并再次,我是一个新手。);

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

推荐答案

太阳包中的类应引起在编译时的一些信息的警告。留意他们。不要在包层次使用类。他们是无证,不会从一个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声音基于剪辑来播放音频。请参阅信息。页面的工作示例。

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

这可能是WAV是EN在Java的声音不支持的格式codeD。媒体格式通常是容器格式,可能是连接使用任意数量的不同codeCS的codeD。它很可能是WAV是使用多个COM pressive $ C $立方厘米如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格式的服务提供者接口的,但如果是连接codeD的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.

也改变形式的code:

Also change code of the form:

catch (Exception e) { .. 

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

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

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