From dae7f2fdfad70424942de10f37b470be6421d580 Mon Sep 17 00:00:00 2001 From: wuqifeng <540416539@qq.com> Date: Sun, 9 Jul 2023 11:43:59 +0800 Subject: [PATCH] feat:增加android端先声评测(自己录音,先声只评测) --- android/app/src/main/kotlin/com/kouyuxingqiu/wow_english/methodChannels/SingSoungMethodChannel.kt | 16 +++++++++++++--- android/app/src/main/kotlin/com/kouyuxingqiu/wow_english/singsound/SingEngineHelper.kt | 32 ++++++++++++++++++++++++-------- ios/Runner/XSMessageMehtodChannel.swift | 2 +- lib/pages/reading/bloc/reading_bloc.dart | 3 ++- lib/pages/reading/bloc/reading_event.dart | 2 +- lib/pages/reading/reading_page.dart | 2 +- lib/pages/repeataftercontent/bloc/repeat_after_content_bloc.dart | 2 +- 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/android/app/src/main/kotlin/com/kouyuxingqiu/wow_english/methodChannels/SingSoungMethodChannel.kt b/android/app/src/main/kotlin/com/kouyuxingqiu/wow_english/methodChannels/SingSoungMethodChannel.kt index 611e93b..76d1f11 100644 --- a/android/app/src/main/kotlin/com/kouyuxingqiu/wow_english/methodChannels/SingSoungMethodChannel.kt +++ b/android/app/src/main/kotlin/com/kouyuxingqiu/wow_english/methodChannels/SingSoungMethodChannel.kt @@ -18,6 +18,7 @@ import java.lang.ref.WeakReference */ class SingSoungMethodChannel(activity: FlutterActivity, flutterEngine: FlutterEngine): SingEngineLifecycles.OnSingEngineAdapter() { private var methodChannel: MethodChannel? = null + private val TAG = "SingSoungMethodChannel" companion object { var channel: WeakReference? = null @@ -36,20 +37,29 @@ class SingSoungMethodChannel(activity: FlutterActivity, flutterEngine: FlutterEn ) init(activity) methodChannel?.setMethodCallHandler { call, result -> + Log.d(TAG, "SingSoungMethodChannel CALL=${call.method} ${call.arguments}") when (call.method) { "initVoiceSdk" -> { } "startVoice" -> { val paramMap = call.arguments as HashMap - Log.d("WQF", "SingSoungMethodChannel startVoice=${call.arguments.javaClass} paramMap=$paramMap") + Log.d(TAG, "SingSoungMethodChannel startVoice=${call.arguments.javaClass} paramMap=$paramMap") paramMap["word"]?.let { SingEngineHelper.startRecord(it) } - //do nothing } "stopVoice" -> { - Log.d("WQF", "SingSoungMethodChannel stopVoice") + Log.d(TAG, "SingSoungMethodChannel stopVoice") SingEngineHelper.stopRecord() } + "startLocalVoice" -> { + val paramMap = call.arguments as HashMap + Log.d(TAG, "SingSoungMethodChannel startLocalVoice=${call.arguments.javaClass} paramMap=$paramMap") + paramMap["voicePath"]?.let { voiceFilePath -> + paramMap["word"]?.let { evaluateContent -> + SingEngineHelper.evaluate(voiceFilePath, evaluateContent) } + } + + } else -> { result.notImplemented() } diff --git a/android/app/src/main/kotlin/com/kouyuxingqiu/wow_english/singsound/SingEngineHelper.kt b/android/app/src/main/kotlin/com/kouyuxingqiu/wow_english/singsound/SingEngineHelper.kt index c657bee..5ebf180 100644 --- a/android/app/src/main/kotlin/com/kouyuxingqiu/wow_english/singsound/SingEngineHelper.kt +++ b/android/app/src/main/kotlin/com/kouyuxingqiu/wow_english/singsound/SingEngineHelper.kt @@ -106,8 +106,29 @@ object SingEngineHelper : } } - // 开始语音评测 - fun startRecord(originText: String, @EvalTargetType evalTargetType: Int? = EvalTargetType.SENTENCE) { + // 开始语音评测(先声录音+评测) + fun startRecord(originText: String, @EvalTargetType evalTargetType: Int? = EvalTargetType.SENTENCE, userId: String? = VoiceConfig.UserID) { + buildEvaluateConfig(originText, evalTargetType, userId) + //开始测评 + mSingEngine?.start() + mCurEvalType = evalTargetType + Log.w(TAG, "startRecord originText=$originText evalTargetType =$evalTargetType") + } + + /** + * 评测外部录音文件 + * @param voiceFilePath 录音文件路径 + */ + fun evaluate(voiceFilePath: String, originText: String, @EvalTargetType evalTargetType: Int? = EvalTargetType.SENTENCE, userId: String? = VoiceConfig.UserID) { + buildEvaluateConfig(originText, evalTargetType, userId) + if (mIsReady) { + mSingEngine?.startWithPCM(voiceFilePath) + Log.w(TAG, "startWithPCM") + } + mCurEvalType = evalTargetType + } + + private fun buildEvaluateConfig(originText: String, @EvalTargetType evalTargetType: Int? = EvalTargetType.SENTENCE, userId: String? = VoiceConfig.UserID) { try { val request = JSONObject() when (evalTargetType) { @@ -130,7 +151,6 @@ object SingEngineHelper : .put("typeThres", SingSoundConfig.BASE_TYPETHRES) .put("precision", 1) // 评分精度,默认1 .put("attachAudioUrl", 1) // 评分结果中是否包含音频 url - .put("userId", "test") .put("phones", jsonObj) // 指定单词的发音。 .put("rank", 100) } else { @@ -161,16 +181,12 @@ object SingEngineHelper : } //构建评测请求参数 - val startCfg = mSingEngine?.buildStartJson(VoiceConfig.UserID, request) + val startCfg = mSingEngine?.buildStartJson(userId, request) //设置评测请求参数 mSingEngine?.setStartCfg(startCfg) - //开始测评 - mSingEngine?.start() - mCurEvalType = evalTargetType } catch (e: Exception) { e.printStackTrace() } - Log.w(TAG, "startRecord originText=$originText evalTargetType =$evalTargetType") } fun stopRecord() { // 停止录音(有回调) diff --git a/ios/Runner/XSMessageMehtodChannel.swift b/ios/Runner/XSMessageMehtodChannel.swift index 4243db0..d5c2e22 100644 --- a/ios/Runner/XSMessageMehtodChannel.swift +++ b/ios/Runner/XSMessageMehtodChannel.swift @@ -93,7 +93,7 @@ class XSMessageMehtodChannel: NSObject,SSOralEvaluatingManagerDelegate { return } - if (call.method == "starLocalVoice") { + if (call.method == "startLocalVoice") { self.evaluateLocalVoice(dict: call.arguments as! Dictionary) return } diff --git a/lib/pages/reading/bloc/reading_bloc.dart b/lib/pages/reading/bloc/reading_bloc.dart index afcd10b..28e67df 100644 --- a/lib/pages/reading/bloc/reading_bloc.dart +++ b/lib/pages/reading/bloc/reading_bloc.dart @@ -7,6 +7,7 @@ import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:wow_english/pages/reading/widgets/ReadingModeType.dart'; +import '../../../common/core/user_util.dart'; import '../../../common/request/dao/listen_dao.dart'; import '../../../common/request/exception.dart'; import '../../../models/course_process_entity.dart'; @@ -296,7 +297,7 @@ class ReadingPageBloc extends Bloc { ); if (result) { methodChannel.invokeMethod( - 'startVoice', {'word': content, 'type': '0', 'userId': '1'}); + 'startVoice', {'word': content, 'type': '0', 'userId': UserUtil.getUser()?.id.toString()}); } } diff --git a/lib/pages/reading/bloc/reading_event.dart b/lib/pages/reading/bloc/reading_event.dart index 1dd662c..ccebd9a 100644 --- a/lib/pages/reading/bloc/reading_event.dart +++ b/lib/pages/reading/bloc/reading_event.dart @@ -38,7 +38,7 @@ class XSVoiceResultEvent extends ReadingPageEvent { class XSVoiceStartEvent extends ReadingPageEvent { final String content; final String type; - final String userId; + final String? userId; XSVoiceStartEvent(this.content,this.type,this.userId); } diff --git a/lib/pages/reading/reading_page.dart b/lib/pages/reading/reading_page.dart index 62e509b..dd84ca1 100644 --- a/lib/pages/reading/reading_page.dart +++ b/lib/pages/reading/reading_page.dart @@ -179,7 +179,7 @@ class _ReadingPage extends StatelessWidget { bloc.add(XSVoiceStartEvent( bloc.currentPageData()?.word ?? '', '0', - UserUtil.getUser()!.id.toString())); + UserUtil.getUser()?.id.toString())); } }, child: Image.asset( diff --git a/lib/pages/repeataftercontent/bloc/repeat_after_content_bloc.dart b/lib/pages/repeataftercontent/bloc/repeat_after_content_bloc.dart index 745c844..7989342 100644 --- a/lib/pages/repeataftercontent/bloc/repeat_after_content_bloc.dart +++ b/lib/pages/repeataftercontent/bloc/repeat_after_content_bloc.dart @@ -197,7 +197,7 @@ class RepeatAfterContentBloc extends Bloc emitter) async { await methodChannel.invokeMethod( - 'starLocalVoice', + 'startLocalVoice', { 'type':event.type, 'word':event.testWord, -- libgit2 0.22.2