了解 AudioFormat 、 AudioInputStream 的构造函数和 start 方法

发布于 2024-11-27 08:10:15 字数 720 浏览 1 评论 0原文

我曾尝试编写播放声音文件的程序,但到目前为止尚未成功。 我无法理解代码的某些部分:

InputStream is = new FileInputStream("sound file");
   AudioFormat af = new AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian); // I don't understand it's constructor
   long length ; // length in sample frames
   // how cani i know the length of frames ?
   AudioInputStream ais = new AudioInputStream( is , af , length );
   // open ( ais );
   // start playing by invoking start method
  • AudioFormat 的构造函数中,我如何提前知道采样率、文件大小、通道是什么以及最后的 2 个布尔变量?
  • 如何获取样本帧的值(length)
  • 另外我如何调用start方法?我不希望来自任何行的数据,而是来自保存在文件夹中的文件(即剪辑)

I have tried writing program that plays a sound file but have been unsuccessful so far.
I am unable to understand some parts of the code:

InputStream is = new FileInputStream("sound file");
   AudioFormat af = new AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian); // I don't understand it's constructor
   long length ; // length in sample frames
   // how cani i know the length of frames ?
   AudioInputStream ais = new AudioInputStream( is , af , length );
   // open ( ais );
   // start playing by invoking start method
  • In the constructor of AudioFormat how can I know the sample rate, file size in advance, what are channels, and the 2 boolean variable in the end?
  • How can I get the value of sample frames (length)?
  • Also how do I invoke start method? I don't want the data from any line but from the file kept in a folder (i.e a clip)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2024-12-04 08:10:15

除了编码之外,音频格式还包括其他
进一步指定数据的确切排列的属性。
其中包括通道数、采样率、样本大小、字节
顺序、帧速率和帧大小。声音可能有不同的数字
音频通道数:一个用于单声道,两个用于立体声。采样率
测量声压的“快照”(样本)数量
每个通道每秒拍摄一次。 (如果声音是立体声而不是
单声道,每个时刻实际上测量两个样本:一个
一个用于左通道,另一个用于右通道;然而,
采样率仍然衡量每个通道的数量,因此速率是
无论通道数量如何,都相同。这是标准使用
)样本大小表示使用多少位来存储
每个快照; 8和16是典型值。对于 16 位样本(或任何
其他样本大小大于一个字节),字节顺序很重要;这
每个样本中的字节按“小端”或“小端”排列
“大端”风格。对于像 PCM 这样的编码,帧由
给定时间点所有通道的样本集,因此
帧的大小(以字节为单位)始终等于样本的大小(以字节为单位)
字节)乘以通道数。然而,与一些其他类型的
编码一帧可以包含一组整体的压缩数据
系列样本,以及附加的非样本数据。对于这样的
编码中,采样率和样本大小指的是其后面的数据
被解码成PCM,所以它们与
帧速率和帧大小。

链接

In addition to the encoding, the audio format includes other
properties that further specify the exact arrangement of the data.
These include the number of channels, sample rate, sample size, byte
order, frame rate, and frame size. Sounds may have different numbers
of audio channels: one for mono, two for stereo. The sample rate
measures how many "snapshots" (samples) of the sound pressure are
taken per second, per channel. (If the sound is stereo rather than
mono, two samples are actually measured at each instant of time: one
for the left channel, and another for the right channel; however, the
sample rate still measures the number per channel, so the rate is the
same regardless of the number of channels. This is the standard use of
the term.) The sample size indicates how many bits are used to store
each snapshot; 8 and 16 are typical values. For 16-bit samples (or any
other sample size larger than a byte), byte order is important; the
bytes in each sample are arranged in either the "little-endian" or
"big-endian" style. For encodings like PCM, a frame consists of the
set of samples for all channels at a given point in time, and so the
size of a frame (in bytes) is always equal to the size of a sample (in
bytes) times the number of channels. However, with some other sorts of
encodings a frame can contain a bundle of compressed data for a whole
series of samples, as well as additional, non-sample data. For such
encodings, the sample rate and sample size refer to the data after it
is decoded into PCM, and so they are completely different from the
frame rate and frame size.

Link

心是晴朗的。 2024-12-04 08:10:15

解决此问题的更好方法可能是沿着 中显示的“播放 Clip”源代码。 Java 声音信息。 页面。它使大多数问题变得多余(因为我们在使用 Clip 时不需要担心细节)。

如果您在尝试来源后还有任何其他问题,请告诉我。

Probably a better way to approach this is along the lines of the 'Playing a Clip' source code shown in the Java Sound info. page. It makes most of the questions redundant (since we don't need to worry about the fine details when using a Clip).

If you have any further questions after trying the source, let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文