Skip to content

Commit

Permalink
音频自定义采集
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumn committed Oct 21, 2020
1 parent 7654a21 commit 3f8a149
Show file tree
Hide file tree
Showing 6 changed files with 507 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.project.tencentsdkcustomdemo.media.audio;

import android.media.AudioFormat;

import java.io.Serializable;
import java.util.Locale;


public class RecordConfig implements Serializable {
/**
* 录音格式
*/
private RecordFormat format = RecordFormat.PCM;
/**
* 通道数:默认单通道
*/
private int channelConfig = AudioFormat.CHANNEL_IN_MONO;

/**
* 位宽
*/
private int encodingConfig = AudioFormat.ENCODING_PCM_16BIT;

/**
* 采样率
*/
private int sampleRate = 16000;


public RecordConfig() {
}

public RecordConfig(RecordFormat format) {
this.format = format;
}

/**
* @param format 录音文件的格式
* @param channelConfig 声道配置
* 单声道:See {@link AudioFormat#CHANNEL_IN_MONO}
* 双声道:See {@link AudioFormat#CHANNEL_IN_STEREO}
* @param encodingConfig 位宽配置
* 8Bit: See {@link AudioFormat#ENCODING_PCM_8BIT}
* 16Bit: See {@link AudioFormat#ENCODING_PCM_16BIT},
* @param sampleRate 采样率 hz: 8000/16000/44100
*/
public RecordConfig(RecordFormat format, int channelConfig, int encodingConfig, int sampleRate) {
this.format = format;
this.channelConfig = channelConfig;
this.encodingConfig = encodingConfig;
this.sampleRate = sampleRate;
}



/**
* 获取当前录音的采样位宽 单位bit
*
* @return 采样位宽 0: error
*/
public int getEncoding() {

if (encodingConfig == AudioFormat.ENCODING_PCM_8BIT) {
return 8;
} else if (encodingConfig == AudioFormat.ENCODING_PCM_16BIT) {
return 16;
} else {
return 0;
}
}

/**
* 获取当前录音的采样位宽 单位bit
*
* @return 采样位宽 0: error
*/
public int getRealEncoding() {
if (encodingConfig == AudioFormat.ENCODING_PCM_8BIT) {
return 8;
} else if (encodingConfig == AudioFormat.ENCODING_PCM_16BIT) {
return 16;
} else {
return 0;
}
}

/**
* 当前的声道数
*
* @return 声道数: 0:error
*/
public int getChannelCount() {
if (channelConfig == AudioFormat.CHANNEL_IN_MONO) {
return 1;
} else if (channelConfig == AudioFormat.CHANNEL_IN_STEREO) {
return 2;
} else {
return 0;
}
}

//get&set

public RecordFormat getFormat() {
return format;
}

public RecordConfig setFormat(RecordFormat format) {
this.format = format;
return this;
}

public int getChannelConfig() {
return channelConfig;
}

public RecordConfig setChannelConfig(int channelConfig) {
this.channelConfig = channelConfig;
return this;
}

public int getEncodingConfig() {

return encodingConfig;
}

public RecordConfig setEncodingConfig(int encodingConfig) {
this.encodingConfig = encodingConfig;
return this;
}

public int getSampleRate() {
return sampleRate;
}

public RecordConfig setSampleRate(int sampleRate) {
this.sampleRate = sampleRate;
return this;
}


@Override
public String toString() {
return String.format(Locale.getDefault(), "录制格式: %s,采样率:%sHz,位宽:%s bit,声道数:%s", format, sampleRate, getEncoding(), getChannelCount());
}

public enum RecordFormat {

/**
* pcm格式
*/
PCM(".pcm");

private String extension;

public String getExtension() {
return extension;
}

RecordFormat(String extension) {
this.extension = extension;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.project.tencentsdkcustomdemo.media.audio;

/**
* @author zhaolewei on 2018/7/11.
*/
public interface RecordDataListener {

/**
* 当前的录音状态发生变化
*
* @param data 当前音频数据
*/
void onData(byte[] data);

}
Loading

0 comments on commit 3f8a149

Please sign in to comment.