更新时间:2022-03-03 06:48:20
AudioSystem.getAudioInputStream
方法已重载,可以接受 File
, URL
或 InputStream
作为参数.其中, URL
通常是***选择.
The AudioSystem.getAudioInputStream
method is overloaded and can accept a File
, a URL
or an InputStream
as an argument. Of these, URL
is usually the best choice.
与 File
相比,优点是 URL
可以寻址jar中的文件,而 File
则不能.
It's advantage over File
is that a URL
can address a file within a jar, which a File
cannot do.
与 InputStream
相比,它的优势在于附加了附加条件:音频文件必须支持 mark
和 reset
方法.我在音频文件方面的经验是,无论是否可行,都会有一定的成败.我承认我不知道具体原因.
It's advantage over InputStream
is that additional conditions are imposed: the audio file must support mark
and reset
methods. My experience with audio files is that it's kind of hit or miss as to whether this will be possible or not. I confess I don't know the specifics as to why.
要从jar中获取URL,最简单的形式可能是以下形式:
To obtain a URL from your jar, the simplest form it perhaps the following:
URL url = this.getClass().getResource("yourAudioFileName");
这假定音频资源与类"this"在同一包中.包的子文件夹也可以通过这种方式寻址.但是我不认为符号"../"是"../".可以依靠它来从父文件夹获取资源.
This assumes that the audio resource is in the same package as the class "this". Subfolders of the package can be addressed as well, this way. But I don't think the symbol "../" can be counted on to obtain a resource from a parent folder.
或者,您可以在音频资源的包或父包中指定一个类,而不是"this".另外,您可以在地址前面加上"/".在后一种情况下,例如 this.getClass().getResource("/yourAudioFileName")
,项目的根源文件夹是地址的起点,而不是包含"this"的包'.
Alternatively, instead of 'this' you can specify a class in the package or parent package of the audio resource. Also, you can prefix the address with a "/". In this last case, e.g., this.getClass().getResource("/yourAudioFileName")
, the root source folder of the project is the starting point for the address rather than the package holding 'this'.