public final class AudioFormat
extends Object
implements Parcelable
java.lang.Object | |
↳ | android.media.AudioFormat |
AudioFormat
类用于访问许多音频格式和通道配置常量。 它们例如在AudioTrack
和AudioRecord
中用作构造函数的单个参数(如AudioTrack(int, int, int, int, int, int)
)中的AudioTrack(int, int, int, int, int, int)
,其中第四个参数是AudioFormat.ENCODING_*
常量之一。 AudioFormat
常量也用于MediaFormat
中指定媒体中常用的音频相关值,例如KEY_CHANNEL_MASK
。
AudioFormat.Builder
类可用于创建AudioFormat
格式类的实例。 请参阅AudioFormat.Builder
以获取关于配置和构建这些实例的机制的文档。 在这里我们描述AudioFormat
类允许您在每种情况下传达的主要概念,它们是:
紧密地与相关联 AudioFormat
是一个概念 audio frame ,其用于在整个文档,以表示音频数据的最小尺寸完整的单元。
以Hz为AudioFormat
表示, AudioFormat
实例中的采样率表示您正在播放或录制的内容中每秒每个声道的音频采样数。 这不是内容呈现或制作的采样率。 例如,在采样速率为48000Hz的设备上可以播放8000Hz的媒体采样率的声音; 采样率转换由平台自动处理,不会以6倍速播放。
正如API的M
,采样率高达192kHz都支持AudioRecord
和AudioTrack
,并根据需要进行采样速率转换。 为了提高效率并避免有损转换,建议将AudioRecord
和AudioTrack
的采样率与端点设备采样率相匹配,并将采样率限制为不超过48kHz,除非有特殊的设备功能才能保证更高的速率。
音频编码用于描述音频数据的位表示,可以是线性PCM或压缩音频,如AC3或DTS。
对于线性PCM,音频编码描述样本大小,8位,16位或32位,以及样本表示,整数或浮点数。
ENCODING_PCM_8BIT
: The audio sample is a 8 bit unsigned integer in the range [0, 255], with a 128 offset for zero. This is typically stored as a Java byte in a byte array or ByteBuffer. Since the Java byte is signed, be careful with math operations and conversions as the most significant bit is inverted. ENCODING_PCM_16BIT
: The audio sample is a 16 bit signed integer typically stored as a Java short in a short array, but when the short is stored in a ByteBuffer, it is native endian (as compared to the default Java big endian). The short has full range from [-32768, 32767], and is sometimes interpreted as fixed point Q.15 data. ENCODING_PCM_FLOAT
: Introduced in API LOLLIPOP
, this encoding specifies that the audio sample is a 32 bit IEEE single precision float. The sample can be manipulated as a Java float in a float array, though within a ByteBuffer it is stored in native endian byte order. The nominal range of ENCODING_PCM_FLOAT
audio data is [-1.0, 1.0]. It is implementation dependent whether the positive maximum of 1.0 is included in the interval. Values outside of the nominal range are clamped before sending to the endpoint device. Beware that the handling of NaN is undefined; subnormals may be treated as zero; and infinities are generally clamped just like other values for AudioTrack
– try to avoid infinities because they can easily generate a NaN. ENCODING_PCM_FLOAT
for audio capture, processing, and playback. Floats are efficiently manipulated by modern CPUs, have greater precision than 24 bit signed integers, and have greater dynamic range than 32 bit signed integers. AudioRecord
as of API M
and AudioTrack
as of API LOLLIPOP
support ENCODING_PCM_FLOAT
. 对于压缩音频,编码指定压缩方法,例如ENCODING_AC3
和ENCODING_DTS
。 压缩音频数据通常作为字节存储在字节数组或ByteBuffer中。 当为AudioTrack
指定压缩音频编码时,它会创建一个直接(非混合)音轨输出到能够解码压缩音频的端点(如HDMI)。 对于无法解码此类压缩音频的(大多数)其他端点,您需要先解码数据,通常是创建一个MediaCodec
。 或者,可以使用MediaPlayer
播放压缩的音频文件或流。
当通过直接发送压缩音频AudioTrack
,它不需要以音频存取单元的精确倍数写入; 这与MediaCodec
输入缓冲区不同。
在AudioTrack
和AudioRecord
中使用通道掩码来描述音频帧中的采样及其排列。 它们也用于端点(例如USB音频接口,连接到耳机的DAC)以指定特定设备的允许配置。
从API M
,有两种类型的通道掩码:通道位置掩码和通道索引掩码。
BASE
. For input and output, they imply a positional nature - the location of a speaker or a microphone for recording or playback.
channel count | channel position mask |
1 | CHANNEL_OUT_MONO |
2 | CHANNEL_OUT_STEREO |
3 | CHANNEL_OUT_STEREO | CHANNEL_OUT_FRONT_CENTER |
4 | CHANNEL_OUT_QUAD |
5 | CHANNEL_OUT_QUAD | CHANNEL_OUT_FRONT_CENTER |
6 | CHANNEL_OUT_5POINT1 |
7 | CHANNEL_OUT_5POINT1 | CHANNEL_OUT_BACK_CENTER |
8 | CHANNEL_OUT_7POINT1_SURROUND |
CHANNEL_OUT_STEREO
is composed of
CHANNEL_OUT_FRONT_LEFT
and
CHANNEL_OUT_FRONT_RIGHT
.
M
. They allow the selection of a particular channel from the source or sink endpoint by number, i.e. the first channel, the second channel, and so forth. This avoids problems with artificially assigning positions to channels of an endpoint, or figuring what the i
th position bit is within an endpoint's channel position mask etc.
CHANNEL_OUT_QUAD
device, but really one is only interested in channel 0 through channel 3. The USB device would then have the following individual bit channel masks:
CHANNEL_OUT_FRONT_LEFT
,
CHANNEL_OUT_FRONT_RIGHT
,
CHANNEL_OUT_BACK_LEFT
and
CHANNEL_OUT_BACK_RIGHT
. But which is channel 0 and which is channel 3?
1 << channelNumber
. A set bit indicates that channel is present in the audio frame, otherwise it is cleared. The order of the bits also correspond to that channel number's sample order in the audio frame.
0xF
. Suppose we wanted to select only the first and the third channels; this would correspond to a channel index mask
0x5
(the first and third bits set). If an
AudioTrack
uses this channel index mask, the audio frame would consist of two samples, the first sample of each frame routed to channel 0, and the second sample of each frame routed to channel 2. The canonical channel index masks by channel count are given by the formula
(1 << channelCount) - 1
.
CHANNEL_OUT_FRONT_LEFT
, CHANNEL_OUT_FRONT_CENTER
, etc. for HDMI home theater purposes. AudioTrack
to output movie content, where 5.1 multichannel output is to be written. AudioRecord
may only want the third and fourth audio channels of the endpoint (i.e. the second channel pair), and not care the about position it corresponds to, in which case the channel index mask is 0xC
. Multichannel AudioRecord
sessions should use channel index masks. 对于线性PCM,音频帧由一组同时捕获的样本组成,其计数和通道关联由channel mask给出,其样本内容由encoding指定。 例如,立体声16位PCM帧由两个16位线性PCM样本组成,帧大小为4个字节。 对于压缩音频,音频帧可以交替地指代被压缩的数据字节的访问单元,其被逻辑地组合在一起用于解码和比特流访问(例如MediaCodec
),或单个字节的压缩数据(例如AudioTrack.getBufferSizeInFrames()
)或线性PCM帧来自解码压缩数据(例如AudioTrack.getPlaybackHeadPosition()
),这取决于使用音频帧的上下文。
Nested classes |
|
---|---|
class |
AudioFormat.Builder |
Inherited constants |
---|
From interface android.os.Parcelable
|
Fields |
|
---|---|
public static final Creator<AudioFormat> |
CREATOR |
Public methods |
|
---|---|
int |
describeContents() 描述此Parcelable实例的封送表示中包含的特殊对象的种类。 |
boolean |
equals(Object o) 指示其他某个对象是否“等于”这一个。 |
int |
getChannelCount() 返回通道计数。 |
int |
getChannelIndexMask() 返回通道索引掩码。 |
int |
getChannelMask() 返回通道掩码。 |
int |
getEncoding() 返回编码。 |
int |
getSampleRate() 返回采样率。 |
int |
hashCode() 返回对象的哈希码值。 |
String |
toString() 返回对象的字符串表示形式。 |
void |
writeToParcel(Parcel dest, int flags) 将此对象平铺到一个包裹中。 |
Inherited methods |
|
---|---|
From class java.lang.Object
|
|
From interface android.os.Parcelable
|
int CHANNEL_CONFIGURATION_DEFAULT
此常数在API级别5中已弃用。
改为使用CHANNEL_OUT_DEFAULT
或CHANNEL_IN_DEFAULT
。
常数值:1(0x00000001)
int CHANNEL_CONFIGURATION_INVALID
此常数在API级别5中已弃用。
改为使用CHANNEL_INVALID
。
常量值:0(0x00000000)
int CHANNEL_CONFIGURATION_MONO
此常数在API级别5中已弃用。
改为使用CHANNEL_OUT_MONO
或CHANNEL_IN_MONO
。
常量值:2(0x00000002)
int CHANNEL_CONFIGURATION_STEREO
此常数在API级别5中已弃用。
改为使用CHANNEL_OUT_STEREO
或CHANNEL_IN_STEREO
。
常量值:3(0x00000003)
int CHANNEL_OUT_7POINT1
此常数在API级别23中已弃用。
不是典型的7.1环绕配置。 改为使用CHANNEL_OUT_7POINT1_SURROUND
。
常量值:1020(0x000003fc)
int CHANNEL_OUT_7POINT1_SURROUND
常量值:6396(0x000018fc)
int CHANNEL_OUT_FRONT_LEFT_OF_CENTER
常量值:256(0x00000100)
int CHANNEL_OUT_FRONT_RIGHT_OF_CENTER
常量值:512(0x00000200)
int ENCODING_IEC61937
音频数据格式:用于PCM或S / PDIF直通的压缩音频。 IEC61937使用16位样本的立体声流作为包装。 所以轨道的通道掩码必须是CHANNEL_OUT_STEREO
。 数据应该以短的[]数组写入流中。 如果数据写入byte []数组,那么在内部转换为short时,某些平台上可能存在endian问题。
常量值:13(0x0000000d)
int ENCODING_PCM_16BIT
音频数据格式:每个采样PCM 16位。 保证被设备支持。
常量值:2(0x00000002)
int ENCODING_PCM_8BIT
音频数据格式:每个采样PCM 8位。 不保证被设备支持。
常量值:3(0x00000003)
int ENCODING_PCM_FLOAT
音频数据格式:每个采样的单精度浮点数
常量值:4(0x00000004)
int SAMPLE_RATE_UNSPECIFIED
采样率将取决于路线。 对于AudioTrack,它通常是信源采样率,对于AudioRecord它通常是源采样率。
常量值:0(0x00000000)
int describeContents ()
描述此Parcelable实例的封送表示中包含的特殊对象的种类。 例如,如果对象将在writeToParcel(Parcel, int)
的输出中包含writeToParcel(Parcel, int)
,则此方法的返回值必须包含CONTENTS_FILE_DESCRIPTOR
位。
Returns | |
---|---|
int |
a bitmask indicating the set of special object types marshaled by this Parcelable object instance. |
boolean equals (Object o)
指示其他某个对象是否“等于”这一个。
equals
方法在非空对象引用上实现等价关系:
x
, x.equals(x)
should return true
. x
and y
, x.equals(y)
should return true
if and only if y.equals(x)
returns true
. x
, y
, and z
, if x.equals(y)
returns true
and y.equals(z)
returns true
, then x.equals(z)
should return true
. x
and y
, multiple invocations of x.equals(y)
consistently return true
or consistently return false
, provided no information used in equals
comparisons on the objects is modified. x
, x.equals(null)
should return false
. 类Object
的equals
方法实现了对象上最可能的等价关系; 也就是说,对于任何非空参考值x
和y
,当且仅当x
和y
引用同一对象( x == y
的值为true
)时,此方法返回true
。
请注意,无论何时重写此方法,通常都需要覆盖 hashCode
方法,以便维护 hashCode
方法的一般合同,该方法声明相等对象必须具有相同的哈希代码。
Parameters | |
---|---|
o |
Object : the reference object with which to compare. |
Returns | |
---|---|
boolean |
true if this object is the same as the obj argument; false otherwise. |
int getChannelCount ()
返回通道计数。
Returns | |
---|---|
int |
the channel count derived from the channel position mask or the channel index mask. Zero is returned if both the channel position mask and the channel index mask are not set. |
int getChannelIndexMask ()
返回通道索引掩码。 有关基于索引的掩码和基于位置的掩码(由getChannelMask()
返回)之间的差异的更多信息,请参阅channel masks上的部分。
Returns | |
---|---|
int |
one of the values that can be set in setChannelIndexMask(int) or CHANNEL_INVALID if not set or an invalid mask was used. |
int getChannelMask ()
返回通道掩码。 有关基于索引的掩码(由getChannelIndexMask()
返回)与此函数返回的基于位置的掩码之间的差异的详细信息,请参阅channel masks上的部分。
Returns | |
---|---|
int |
one of the values that can be set in setChannelMask(int) or CHANNEL_INVALID if not set. |
int getEncoding ()
返回编码。 有关支持的音频编码的不同类型的更多信息,请参阅encodings上的章节。
Returns | |
---|---|
int |
one of the values that can be set in setEncoding(int) or ENCODING_INVALID if not set. |
int getSampleRate ()
返回采样率。
Returns | |
---|---|
int |
one of the values that can be set in setSampleRate(int) or SAMPLE_RATE_UNSPECIFIED if not set. |
int hashCode ()
返回对象的哈希码值。 为了散列表的好处而支持此方法,例如HashMap
提供的HashMap
。
hashCode
的总合同是:
hashCode
method must consistently return the same integer, provided no information used in equals
comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. equals(Object)
method, then calling the hashCode
method on each of the two objects must produce the same integer result. equals(java.lang.Object)
method, then calling the hashCode
method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables. 尽可能合理实用,由类Object
定义的hashCode方法确实为不同的对象返回不同的整数。 (这通常通过将对象的内部地址转换为整数来实现,但Java TM编程语言不需要此实现技术。)
Returns | |
---|---|
int |
a hash code value for this object. |
String toString ()
返回对象的字符串表示形式。 一般来说, toString
方法返回一个“文本表示”该对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。
类Object
的toString
方法返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @
”以及对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns | |
---|---|
String |
a string representation of the object. |
void writeToParcel (Parcel dest, int flags)
将此对象平铺到一个包裹中。
Parameters | |
---|---|
dest |
Parcel : The Parcel in which the object should be written. |
flags |
int : Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE . |