-
大小: 7KB文件类型: .rar金币: 1下载: 0 次发布日期: 2021-05-17
- 语言: 其他
- 标签: kedaxunfei yuyinpingce
资源简介
科大讯飞语音评测接入相关类,SpeakingExamFragment为使用类,其余为工具类
代码片段和文件信息
package com.shushan.util;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Environment;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @ClassName: AudioRecordUtil
* @Desciption: //录制wav格式音频
* @author: zhangshihao
* @date: 2018-07-21
*/
public class AudioRecordUtil {
private static AudioRecordUtil mInstance;
private AudioRecord recorder;
//录音源
private static int audioSource = MediaRecorder.AudioSource.MIC;
//录音的采样频率
private static int audioRate = 16000;
//录音的声道,单声道
private static int audioChannel = AudioFormat.CHANNEL_IN_MONO;
//量化的深度
private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
//缓存的大小
private static int bufferSize = AudioRecord.getMinBufferSize(audioRateaudioChannelaudioFormat);
//记录播放状态
private boolean isRecording = false;
//数字信号数组
private byte [] noteArray;
//PCM文件
private File pcmFile;
//WAV文件
private File wavFile;
//文件输出流
private OutputStream os;
//文件根目录
private String basePath = Environment.getExternalStorageDirectory().getAbsolutePath()+“/eva/“;
//wav文件目录
private String outFileName = basePath+“/eva.wav“;
//pcm文件目录
private String inFileName = basePath+“/eva.pcm“;
private AudioRecordUtil(){
createFile();//创建文件
recorder = new AudioRecord(audioSourceaudioRateaudioChannelaudioFormatbufferSize);
}
public synchronized static AudioRecordUtil getInstance(){
if(mInstance == null){
mInstance = new AudioRecordUtil();
}
return mInstance;
}
//读取录音数字数据线程
class WriteThread implements Runnable{
public void run(){
writeData();
}
}
//开始录音
public void startRecord(){
isRecording = true;
recorder.startRecording();
}
//停止录音
public void stopRecord(){
isRecording = false;
recorder.stop();
}
//将数据写入文件夹文件的写入没有做优化
public void writeData(){
noteArray = new byte[bufferSize];
//建立文件输出流
try {
os = new BufferedOutputStream(new FileOutputStream(pcmFile));
}catch (IOException e){
}
while(isRecording == true){
int recordSize = recorder.read(noteArray0bufferSize);
if(recordSize>0){
try{
os.write(noteArray);
}catch(IOException e){
}
}
}
if (os != null) {
try {
os.close();
}catch (IOException e){
}
}
}
// 这里得到可播放的
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 11029 2018-08-21 09:32 SpeakingExamFragment.java
文件 3436 2018-08-14 09:47 SpeechEvaluatorUtil.java
文件 8609 2018-08-21 10:03 AudioRecordUtil.java
----------- --------- ---------- ----- ----
23074 3
评论
共有 条评论