17. Recording Function
Recording can be initiated by the device side or the App side. Recording can be ended by the device side or the App side.
Sample Code
/**
* @ClassName: RecordDemo
* @Description: Recording function example
* @Author: ZhangHuanMing
* @CreateDate: 2023/12/28 15:52
*/
public class RecordDemo {
private RecordOpImpl mRecordOp;
private OnRecordStateCallback mOnRecordStateCallback = (bluetoothDevice, recordState) -> {
switch (recordState.getState()) {
case RecordState.RECORD_STATE_START: /* Recording state -- Start state*/
//Recording parameters, audio type sampling rate sentence breaking method
RecordParam recordParam = recordState.getRecordParam();
recordParam.getSampleRate();//Sampling rate
recordParam.getVoiceType();//Audio encoding format
recordParam.getVadWay();//Sentence breaking method
break;
case RecordState.RECORD_STATE_WORKING: //Recording data transmission
recordState.getVoiceDataBlock();//Recording packet data
break;
case RecordState.RECORD_STATE_IDLE: /* Recording state -- Idle (end) state*/
if (recordState.getReason() >= 0) {//End reason, greater than 0 is normal
//Non-AI cloud service function, no need to handle the following states
recordState.isPlayTTS();//Whether to play TTS
recordState.isSyncIatText();//Whether to transmit speech recognition text
recordState.isSyncNlpText();//Whether to transmit semantic recognition text
}
recordState.getVoiceData();//Total recording packet data
break;
}
};
/**
* Initialization
*/
@Test
public void init(){
mRecordOp = new RecordOpImpl(WatchManager.getInstance());
mRecordOp.addOnRecordStateCallback(mOnRecordStateCallback);
}
/**
* Release
*/
@Test
public void release(){
if (mRecordOp != null) {
mRecordOp.removeOnRecordStateCallback(mOnRecordStateCallback);
mRecordOp.release();
mRecordOp = null;
}
}
/**
* Get recording state
*/
@Test
public RecordState getRecordState(){
return mRecordOp.getRecordState();
}
/**
* App side starts recording
*/
@Test
public void startRecord(BluetoothDevice device){
int voiceType = RecordParam.VOICE_TYPE_OPUS;//Recording format-opus
int sampleRate = RecordParam.SAMPLE_RATE_8K;//Sampling rate-8k
int vadWay = RecordParam.VAD_WAY_DEVICE;//Sentence breaking method-device
RecordParam param = new RecordParam(voiceType,sampleRate,vadWay);
OnOperationCallback<Boolean> callback = new OnOperationCallback<Boolean>() {
@Override
public void onSuccess(Boolean result) {
if (result){
//Start successful
}
}
@Override
public void onFailed(BaseError error) {
}
};
mRecordOp.startRecord(device,param,callback);
}
/**
* App side ends recording
*/
@Test
public void stopRecord(BluetoothDevice device){
int reason = REASON_NORMAL;//0: Normal end, 1 Cancel
boolean isSyncIatText= true;//Whether to sync speech recognition text (will sync update settings)
boolean isSyncNlpText= true;//Whether to sync AI semantic recognition text (will sync update settings)
boolean isPlayTTS= true;//Whether to play AI semantic recognition TTS (will sync update settings)
OnOperationCallback<Boolean> callback = new OnOperationCallback<Boolean>() {
@Override
public void onSuccess(Boolean result) {
if (result){
//Stop successful
}
}
@Override
public void onFailed(BaseError error) {
}
};
mRecordOp.stopRecord(device,reason,isSyncIatText,isSyncNlpText,isPlayTTS,callback);
}
}