|
|
@ -12,7 +12,7 @@ import android.os.Looper |
|
|
import java.util.concurrent.TimeUnit |
|
|
import java.util.concurrent.TimeUnit |
|
|
import com.yunqiinnovation.azure_speech.tools.RecordFile |
|
|
import com.yunqiinnovation.azure_speech.tools.RecordFile |
|
|
import com.microsoft.cognitiveservices.speech.audio.AudioConfig |
|
|
import com.microsoft.cognitiveservices.speech.audio.AudioConfig |
|
|
|
|
|
import com.yunqiinnovation.azure_speech.tools.AudioRecordingForegroundService |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -31,6 +31,8 @@ class AzureAsrHelper(private val context: Context) { |
|
|
private var audioConfig: AudioConfig? = null |
|
|
private var audioConfig: AudioConfig? = null |
|
|
private var continuousCallback: ContinuousRecognizeCallback? = null |
|
|
private var continuousCallback: ContinuousRecognizeCallback? = null |
|
|
|
|
|
|
|
|
|
|
|
// 添加前台服务管理标志 |
|
|
|
|
|
private var isForegroundServiceRunning = false |
|
|
// 配置参数 |
|
|
// 配置参数 |
|
|
private var currentLanguage = "zh-CN" |
|
|
private var currentLanguage = "zh-CN" |
|
|
private var supportedLanguages = arrayOf("zh-CN") |
|
|
private var supportedLanguages = arrayOf("zh-CN") |
|
|
@ -247,6 +249,20 @@ class AzureAsrHelper(private val context: Context) { |
|
|
if (audioStream.isContinuousRecognitionActive) { |
|
|
if (audioStream.isContinuousRecognitionActive) { |
|
|
return true |
|
|
return true |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 启动前台服务(在开始音频录制前) |
|
|
|
|
|
if (!isForegroundServiceRunning && audioSourceType == AudioSourceType.MICROPHONE) { |
|
|
|
|
|
try { |
|
|
|
|
|
AudioRecordingForegroundService.startService(context) |
|
|
|
|
|
isForegroundServiceRunning = true |
|
|
|
|
|
Log.d(tag, "前台服务已启动") |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(tag, "启动前台服务失败: ${e.message}") |
|
|
|
|
|
continuousCallback?.onError(1003, "启动前台服务失败: ${e.message}") |
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (!isRecognizerValid()) { |
|
|
if (!isRecognizerValid()) { |
|
|
Log.d(tag, "重新启动连续识别") |
|
|
Log.d(tag, "重新启动连续识别") |
|
|
val callback = continuousCallback |
|
|
val callback = continuousCallback |
|
|
@ -393,6 +409,18 @@ class AzureAsrHelper(private val context: Context) { |
|
|
recognizer?.close() |
|
|
recognizer?.close() |
|
|
recognizer = null |
|
|
recognizer = null |
|
|
audioStream.isContinuousRecognitionActive = false |
|
|
audioStream.isContinuousRecognitionActive = false |
|
|
|
|
|
|
|
|
|
|
|
// 停止前台服务 |
|
|
|
|
|
if (isForegroundServiceRunning) { |
|
|
|
|
|
try { |
|
|
|
|
|
AudioRecordingForegroundService.stopService(context) |
|
|
|
|
|
isForegroundServiceRunning = false |
|
|
|
|
|
Log.d(tag, "前台服务已停止") |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(tag, "停止前台服务失败: ${e.message}") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return true |
|
|
return true |
|
|
} catch (e: Exception) { |
|
|
} catch (e: Exception) { |
|
|
// 强制重置状态 |
|
|
// 强制重置状态 |
|
|
@ -404,6 +432,17 @@ class AzureAsrHelper(private val context: Context) { |
|
|
recognizer?.close() |
|
|
recognizer?.close() |
|
|
recognizer = null |
|
|
recognizer = null |
|
|
audioStream.isContinuousRecognitionActive = false |
|
|
audioStream.isContinuousRecognitionActive = false |
|
|
|
|
|
|
|
|
|
|
|
// 确保前台服务被停止 |
|
|
|
|
|
if (isForegroundServiceRunning) { |
|
|
|
|
|
try { |
|
|
|
|
|
AudioRecordingForegroundService.stopService(context) |
|
|
|
|
|
isForegroundServiceRunning = false |
|
|
|
|
|
} catch (e2: Exception) { |
|
|
|
|
|
Log.e(tag, "强制停止前台服务失败: ${e2.message}") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -442,6 +481,18 @@ class AzureAsrHelper(private val context: Context) { |
|
|
// 释放音频配置 |
|
|
// 释放音频配置 |
|
|
audioConfig?.close() |
|
|
audioConfig?.close() |
|
|
audioConfig = null |
|
|
audioConfig = null |
|
|
|
|
|
|
|
|
|
|
|
// 确保前台服务被停止 |
|
|
|
|
|
if (isForegroundServiceRunning) { |
|
|
|
|
|
try { |
|
|
|
|
|
AudioRecordingForegroundService.stopService(context) |
|
|
|
|
|
isForegroundServiceRunning = false |
|
|
|
|
|
Log.d(tag, "dispose时前台服务已停止") |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(tag, "dispose时停止前台服务失败: ${e.message}") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 确保状态被重置 |
|
|
// 确保状态被重置 |
|
|
audioStream.isContinuousRecognitionActive = false |
|
|
audioStream.isContinuousRecognitionActive = false |
|
|
|
|
|
|
|
|
@ -451,6 +502,7 @@ class AzureAsrHelper(private val context: Context) { |
|
|
audioConfig = null |
|
|
audioConfig = null |
|
|
recognizer = null |
|
|
recognizer = null |
|
|
speechConfig = null |
|
|
speechConfig = null |
|
|
|
|
|
isForegroundServiceRunning = false |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -479,11 +531,23 @@ class AzureAsrHelper(private val context: Context) { |
|
|
*/ |
|
|
*/ |
|
|
fun enableRecord(audioSourceType: AudioSourceType = AudioSourceType.MICROPHONE,filePath: String, audioDataCallback: SimpleAudioReceiver.AudioDataCallback? = null) { |
|
|
fun enableRecord(audioSourceType: AudioSourceType = AudioSourceType.MICROPHONE,filePath: String, audioDataCallback: SimpleAudioReceiver.AudioDataCallback? = null) { |
|
|
Log.i(tag, "开启录音:") |
|
|
Log.i(tag, "开启录音:") |
|
|
this.audioSourceType = audioSourceType |
|
|
this.audioSourceType = audioSourceType |
|
|
if(audioSourceType == AudioSourceType.EXTERNAL){ |
|
|
if(audioSourceType == AudioSourceType.EXTERNAL){ |
|
|
Log.i(tag, "外部音频源不在这里录音") |
|
|
Log.i(tag, "外部音频源不在这里录音") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 启动前台服务(在开始录音前) |
|
|
|
|
|
if (!isForegroundServiceRunning) { |
|
|
|
|
|
try { |
|
|
|
|
|
AudioRecordingForegroundService.startService(context) |
|
|
|
|
|
isForegroundServiceRunning = true |
|
|
|
|
|
Log.d(tag, "录音前台服务已启动") |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(tag, "启动录音前台服务失败: ${e.message}") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// Initialize if not already done |
|
|
// Initialize if not already done |
|
|
if (!audioStream.isInitialized()) { |
|
|
if (!audioStream.isInitialized()) { |
|
|
audioStream.initAudioRecord() |
|
|
audioStream.initAudioRecord() |
|
|
@ -574,6 +638,17 @@ class AzureAsrHelper(private val context: Context) { |
|
|
|
|
|
|
|
|
if (!audioStream.isContinuousRecognitionActive) { |
|
|
if (!audioStream.isContinuousRecognitionActive) { |
|
|
audioStream.stopMicrophoneCapture() |
|
|
audioStream.stopMicrophoneCapture() |
|
|
|
|
|
|
|
|
|
|
|
// 停止录音时也停止前台服务(如果没有其他音频任务) |
|
|
|
|
|
if (isForegroundServiceRunning) { |
|
|
|
|
|
try { |
|
|
|
|
|
AudioRecordingForegroundService.stopService(context) |
|
|
|
|
|
isForegroundServiceRunning = false |
|
|
|
|
|
Log.d(tag, "录音前台服务已停止") |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(tag, "停止录音前台服务失败: ${e.message}") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
audioStream.isRecord = false |
|
|
audioStream.isRecord = false |
|
|
|