Most visited

Recently visited

Added in API level 3

AudioFormat

public final class AudioFormat
extends Object implements Parcelable

java.lang.Object
   ↳ android.media.AudioFormat


AudioFormat类用于访问许多音频格式和通道配置常量。 它们例如在AudioTrackAudioRecord中用作构造函数的单个参数(如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类允许您在每种情况下传达的主要概念,它们是:

  1. sample rate
  2. encoding
  3. channel masks

紧密地与相关联 AudioFormat是一个概念 audio frame ,其用于在整个文档,以表示音频数据的最小尺寸完整的单元。

Sample rate

以Hz为AudioFormat表示, AudioFormat实例中的采样率表示您正在播放或录制的内容中每秒每个声道的音频采样数。 这不是内容呈现或制作的采样率。 例如,在采样速率为48000Hz的设备上可以播放8000Hz的媒体采样率的声音; 采样率转换由平台自动处理,不会以6倍速播放。

正如API的M ,采样率高达192kHz都支持AudioRecordAudioTrack ,并根据需要进行采样速率转换。 为了提高效率并避免有损转换,建议将AudioRecordAudioTrack的采样率与端点设备采样率相匹配,并将采样率限制为不超过48kHz,除非有特殊的设备功能才能保证更高的速率。

Encoding

音频编码用于描述音频数据的位表示,可以是线性PCM或压缩音频,如AC3或DTS。

对于线性PCM,音频编码描述样本大小,8位,16位或32位,以及样本表示,整数或浮点数。

对于压缩音频,编码指定压缩方法,例如ENCODING_AC3ENCODING_DTS 压缩音频数据通常作为字节存储在字节数组或ByteBuffer中。 当为AudioTrack指定压缩音频编码时,它会创建一个直接(非混合)音轨输出到能够解码压缩音频的端点(如HDMI)。 对于无法解码此类压缩音频的(大多数)其他端点,您需要先解码数据,通常是创建一个MediaCodec 或者,可以使用MediaPlayer播放压缩的音频文件或流。

当通过直接发送压缩音频AudioTrack ,它不需要以音频存取单元的精确倍数写入; 这与MediaCodec输入缓冲区不同。

Channel mask

AudioTrackAudioRecord中使用通道掩码来描述音频帧中的采样及其排列。 它们也用于端点(例如USB音频接口,连接到耳机的DAC)以指定特定设备的允许配置。
从API M ,有两种类型的通道掩码:通道位置掩码和通道索引掩码。

Channel position masks
Channel position masks are the original Android channel masks, and are used since API BASE. For input and output, they imply a positional nature - the location of a speaker or a microphone for recording or playback.
For a channel position mask, each allowed channel position corresponds to a bit in the channel mask. If that channel position is present in the audio frame, that bit is set, otherwise it is zero. The order of the bits (from lsb to msb) corresponds to the order of that position's sample in the audio frame.
The canonical channel position masks by channel count are as follows:
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

These masks are an ORed composite of individual channel masks. For example CHANNEL_OUT_STEREO is composed of CHANNEL_OUT_FRONT_LEFT and CHANNEL_OUT_FRONT_RIGHT.
Channel index masks
Channel index masks are introduced in API 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.
Here's an example where channel index masks address this confusion: dealing with a 4 channel USB device. Using a position mask, and based on the channel count, this would be a 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?
For a channel index mask, each channel number is represented as a bit in the mask, from the lsb (channel 0) upwards to the msb, numerically this bit value is 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.
For the previous 4 channel USB device example, the device would have a channel index mask 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.
Use cases

Audio Frame

对于线性PCM,音频帧由一组同时捕获的样本组成,其计数和通道关联由channel mask给出,其样本内容由encoding指定。 例如,立体声16位PCM帧由两个16位线性PCM样本组成,帧大小为4个字节。 对于压缩音频,音频帧可以交替地指代被压缩的数据字节的访问单元,其被逻辑地组合在一起用于解码和比特流访问(例如MediaCodec ),或单个字节的压缩数据(例如AudioTrack.getBufferSizeInFrames() )或线性PCM帧来自解码压缩数据(例如AudioTrack.getPlaybackHeadPosition() ),这取决于使用音频帧的上下文。

Summary

Nested classes

class AudioFormat.Builder

AudioFormat对象的生成器类。

Constants

int CHANNEL_CONFIGURATION_DEFAULT

此常数在API级别5中已弃用。请改为使用CHANNEL_OUT_DEFAULTCHANNEL_IN_DEFAULT

int CHANNEL_CONFIGURATION_INVALID

此常数在API级别5中已弃用。请改为使用CHANNEL_INVALID

int CHANNEL_CONFIGURATION_MONO

此常数在API级别5中已弃用。请改为使用CHANNEL_OUT_MONOCHANNEL_IN_MONO

int CHANNEL_CONFIGURATION_STEREO

此常数在API级别5中已弃用。请改为使用CHANNEL_OUT_STEREOCHANNEL_IN_STEREO

int CHANNEL_INVALID

音频通道掩码无效

int CHANNEL_IN_BACK

int CHANNEL_IN_BACK_PROCESSED

int CHANNEL_IN_DEFAULT

int CHANNEL_IN_FRONT

int CHANNEL_IN_FRONT_PROCESSED

int CHANNEL_IN_LEFT

int CHANNEL_IN_LEFT_PROCESSED

int CHANNEL_IN_MONO

int CHANNEL_IN_PRESSURE

int CHANNEL_IN_RIGHT

int CHANNEL_IN_RIGHT_PROCESSED

int CHANNEL_IN_STEREO

int CHANNEL_IN_VOICE_DNLINK

int CHANNEL_IN_VOICE_UPLINK

int CHANNEL_IN_X_AXIS

int CHANNEL_IN_Y_AXIS

int CHANNEL_IN_Z_AXIS

int CHANNEL_OUT_5POINT1

int CHANNEL_OUT_7POINT1

该常数在API级别23中已弃用。不是典型的7.1环绕配置。 改为使用CHANNEL_OUT_7POINT1_SURROUND

int CHANNEL_OUT_7POINT1_SURROUND

int CHANNEL_OUT_BACK_CENTER

int CHANNEL_OUT_BACK_LEFT

int CHANNEL_OUT_BACK_RIGHT

int CHANNEL_OUT_DEFAULT

默认音频通道掩码

int CHANNEL_OUT_FRONT_CENTER

int CHANNEL_OUT_FRONT_LEFT

int CHANNEL_OUT_FRONT_LEFT_OF_CENTER

int CHANNEL_OUT_FRONT_RIGHT

int CHANNEL_OUT_FRONT_RIGHT_OF_CENTER

int CHANNEL_OUT_LOW_FREQUENCY

int CHANNEL_OUT_MONO

int CHANNEL_OUT_QUAD

int CHANNEL_OUT_SIDE_LEFT

int CHANNEL_OUT_SIDE_RIGHT

int CHANNEL_OUT_STEREO

int CHANNEL_OUT_SURROUND

int ENCODING_AC3

音频数据格式:AC-3压缩

int ENCODING_DEFAULT

默认音频数据格式

int ENCODING_DTS

音频数据格式:DTS压缩

int ENCODING_DTS_HD

音频数据格式:DTS HD压缩

int ENCODING_E_AC3

音频数据格式:E-AC-3压缩

int ENCODING_IEC61937

音频数据格式:用于PCM或S / PDIF直通的压缩音频。

int ENCODING_INVALID

音频数据格式无效

int ENCODING_PCM_16BIT

音频数据格式:每个采样PCM 16位。

int ENCODING_PCM_8BIT

音频数据格式:每个采样PCM 8位。

int ENCODING_PCM_FLOAT

音频数据格式:每个采样的单精度浮点数

int SAMPLE_RATE_UNSPECIFIED

采样率将取决于路线。

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

Constants

CHANNEL_CONFIGURATION_DEFAULT

Added in API level 3
int CHANNEL_CONFIGURATION_DEFAULT

此常数在API级别5中已弃用。
改为使用CHANNEL_OUT_DEFAULTCHANNEL_IN_DEFAULT

常数值:1(0x00000001)

CHANNEL_CONFIGURATION_INVALID

Added in API level 3
int CHANNEL_CONFIGURATION_INVALID

此常数在API级别5中已弃用。
改为使用CHANNEL_INVALID

常量值:0(0x00000000)

CHANNEL_CONFIGURATION_MONO

Added in API level 3
int CHANNEL_CONFIGURATION_MONO

此常数在API级别5中已弃用。
改为使用CHANNEL_OUT_MONOCHANNEL_IN_MONO

常量值:2(0x00000002)

CHANNEL_CONFIGURATION_STEREO

Added in API level 3
int CHANNEL_CONFIGURATION_STEREO

此常数在API级别5中已弃用。
改为使用CHANNEL_OUT_STEREOCHANNEL_IN_STEREO

常量值:3(0x00000003)

CHANNEL_INVALID

Added in API level 5
int CHANNEL_INVALID

音频通道掩码无效

常量值:0(0x00000000)

CHANNEL_IN_BACK

Added in API level 5
int CHANNEL_IN_BACK

常量值:32(0x00000020)

CHANNEL_IN_BACK_PROCESSED

Added in API level 5
int CHANNEL_IN_BACK_PROCESSED

常量值:512(0x00000200)

CHANNEL_IN_DEFAULT

Added in API level 5
int CHANNEL_IN_DEFAULT

常数值:1(0x00000001)

CHANNEL_IN_FRONT

Added in API level 5
int CHANNEL_IN_FRONT

常量值:16(0x00000010)

CHANNEL_IN_FRONT_PROCESSED

Added in API level 5
int CHANNEL_IN_FRONT_PROCESSED

常量值:256(0x00000100)

CHANNEL_IN_LEFT

Added in API level 5
int CHANNEL_IN_LEFT

常量值:4(0x00000004)

CHANNEL_IN_LEFT_PROCESSED

Added in API level 5
int CHANNEL_IN_LEFT_PROCESSED

常量值:64(0x00000040)

CHANNEL_IN_MONO

Added in API level 5
int CHANNEL_IN_MONO

常量值:16(0x00000010)

CHANNEL_IN_PRESSURE

Added in API level 5
int CHANNEL_IN_PRESSURE

常量值:1024(0x00000400)

CHANNEL_IN_RIGHT

Added in API level 5
int CHANNEL_IN_RIGHT

常量值:8(0x00000008)

CHANNEL_IN_RIGHT_PROCESSED

Added in API level 5
int CHANNEL_IN_RIGHT_PROCESSED

常量值:128(0x00000080)

CHANNEL_IN_STEREO

Added in API level 5
int CHANNEL_IN_STEREO

常量值:12(0x0000000c)

CHANNEL_IN_VOICE_DNLINK

Added in API level 5
int CHANNEL_IN_VOICE_DNLINK

常量值:32768(0x00008000)

CHANNEL_IN_VOICE_UPLINK

Added in API level 5
int CHANNEL_IN_VOICE_UPLINK

常量值:16384(0x00004000)

CHANNEL_IN_X_AXIS

Added in API level 5
int CHANNEL_IN_X_AXIS

常量值:2048(0x00000800)

CHANNEL_IN_Y_AXIS

Added in API level 5
int CHANNEL_IN_Y_AXIS

常量值:4096(0x00001000)

CHANNEL_IN_Z_AXIS

Added in API level 5
int CHANNEL_IN_Z_AXIS

常量值:8192(0x00002000)

CHANNEL_OUT_5POINT1

Added in API level 5
int CHANNEL_OUT_5POINT1

常量值:252(0x000000fc)

CHANNEL_OUT_7POINT1

Added in API level 5
int CHANNEL_OUT_7POINT1

此常数在API级别23中已弃用。
不是典型的7.1环绕配置。 改为使用CHANNEL_OUT_7POINT1_SURROUND

常量值:1020(0x000003fc)

CHANNEL_OUT_7POINT1_SURROUND

Added in API level 23
int CHANNEL_OUT_7POINT1_SURROUND

常量值:6396(0x000018fc)

CHANNEL_OUT_BACK_CENTER

Added in API level 5
int CHANNEL_OUT_BACK_CENTER

常量值:1024(0x00000400)

CHANNEL_OUT_BACK_LEFT

Added in API level 5
int CHANNEL_OUT_BACK_LEFT

常量值:64(0x00000040)

CHANNEL_OUT_BACK_RIGHT

Added in API level 5
int CHANNEL_OUT_BACK_RIGHT

常量值:128(0x00000080)

CHANNEL_OUT_DEFAULT

Added in API level 5
int CHANNEL_OUT_DEFAULT

默认音频通道掩码

常数值:1(0x00000001)

CHANNEL_OUT_FRONT_CENTER

Added in API level 5
int CHANNEL_OUT_FRONT_CENTER

常量值:16(0x00000010)

CHANNEL_OUT_FRONT_LEFT

Added in API level 5
int CHANNEL_OUT_FRONT_LEFT

常量值:4(0x00000004)

CHANNEL_OUT_FRONT_LEFT_OF_CENTER

Added in API level 5
int CHANNEL_OUT_FRONT_LEFT_OF_CENTER

常量值:256(0x00000100)

CHANNEL_OUT_FRONT_RIGHT

Added in API level 5
int CHANNEL_OUT_FRONT_RIGHT

常量值:8(0x00000008)

CHANNEL_OUT_FRONT_RIGHT_OF_CENTER

Added in API level 5
int CHANNEL_OUT_FRONT_RIGHT_OF_CENTER

常量值:512(0x00000200)

CHANNEL_OUT_LOW_FREQUENCY

Added in API level 5
int CHANNEL_OUT_LOW_FREQUENCY

常量值:32(0x00000020)

CHANNEL_OUT_MONO

Added in API level 5
int CHANNEL_OUT_MONO

常量值:4(0x00000004)

CHANNEL_OUT_QUAD

Added in API level 5
int CHANNEL_OUT_QUAD

常量值:204(0x000000cc)

CHANNEL_OUT_SIDE_LEFT

Added in API level 21
int CHANNEL_OUT_SIDE_LEFT

常量值:2048(0x00000800)

CHANNEL_OUT_SIDE_RIGHT

Added in API level 21
int CHANNEL_OUT_SIDE_RIGHT

常量值:4096(0x00001000)

CHANNEL_OUT_STEREO

Added in API level 5
int CHANNEL_OUT_STEREO

常量值:12(0x0000000c)

CHANNEL_OUT_SURROUND

Added in API level 5
int CHANNEL_OUT_SURROUND

常量值:1052(0x0000041c)

ENCODING_AC3

Added in API level 21
int ENCODING_AC3

音频数据格式:AC-3压缩

常量值:5(0x00000005)

ENCODING_DEFAULT

Added in API level 3
int ENCODING_DEFAULT

默认音频数据格式

常数值:1(0x00000001)

ENCODING_DTS

Added in API level 23
int ENCODING_DTS

音频数据格式:DTS压缩

常量值:7(0x00000007)

ENCODING_DTS_HD

Added in API level 23
int ENCODING_DTS_HD

音频数据格式:DTS HD压缩

常量值:8(0x00000008)

ENCODING_E_AC3

Added in API level 21
int ENCODING_E_AC3

音频数据格式:E-AC-3压缩

常数值:6(0x00000006)

ENCODING_IEC61937

Added in API level 24
int ENCODING_IEC61937

音频数据格式:用于PCM或S / PDIF直通的压缩音频。 IEC61937使用16位样本的立体声流作为包装。 所以轨道的通道掩码必须是CHANNEL_OUT_STEREO 数据应该以短的[]数组写入流中。 如果数据写入byte []数组,那么在内部转换为short时,某些平台上可能存在endian问题。

常量值:13(0x0000000d)

ENCODING_INVALID

Added in API level 3
int ENCODING_INVALID

音频数据格式无效

常量值:0(0x00000000)

ENCODING_PCM_16BIT

Added in API level 3
int ENCODING_PCM_16BIT

音频数据格式:每个采样PCM 16位。 保证被设备支持。

常量值:2(0x00000002)

ENCODING_PCM_8BIT

Added in API level 3
int ENCODING_PCM_8BIT

音频数据格式:每个采样PCM 8位。 不保证被设备支持。

常量值:3(0x00000003)

ENCODING_PCM_FLOAT

Added in API level 21
int ENCODING_PCM_FLOAT

音频数据格式:每个采样的单精度浮点数

常量值:4(0x00000004)

SAMPLE_RATE_UNSPECIFIED

Added in API level 24
int SAMPLE_RATE_UNSPECIFIED

采样率将取决于路线。 对于AudioTrack,它通常是信源采样率,对于AudioRecord它通常是源采样率。

常量值:0(0x00000000)

Fields

CREATOR

Added in API level 24
Creator<AudioFormat> CREATOR

Public methods

describeContents

Added in API level 24
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.

equals

Added in API level 3
boolean equals (Object o)

指示其他某个对象是否“等于”这一个。

equals方法在非空对象引用上实现等价关系:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values 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.
  • For any non-null reference value x, x.equals(null) should return false.

Objectequals方法实现了对象上最可能的等价关系; 也就是说,对于任何非空参考值xy ,当且仅当xy引用同一对象( 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.

getChannelCount

Added in API level 23
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.

getChannelIndexMask

Added in API level 23
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.

getChannelMask

Added in API level 21
int getChannelMask ()

返回通道掩码。 有关基于索引的掩码(由getChannelIndexMask()返回)与此函数返回的基于位置的掩码之间的差异的详细信息,请参阅channel masks上的部分。

Returns
int one of the values that can be set in setChannelMask(int) or CHANNEL_INVALID if not set.

getEncoding

Added in API level 21
int getEncoding ()

返回编码。 有关支持的音频编码的不同类型的更多信息,请参阅encodings上的章节。

Returns
int one of the values that can be set in setEncoding(int) or ENCODING_INVALID if not set.

getSampleRate

Added in API level 21
int getSampleRate ()

返回采样率。

Returns
int one of the values that can be set in setSampleRate(int) or SAMPLE_RATE_UNSPECIFIED if not set.

hashCode

Added in API level 3
int hashCode ()

返回对象的哈希码值。 为了散列表的好处而支持此方法,例如HashMap提供的HashMap

hashCode的总合同是:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the 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.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the 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.

toString

Added in API level 3
String toString ()

返回对象的字符串表示形式。 一般来说, toString方法返回一个“文本表示”该对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。

ObjecttoString方法返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”以及对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
String a string representation of the object.

writeToParcel

Added in API level 24
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.

Hooray!