160 lines
5.0 KiB
Java
160 lines
5.0 KiB
Java
package com.ubt.jimu.blockly.feature.audio;
|
||
|
||
import android.media.MediaRecorder;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
import com.ubtech.utils.FileHelper;
|
||
import java.io.File;
|
||
|
||
/* loaded from: classes.dex */
|
||
public class AudioRecoder {
|
||
private static AudioRecoder audioRecoder = null;
|
||
public static boolean recoding = false;
|
||
private String fileName;
|
||
private MediaRecorder recorder;
|
||
private final String TAG = AudioRecoder.class.getSimpleName();
|
||
private int sampleRateInHz = 16000;
|
||
private long lastRecordTime = 0;
|
||
|
||
private class RecorderInfoListener implements MediaRecorder.OnInfoListener {
|
||
private RecorderInfoListener() {
|
||
}
|
||
|
||
@Override // android.media.MediaRecorder.OnInfoListener
|
||
public void onInfo(MediaRecorder mediaRecorder, int i, int i2) {
|
||
if (i == 1) {
|
||
Log.e(AudioRecoder.this.TAG, "OnInfo: MEDIA_RECORDER_INFO_UNKNOWN");
|
||
return;
|
||
}
|
||
if (i == 800) {
|
||
AudioRecoder.recoding = false;
|
||
Log.e(AudioRecoder.this.TAG, "OnInfo: MEDIA_RECORDER_INFO_MAX_DURATION_REACHED");
|
||
} else {
|
||
if (i != 801) {
|
||
return;
|
||
}
|
||
AudioRecoder.recoding = false;
|
||
Log.e(AudioRecoder.this.TAG, "OnInfo: MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED");
|
||
}
|
||
}
|
||
}
|
||
|
||
private class RecorderOnErrorListener implements MediaRecorder.OnErrorListener {
|
||
private RecorderOnErrorListener() {
|
||
}
|
||
|
||
@Override // android.media.MediaRecorder.OnErrorListener
|
||
public void onError(MediaRecorder mediaRecorder, int i, int i2) {
|
||
AudioRecoder.recoding = false;
|
||
Log.e(AudioRecoder.this.TAG, "录音失败:what=" + i + " extra=" + i2);
|
||
File file = new File(AudioRecoder.this.fileName);
|
||
if (file.exists()) {
|
||
FileHelper.a(file.getParentFile());
|
||
}
|
||
}
|
||
}
|
||
|
||
private AudioRecoder() {
|
||
}
|
||
|
||
private boolean aacLibRecord(String str) {
|
||
return true;
|
||
}
|
||
|
||
private boolean aacRecord(String str) throws Exception {
|
||
try {
|
||
release();
|
||
File parentFile = new File(str).getParentFile();
|
||
if (!parentFile.exists()) {
|
||
parentFile.mkdirs();
|
||
}
|
||
this.fileName = str;
|
||
this.recorder = new MediaRecorder();
|
||
this.recorder.setAudioSource(1);
|
||
this.recorder.setOutputFormat(6);
|
||
this.recorder.setAudioEncoder(3);
|
||
this.recorder.setAudioSamplingRate(8000);
|
||
this.recorder.setAudioChannels(1);
|
||
this.recorder.setOutputFile(str);
|
||
this.recorder.setOnErrorListener(new RecorderOnErrorListener());
|
||
this.recorder.setOnInfoListener(new RecorderInfoListener());
|
||
this.recorder.prepare();
|
||
this.recorder.start();
|
||
return true;
|
||
} catch (Exception e) {
|
||
onRecordFailed(str);
|
||
e.printStackTrace();
|
||
release();
|
||
Log.e(this.TAG, "prepare() failed");
|
||
throw e;
|
||
}
|
||
}
|
||
|
||
public static synchronized AudioRecoder getInstance() {
|
||
AudioRecoder audioRecoder2;
|
||
synchronized (AudioRecoder.class) {
|
||
if (audioRecoder == null) {
|
||
audioRecoder = new AudioRecoder();
|
||
}
|
||
audioRecoder2 = audioRecoder;
|
||
}
|
||
return audioRecoder2;
|
||
}
|
||
|
||
private void onRecordFailed(String str) {
|
||
File parentFile = new File(str).getParentFile();
|
||
if (parentFile.exists()) {
|
||
FileHelper.a(parentFile);
|
||
try {
|
||
FileHelper.a(parentFile);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
public String getFileName() {
|
||
return this.fileName;
|
||
}
|
||
|
||
public String getKey() {
|
||
if (TextUtils.isEmpty(this.fileName)) {
|
||
return null;
|
||
}
|
||
String str = this.fileName;
|
||
String substring = str.substring(str.lastIndexOf(File.separator) + 1, this.fileName.length());
|
||
return substring.substring(0, substring.lastIndexOf("."));
|
||
}
|
||
|
||
public void release() {
|
||
MediaRecorder mediaRecorder = this.recorder;
|
||
if (mediaRecorder != null) {
|
||
try {
|
||
try {
|
||
mediaRecorder.release();
|
||
} catch (IllegalStateException e) {
|
||
e.printStackTrace();
|
||
}
|
||
} finally {
|
||
this.recorder = null;
|
||
}
|
||
}
|
||
}
|
||
|
||
public boolean startRecord(String str) throws Exception {
|
||
recoding = true;
|
||
long currentTimeMillis = System.currentTimeMillis();
|
||
if (currentTimeMillis - this.lastRecordTime < 500) {
|
||
return false;
|
||
}
|
||
Log.i(this.TAG, "调用录音:" + str);
|
||
this.lastRecordTime = currentTimeMillis;
|
||
return aacRecord(str);
|
||
}
|
||
|
||
public void stopRecod() {
|
||
recoding = false;
|
||
release();
|
||
}
|
||
}
|