发行播放音频与#1的javasound标记示例 [英] Issue playing audio with Stackoverflow's javasound tag example

查看:152
本文介绍了发行播放音频与#1的javasound标记示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读有关 javasound 标记前面标记的问题,我试图实现简单的code,我们可以看到有(有一些小的修改,如使用文件而不是网​​址

Reading about the javasound tag earlier today, I tried to implement the simple code that we can see there (with some minor modifications, such as using File instead of URL):

File file = new File(System.getProperty("user.dir") + "/sound.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
clip.open(ais); // exception

但我发现了一个 java.lang.IllegalArgumentException异常:无效的格式当我尝试打开的AudioInputStream 我们看到在那里。

But I'm getting a java.lang.IllegalArgumentException: Invalid format when I try to open the AudioInputStream that we see there.

然而,当我尝试用下面的code,我从网上找来

However, when I try it with the following code that I got from the internet

File file = new File(System.getProperty("user.dir") + "/sound.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
AudioFormat format = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(ais);
clip.start();

它的确实工作 sound.wav 播放正确 - 但是,我觉得这是笨重的东西,应该是那样简单我们的第一个例子,在 javasound 标签本身。

it does work: sound.wav is played correctly - however, I find this to be clunky for something that should be as simple as our first example, given on the javasound tag itself.

通过从片段阅读文档,我承认,抛出:IllegalArgumentException 被抛出时, [...]流的音频格式未完全指定或无效。然而,这似乎是有效的格式。

By reading the documentation from clip, I acknowledged that IllegalArgumentException is thrown when [...] the stream's audio format is not fully specified or invalid. However, it does seem to be a valid format.

什么是错在这里我们的第一个例子吗? <一href=\"http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html#getAudioFileFormat(java.io.File)\"相对=nofollow> AudioSystem.getAudioInputStream()不接受文件作为参数,并且,从第二个例子可见,它似乎拥有有效音频文件数据,因为它的工作原理。我失去了一些东西明显?而且,如果是的话,应该不是指出在 javasound 标签?

What is wrong with our first example here? AudioSystem.getAudioInputStream() does accept a File as argument, and, as visible from the second example, it does appear to have valid audio file data, because it works. Am I missing something obvious? And, if so, shouldn't it be pointed out on javasound tag?

推荐答案

一些研究,我发现它可能有一些做的系统属性之后。在这些属性可以指定,则对getline()方法(函数getline(),getClip(),getSourceDataLine()和getTargetDataLine())的默认值。如果调用这些方法之一,Java的执行以下操作(的 AudioSystem描述):

After some research I found out that it could have something to do with the system properties. In those properties you can specify defaults for the getLine() methods (getLine(), getClip(), getSourceDataLine() and getTargetDataLine()). If you call one of these methods, Java does the following (AudioSystem description):

该系统javax.sound.sampled.Clip中属性设置为

The system property javax.sound.sampled.Clip is set to

= javax.sound.sampled.Clip中的#great.Mixer great.clip.Clap

javax.sound.sampled.Clip = great.Mixer#great.clip.Clap

混音器类被称为的 great.Mixer 的和剪辑名为 great.clip.Clap

The Mixer class is called great.Mixer and the Clip is called great.clip.Clap

当你请求一个剪辑,Java的检查以下内容:

When you request a Clip, Java checks the following:


  • 如果调音台的 great.Mixer 的被发现,包含剪辑的 great.clip.Clap 的,返回此Clip

  • 如果调音台的 great.Mixer 的被发现,但不包含剪辑的 great.clip.Clap 的,返回调音台
  • 指定的第一个剪辑
  • 如果调音台的 great.Mixer 的不包含任何剪辑或者没有发现,任何调音台返回的 great.clip.Clap 的第一个实例

  • 如果没有搅拌机中包含的 great.clip.Clap 的,返回第一混频器的第一个Clip发现

  • 如果任何调音台没有指定剪辑,抛出一个抛出:IllegalArgumentException

  • If the Mixer great.Mixer is found and contains the Clip great.clip.Clap, return this Clip
  • If the Mixer great.Mixer is found, but does not contain the Clip great.clip.Clap, return the first Clip specified in the Mixer
  • If the Mixer great.Mixer does not contain any Clips or if it isn't found, return the first instance of great.clip.Clap in any Mixer
  • If no Mixer contains great.clip.Clap, return the first Clip of the first Mixer found
  • If no Clip is specified in any Mixer, throw an IllegalArgumentException

这也就意味着,如果你得到一个IllegalArgumentException,没有剪辑安装在任何搅拌机。

What that means is that if you are getting an IllegalArgumentException, no Clips are installed in any Mixers.

通过

DataLine.Info info = new DataLine.Info(Clip.class, format);

要定义一个新的剪辑。这意味着你的下一次调用一个函数getline()方法将返回这个对象,而不是给你一个错误。

you are defining a new Clip. Which means your next Call to a getLine() method will return this object instead of giving you an error.

这篇关于发行播放音频与#1的javasound标记示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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