Skip to content

Commit 2dafb5a

Browse files
author
archurtan
committed
音频采集工具类暴露采样率设置接口
Change-Id: If88136cd04af5df53cd5b767bdd3575fa11eb0c1 (cherry picked from commit 89899f5)
1 parent c6f91cc commit 2dafb5a

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

sdk/video-link-android/src/main/java/com/tencent/iot/video/link/util/audio/AudioRecordUtil.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,24 @@
88

99

1010
public class AudioRecordUtil implements EncoderListener {
11-
12-
//设置音频采样率,44100是目前的标准,但是某些设备仍然支持22050,16000,11025
13-
private int sampleRateInHz = 16000;
14-
//设置音频的录制的声道CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道
15-
private int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
16-
//音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
17-
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
18-
//录制状态
19-
private volatile boolean recorderState = true;
11+
private int channelConfig = AudioFormat.CHANNEL_IN_STEREO; //设置音频的录制的声道CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道
12+
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT; //音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
13+
private volatile boolean recorderState = true; //录制状态
2014
private byte[] buffer;
2115
private AudioRecord audioRecord;
2216
private volatile PCMEncoder pcmEncoder;
2317
private volatile FLVPacker flvPacker;
2418
private Context context;
2519
private String deviceId; //"productId/deviceName"
2620
private int recordMinBufferSize;
27-
private int sampleRate;
21+
private int sampleRate; //音频采样率
2822
private int channel;
2923
private int bitDepth;
3024

31-
public AudioRecordUtil(Context ctx, String id) {
25+
public AudioRecordUtil(Context ctx, String id, int sampleRate) {
3226
context = ctx;
3327
deviceId = id;
34-
init(sampleRateInHz, channelConfig, audioFormat);
28+
init(sampleRate, channelConfig, audioFormat);
3529
}
3630
public AudioRecordUtil(Context ctx, int sampleRate, int channel, int bitDepth) {
3731
context = ctx;
@@ -58,7 +52,7 @@ public void start() {
5852
private void reset() {
5953
buffer = new byte[recordMinBufferSize];
6054
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channel, bitDepth, recordMinBufferSize);
61-
pcmEncoder = new PCMEncoder(sampleRateInHz, this, PCMEncoder.AAC_FORMAT);
55+
pcmEncoder = new PCMEncoder(sampleRate, this, PCMEncoder.AAC_FORMAT);
6256
flvPacker = new FLVPacker();
6357
}
6458

@@ -69,6 +63,8 @@ public void stop() {
6963
recorderState = false;
7064
audioRecord.stop();
7165
audioRecord = null;
66+
pcmEncoder = null;
67+
flvPacker = null;
7268
}
7369

7470
public void release() {

sdk/video-link-android/src/main/java/com/tencent/iot/video/link/util/audio/FLVPacker.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
public class FLVPacker {
88

99
private long pts = 0;
10-
private long sudioPts = 0;
1110
private volatile boolean isHead = false;
1211

1312
public synchronized byte[] getFLV(byte[] data) {
@@ -32,13 +31,13 @@ public synchronized byte[] aacToFlv(byte[] date) {
3231
baos.write(0x08);
3332
// 长度
3433
baos.write(integerTo3Bytes(data.length + 2));
35-
if (sudioPts == 0) {
34+
if (pts == 0) {
3635
// 时间戳
3736
baos.write(0x00);
3837
baos.write(0x00);
3938
baos.write(0x00);
4039
baos.write(0x00);
41-
sudioPts = System.currentTimeMillis();
40+
pts = System.currentTimeMillis();
4241
} else {
4342
byte[] b = integerTo4Bytes((int) (System.currentTimeMillis() - pts));
4443
baos.write(b[1]);

sdk/video-link-android/src/main/java/com/tencent/iot/video/link/util/audio/PCMEncoder.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import java.io.IOException;
88
import java.nio.ByteBuffer;
9+
import java.util.HashMap;
10+
import java.util.Map;
911

1012

1113
public class PCMEncoder {
@@ -23,19 +25,37 @@ public class PCMEncoder {
2325
private MediaCodec.BufferInfo encodeBufferInfo;
2426
private EncoderListener encoderListener;
2527
private int encodeType = 0;
28+
private int sampleRate = 0;
29+
30+
// 采样频率对照表
31+
private static Map<Integer, Integer> samplingFrequencyIndexMap = new HashMap<>();
32+
33+
static {
34+
samplingFrequencyIndexMap.put(96000, 0);
35+
samplingFrequencyIndexMap.put(88200, 1);
36+
samplingFrequencyIndexMap.put(64000, 2);
37+
samplingFrequencyIndexMap.put(48000, 3);
38+
samplingFrequencyIndexMap.put(44100, 4);
39+
samplingFrequencyIndexMap.put(32000, 5);
40+
samplingFrequencyIndexMap.put(24000, 6);
41+
samplingFrequencyIndexMap.put(22050, 7);
42+
samplingFrequencyIndexMap.put(16000, 8);
43+
samplingFrequencyIndexMap.put(12000, 9);
44+
samplingFrequencyIndexMap.put(11025, 10);
45+
samplingFrequencyIndexMap.put(8000, 11);
46+
}
2647

2748
public PCMEncoder(int sampleRate, EncoderListener encoderListener, int encodeFormat) {
2849
this.encoderListener = encoderListener;
2950
this.encodeType = encodeFormat;
30-
init(sampleRate);
51+
this.sampleRate = sampleRate;
52+
init();
3153
}
3254

3355
/**
3456
* 初始化AAC编码器
35-
*
36-
* @param sampleRate 采样率
3757
*/
38-
private void init(int sampleRate) {
58+
private void init() {
3959
try {
4060
//参数对应-> mime type、采样率、声道数
4161
MediaFormat encodeFormat = MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_AAC,
@@ -110,8 +130,8 @@ public void encodeData(byte[] data) {
110130
*/
111131
private void addADTStoPacket(byte[] packet, int packetLen) {
112132
int profile = 2; // AAC LC
113-
int freqIdx = 8; // 44.1KHz
114133
int chanCfg = 2; // CPE
134+
int freqIdx = samplingFrequencyIndexMap.get(sampleRate);
115135
// filled in ADTS data
116136
packet[0] = (byte) 0xFF;
117137
packet[1] = (byte) 0xF9;

sdkdemo/src/main/java/com/tencent/iot/explorer/link/demo/video/preview/VideoPreviewActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class VideoPreviewActivity : VideoBaseActivity(), EventView, TextureView.Surface
120120
tv_event_status.setText(R.string.loading)
121121
XP2P.setQcloudApiCred(it.accessId, it.accessToken)
122122
XP2P.setCallback(this)
123-
audioRecordUtil = AudioRecordUtil(this, "${it.productId}/${presenter.getDeviceName()}")
123+
audioRecordUtil = AudioRecordUtil(this, "${it.productId}/${presenter.getDeviceName()}", 16000)
124124
}
125125

126126
startPlayer()

0 commit comments

Comments
 (0)