interview-question.js 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const common_config = require("../../common/config.js");
  4. const _sfc_main = {
  5. name: "IdentityVerify",
  6. data() {
  7. return {
  8. loading: false,
  9. responses: [],
  10. processedResponses: [],
  11. assistantResponse: "",
  12. audioTranscript: "",
  13. videoPlaying: false,
  14. showDebugInfo: false,
  15. // 设置为true可以显示调试信息
  16. videoUrl: "https://data.qicai321.com/minlong/1809457d-30c5-40bb-b6d8-a8c2e8e1e9e2.mp4",
  17. //'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/abdaa6fda8494e3a8613304743ed0433.mp4', // 用于存储AI数字人视频URL
  18. showReplayButton: false,
  19. cameraStream: null,
  20. // 存储摄像头流
  21. cameraError: null,
  22. // 存储摄像头错误信息
  23. useMiniProgramCameraComponent: false,
  24. // 添加小程序相机组件标志
  25. cameraContext: null,
  26. // 添加相机上下文
  27. currentSubtitle: "",
  28. subtitles: [
  29. {
  30. startTime: 0,
  31. // 开始时间(秒)
  32. endTime: 6,
  33. // 结束时间(秒)
  34. text: "您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?"
  35. }
  36. ],
  37. // secondVideoSubtitles: [
  38. // {
  39. // startTime: 0,
  40. // endTime: 10,
  41. // text: '请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:'
  42. // }
  43. // ],
  44. // thirdVideoSubtitles: [
  45. // {
  46. // startTime: 0,
  47. // endTime: 4,
  48. // text: '在工作中,你如何确保个人防护装备的正确使用?'
  49. // }
  50. // ],
  51. // fourthVideoSubtitles: [
  52. // {
  53. // startTime: 0,
  54. // endTime: 4,
  55. // text: '描述一次你与团队合作改善生产流程的经历。'
  56. // }
  57. // ],
  58. // fifthVideoSubtitles: [
  59. // {
  60. // startTime: 0,
  61. // endTime: 6,
  62. // text: '你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?'
  63. // }
  64. // ],
  65. // sixthVideoSubtitles: [
  66. // {
  67. // startTime: 0,
  68. // endTime: 5,
  69. // text: '您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?'
  70. // }
  71. // ],
  72. showAnswerButton: false,
  73. // 控制答题按钮显示
  74. currentVideoIndex: 0,
  75. // 当前播放的视频索引
  76. videoList: [
  77. "https://data.qicai321.com/minlong/1809457d-30c5-40bb-b6d8-a8c2e8e1e9e2.mp4"
  78. //结束
  79. ],
  80. isRecording: false,
  81. recordingTimer: null,
  82. showStopRecordingButton: false,
  83. mediaRecorder: null,
  84. recordedChunks: [],
  85. recorder: null,
  86. lastUploadedVideoUrl: "",
  87. showStartRecordingButton: false,
  88. showRetryButton: false,
  89. // 控制重试按钮显示
  90. lastVideoToRetry: null,
  91. // 存储上次失败的视频URL,用于重试
  92. recordingStartTime: null,
  93. // 录制开始时间
  94. recordingTimerCount: 0,
  95. // 录制计时器计数
  96. recordingTimeDisplay: "00:00",
  97. // 格式化的录制时间显示
  98. // 添加上传队列相关数据
  99. uploadQueue: [],
  100. // 存储待上传的视频
  101. isUploading: false,
  102. // 标记是否正在上传
  103. uploadProgress: {},
  104. // 存储每个视频的上传进度
  105. uploadStatus: {},
  106. // 存储每个视频的上传状态
  107. showUploadStatus: false,
  108. // 是否显示上传状态指示器
  109. uploadStatusText: "",
  110. // 上传状态文本
  111. mediaRecorderTimeout: null,
  112. // 用于存储MediaRecorder的超时机制
  113. maxRecordingTime: 300,
  114. // 最大录制时间(秒)- 从60秒改为300秒(5分钟)
  115. remainingTime: 300,
  116. // 剩余录制时间(秒)- 从60秒改为300秒
  117. // 修改初始视频索引,添加参数控制
  118. startFromLastVideo: false,
  119. // 是否从最后一个视频开始播放
  120. countdownValue: 10,
  121. // 倒计时数值
  122. showCountdown: false,
  123. // 是否显示倒计时蒙层
  124. countdownTimer: null,
  125. showGif: false,
  126. // 控制是否显示GIF
  127. gifUrl: "",
  128. // GIF图片的URL
  129. // 添加AI语音交互相关属性
  130. aiVoiceUrl: "",
  131. // AI语音URL
  132. aiText: "",
  133. // AI回复文本
  134. transcribedText: "",
  135. // 转写文本
  136. isPlayingAiVoice: false,
  137. // 是否正在播放AI语音
  138. aiAudioPlayer: null,
  139. // AI语音播放器
  140. // 添加新的状态变量
  141. showContinueQuestionButton: false,
  142. // 控制"继续提问"按钮显示
  143. showEndInterviewButton: false,
  144. // 控制"结束面试"按钮显示
  145. questionRound: 0,
  146. // 当前提问轮次
  147. maxQuestionRounds: 1,
  148. // 最大提问轮次(可配置)
  149. conversationHistory: [],
  150. // 对话历史记录
  151. pendingAutoNavigation: false,
  152. // 标记是否有待处理的自动跳转
  153. hasTriedFallbackMethod: false,
  154. // 标记是否尝试过备选方法播放音频
  155. hasShownTextFallbackToast: false
  156. // 标记是否已显示文本回退提示
  157. };
  158. },
  159. onLoad(options) {
  160. if (options && options.startFromLast === "true") {
  161. this.startFromLastVideo = true;
  162. this.currentVideoIndex = this.videoList.length - 1;
  163. }
  164. },
  165. mounted() {
  166. if (this.startFromLastVideo) {
  167. console.log("从最后一个视频开始播放");
  168. this.currentVideoIndex = this.videoList.length - 1;
  169. this.videoUrl = this.videoList[this.currentVideoIndex];
  170. }
  171. this.playDigitalHumanVideo();
  172. this.checkAudioPermission();
  173. this.initCamera();
  174. this.checkIOSCameraRecordPermission();
  175. this.checkAndFixRenderingIssues();
  176. setTimeout(() => {
  177. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  178. this.testAudioInput();
  179. }
  180. }, 3e3);
  181. common_vendor.index.setKeepScreenOn({
  182. keepScreenOn: true
  183. });
  184. },
  185. beforeDestroy() {
  186. this.stopUserCamera();
  187. if (this.aiAudioPlayer) {
  188. try {
  189. this.aiAudioPlayer.destroy();
  190. } catch (e) {
  191. console.error("销毁音频播放器失败:", e);
  192. }
  193. this.aiAudioPlayer = null;
  194. }
  195. if (this.recordingTimer) {
  196. clearInterval(this.recordingTimer);
  197. this.recordingTimer = null;
  198. }
  199. if (this.countdownTimer) {
  200. clearInterval(this.countdownTimer);
  201. this.countdownTimer = null;
  202. }
  203. if (this.mediaRecorderTimeout) {
  204. clearTimeout(this.mediaRecorderTimeout);
  205. this.mediaRecorderTimeout = null;
  206. }
  207. if (this.mediaRecorder && this.mediaRecorder.state === "recording") {
  208. try {
  209. this.mediaRecorder.stop();
  210. } catch (e) {
  211. console.error("停止MediaRecorder失败:", e);
  212. }
  213. }
  214. if (this.recorder) {
  215. try {
  216. this.cameraContext.stopRecord();
  217. } catch (e) {
  218. console.error("停止小程序录制失败:", e);
  219. }
  220. }
  221. common_vendor.index.setKeepScreenOn({
  222. keepScreenOn: false
  223. });
  224. },
  225. methods: {
  226. // 初始化相机
  227. async initCamera() {
  228. const systemInfo = common_vendor.index.getSystemInfoSync();
  229. const isMiniProgram = systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay" || systemInfo.uniPlatform === "mp-baidu";
  230. if (isMiniProgram) {
  231. this.useMiniProgramCameraComponent = true;
  232. this.cameraContext = common_vendor.index.createCameraContext();
  233. common_vendor.index.getSetting({
  234. success: (res) => {
  235. if (!res.authSetting["scope.record"]) {
  236. common_vendor.index.authorize({
  237. scope: "scope.record",
  238. success: () => {
  239. console.log("录音权限已获取");
  240. },
  241. fail: (err) => {
  242. console.error("录音权限获取失败:", err);
  243. this.showPermissionDialog("录音");
  244. }
  245. });
  246. }
  247. if (!res.authSetting["scope.camera"]) {
  248. common_vendor.index.authorize({
  249. scope: "scope.camera",
  250. success: () => {
  251. console.log("相机权限已获取");
  252. },
  253. fail: (err) => {
  254. console.error("相机权限获取失败:", err);
  255. this.showPermissionDialog("相机");
  256. }
  257. });
  258. }
  259. const systemInfo2 = common_vendor.index.getSystemInfoSync();
  260. if (systemInfo2.platform === "ios") {
  261. if (!res.authSetting["scope.camera"] || !res.authSetting["scope.record"]) {
  262. console.log("iOS需要同时获取相机和录音权限");
  263. }
  264. }
  265. }
  266. });
  267. } else {
  268. try {
  269. const constraints = {
  270. audio: {
  271. echoCancellation: true,
  272. noiseSuppression: true,
  273. autoGainControl: true
  274. },
  275. video: {
  276. width: { ideal: 640, max: 1280 },
  277. // 控制视频宽度
  278. height: { ideal: 480, max: 720 },
  279. // 控制视频高度
  280. frameRate: { ideal: 15, max: 24 },
  281. // 控制帧率
  282. facingMode: "user"
  283. }
  284. };
  285. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  286. this.cameraStream = stream;
  287. const audioTracks = stream.getAudioTracks();
  288. console.log("音频轨道数量:", audioTracks.length);
  289. if (audioTracks.length > 0) {
  290. console.log("音频轨道已获取:", audioTracks[0].label);
  291. audioTracks[0].enabled = true;
  292. } else {
  293. console.warn("未检测到音频轨道,尝试单独获取音频");
  294. this.tryGetAudioOnly();
  295. }
  296. const videoElement = this.$refs.userCameraVideo;
  297. if (videoElement) {
  298. videoElement.srcObject = stream;
  299. videoElement.muted = true;
  300. }
  301. } catch (error) {
  302. console.error("获取摄像头失败:", error);
  303. this.cameraError = error.message || "无法访问摄像头";
  304. common_vendor.index.showToast({
  305. title: "无法访问摄像头,请检查权限设置",
  306. icon: "none"
  307. });
  308. }
  309. }
  310. },
  311. // 停止用户摄像头
  312. stopUserCamera() {
  313. if (this.cameraStream) {
  314. this.cameraStream.getTracks().forEach((track) => {
  315. track.stop();
  316. });
  317. this.cameraStream = null;
  318. }
  319. },
  320. async fetchData() {
  321. this.loading = true;
  322. this.assistantResponse = "";
  323. this.audioTranscript = "";
  324. this.processedResponses = [];
  325. try {
  326. const requestTask = common_vendor.index.request({
  327. url: "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
  328. method: "POST",
  329. header: {
  330. "Content-Type": "application/json",
  331. "Authorization": "Bearer sk-9e1ec73a7d97493b8613c63f06b6110c"
  332. },
  333. data: {
  334. "model": "qwen-omni-turbo",
  335. "messages": [
  336. {
  337. "role": "user",
  338. "content": [
  339. {
  340. "type": "input_audio",
  341. "input_audio": {
  342. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  343. "format": "wav"
  344. }
  345. },
  346. {
  347. "type": "text",
  348. "text": "这段音频在说什么"
  349. }
  350. ]
  351. }
  352. ],
  353. "stream": true,
  354. "stream_options": {
  355. "include_usage": true
  356. },
  357. "modalities": ["text", "audio"],
  358. "audio": { "voice": "Cherry", "format": "wav" }
  359. },
  360. success: (res) => {
  361. console.log("请求成功,响应数据:", res.data);
  362. if (typeof res.data === "string" && res.data.includes("data: {")) {
  363. const chunks = res.data.split("data: ").filter((chunk) => chunk.trim() !== "");
  364. chunks.forEach((chunk) => {
  365. this.handleStreamResponse(chunk);
  366. });
  367. } else {
  368. this.handleStreamResponse(res.data);
  369. }
  370. this.playDigitalHumanVideo();
  371. },
  372. fail: (err) => {
  373. console.error("请求失败:", err);
  374. },
  375. complete: () => {
  376. this.loading = false;
  377. }
  378. });
  379. } catch (error) {
  380. console.error("获取数据失败:", error);
  381. this.loading = false;
  382. }
  383. },
  384. handleStreamResponse(data) {
  385. if (typeof data === "string") {
  386. if (data === "[DONE]")
  387. return;
  388. try {
  389. const cleanData = data.trim();
  390. if (cleanData.startsWith("{") && cleanData.endsWith("}")) {
  391. const jsonData = JSON.parse(cleanData);
  392. this.processStreamChunk(jsonData);
  393. }
  394. } catch (e) {
  395. console.error("解析JSON失败:", e, "原始数据:", data);
  396. }
  397. } else {
  398. this.processStreamChunk(data);
  399. }
  400. },
  401. processStreamChunk(chunk) {
  402. if (chunk.choices && chunk.choices.length > 0) {
  403. const choice = chunk.choices[0];
  404. if (choice.delta && choice.delta.content) {
  405. this.assistantResponse += choice.delta.content;
  406. }
  407. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  408. this.audioTranscript += choice.delta.audio.transcript;
  409. }
  410. if (choice.delta) {
  411. const result = {};
  412. if (choice.delta.role) {
  413. result.role = choice.delta.role;
  414. }
  415. if (choice.delta.audio && choice.delta.audio.transcript) {
  416. result.transcript = choice.delta.audio.transcript;
  417. }
  418. if (Object.keys(result).length > 0) {
  419. this.processedResponses.push(result);
  420. }
  421. }
  422. }
  423. },
  424. processResponseData() {
  425. this.processedResponses = this.responses.map((item) => {
  426. const result = {};
  427. if (item.delta && item.delta.role) {
  428. result.role = item.delta.role;
  429. }
  430. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  431. result.transcript = item.delta.audio.transcript;
  432. }
  433. return result;
  434. }).filter((item) => Object.keys(item).length > 0);
  435. },
  436. // 播放数字人视频
  437. playDigitalHumanVideo() {
  438. var _a;
  439. if (JSON.parse(common_vendor.index.getStorageSync("configData")) == null) {
  440. this.videoUrl = this.videoList[this.currentVideoIndex];
  441. } else {
  442. this.videoUrl = ((_a = JSON.parse(common_vendor.index.getStorageSync("configData")).digital_human) == null ? void 0 : _a.ending_video_url) || this.videoList[this.currentVideoIndex];
  443. }
  444. this.videoPlaying = true;
  445. console.log(`播放视频 ${this.currentVideoIndex + 1}/${this.videoList.length}: ${this.videoUrl}`);
  446. this.$nextTick(() => {
  447. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  448. if (videoContext) {
  449. videoContext.play();
  450. setTimeout(() => {
  451. if (this.videoPlaying && this.$refs.videoPlayer) {
  452. console.log("视频应该正在播放");
  453. } else {
  454. console.log("视频可能未成功播放,尝试替代方案");
  455. this.tryAlternativeVideoPath();
  456. }
  457. }, 1e3);
  458. } else {
  459. console.error("无法创建视频上下文");
  460. this.tryAlternativeVideoPath();
  461. }
  462. });
  463. },
  464. // 修改 tryAlternativeVideoPath 方法
  465. tryAlternativeVideoPath() {
  466. console.log("尝试使用替代路径");
  467. const alternativePaths = [
  468. "./static/demo.mp4",
  469. "../static/demo.mp4",
  470. "static/demo.mp4",
  471. "/static/demo.mp4",
  472. // 添加绝对路径
  473. `${window.location.origin}/static/demo.mp4`
  474. ];
  475. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  476. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  477. this.videoUrl = alternativePaths[nextPathIndex];
  478. console.log("尝试新路径:", this.videoUrl);
  479. this.$nextTick(() => {
  480. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  481. if (videoContext) {
  482. videoContext.stop();
  483. videoContext.play();
  484. setTimeout(() => {
  485. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  486. console.log("所有路径均失败,尝试使用uni.getVideoInfo检查视频");
  487. this.checkVideoWithAPI();
  488. }
  489. }, 1e3);
  490. }
  491. });
  492. },
  493. // 添加新方法:使用uni API检查视频
  494. checkVideoWithAPI() {
  495. common_vendor.index.getVideoInfo({
  496. src: "/static/demo.mp4",
  497. success: (res) => {
  498. console.log("视频信息获取成功:", res);
  499. this.videoUrl = "/static/demo.mp4";
  500. this.$nextTick(() => {
  501. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  502. if (videoContext) {
  503. videoContext.play();
  504. }
  505. });
  506. },
  507. fail: (err) => {
  508. console.error("视频信息获取失败:", err);
  509. this.fallbackToLocalVideo();
  510. }
  511. });
  512. },
  513. // 添加新方法:回退到本地视频
  514. fallbackToLocalVideo() {
  515. console.log("尝试使用本地视频资源");
  516. const platform = common_vendor.index.getSystemInfoSync().platform;
  517. if (platform === "android" || platform === "ios") {
  518. this.videoUrl = platform === "android" ? "android.resource://package_name/raw/demo" : "file:///assets/demo.mp4";
  519. this.$nextTick(() => {
  520. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  521. if (videoContext) {
  522. videoContext.play();
  523. }
  524. });
  525. } else {
  526. this.videoPlaying = false;
  527. common_vendor.index.showToast({
  528. title: "视频加载失败,显示静态图片",
  529. icon: "none"
  530. });
  531. }
  532. },
  533. // 修改 handleVideoError 方法
  534. handleVideoError(e) {
  535. console.error("视频加载错误:", e);
  536. if (e && e.detail) {
  537. console.error("详细错误信息:", e.detail);
  538. }
  539. common_vendor.index.getFileInfo({
  540. filePath: this.videoUrl.startsWith("/") ? this.videoUrl.substring(1) : this.videoUrl,
  541. success: (res) => {
  542. console.log("文件存在,大小:", res.size);
  543. this.tryDifferentFormat();
  544. },
  545. fail: (err) => {
  546. console.error("文件不存在或无法访问:", err);
  547. this.tryAlternativeVideoPath();
  548. }
  549. });
  550. common_vendor.index.showToast({
  551. title: "视频加载失败,请检查文件是否存在",
  552. icon: "none",
  553. duration: 2e3
  554. });
  555. },
  556. // 添加新方法:尝试不同格式
  557. tryDifferentFormat() {
  558. console.log("尝试不同的视频格式");
  559. const formats = [
  560. { ext: "mp4", mime: "video/mp4" },
  561. { ext: "webm", mime: "video/webm" },
  562. { ext: "ogg", mime: "video/ogg" },
  563. { ext: "mov", mime: "video/quicktime" }
  564. ];
  565. const currentPath = this.videoUrl;
  566. const basePath = currentPath.substring(0, currentPath.lastIndexOf(".")) || "/static/demo";
  567. let nextFormat = formats.find((f) => !currentPath.endsWith(f.ext));
  568. if (nextFormat) {
  569. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  570. console.log("尝试新格式:", this.videoUrl);
  571. this.$nextTick(() => {
  572. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  573. if (videoContext) {
  574. videoContext.stop();
  575. videoContext.play();
  576. }
  577. });
  578. } else {
  579. this.useBuiltInResource();
  580. }
  581. },
  582. // 添加新方法:使用内置资源
  583. useBuiltInResource() {
  584. console.log("尝试使用内置资源");
  585. const platform = common_vendor.index.getSystemInfoSync().platform;
  586. if (platform === "windows") {
  587. process.env.UNI_INPUT_DIR || "";
  588. this.videoUrl = `./static/demo.mp4`;
  589. console.log("Windows平台尝试路径:", this.videoUrl);
  590. } else if (platform === "android" || platform === "ios") {
  591. this.useNativeVideo();
  592. } else {
  593. const baseUrl = window.location.origin;
  594. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  595. console.log("Web平台尝试URL:", this.videoUrl);
  596. }
  597. this.$nextTick(() => {
  598. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  599. if (videoContext) {
  600. videoContext.play();
  601. }
  602. });
  603. },
  604. // 添加新方法:使用原生视频能力
  605. useNativeVideo() {
  606. console.log("尝试使用原生视频能力");
  607. common_vendor.index.chooseVideo({
  608. sourceType: ["album"],
  609. success: (res) => {
  610. this.videoUrl = res.tempFilePath;
  611. this.$nextTick(() => {
  612. const videoContext = common_vendor.index.createVideoContext("myVideo", this);
  613. if (videoContext) {
  614. videoContext.play();
  615. }
  616. });
  617. },
  618. fail: () => {
  619. this.videoPlaying = false;
  620. common_vendor.index.showToast({
  621. title: "无法加载视频,显示静态图片",
  622. icon: "none"
  623. });
  624. }
  625. });
  626. },
  627. // 处理视频结束事件
  628. handleVideoEnded() {
  629. console.log("视频播放结束,当前视频索引:", this.currentVideoIndex);
  630. this.videoPlaying = false;
  631. this.showStartRecordingButton = true;
  632. this.startCountdown();
  633. this.showAnswerButton = false;
  634. },
  635. // 添加新方法:开始倒计时
  636. startCountdown() {
  637. this.showCountdown = true;
  638. this.countdownValue = 10;
  639. this.countdownTimer = setInterval(() => {
  640. this.countdownValue--;
  641. if (this.countdownValue <= 0) {
  642. this.clearCountdown();
  643. this.showStartRecordingButton = false;
  644. this.startRecordingAnswer();
  645. }
  646. }, 1e3);
  647. },
  648. // 添加新方法:清除倒计时
  649. clearCountdown() {
  650. if (this.countdownTimer) {
  651. clearInterval(this.countdownTimer);
  652. this.countdownTimer = null;
  653. }
  654. this.showCountdown = false;
  655. },
  656. // 修改 handleStartRecordingClick 方法,使其可以终止倒计时
  657. handleStartRecordingClick() {
  658. this.showStartRecordingButton = false;
  659. this.clearCountdown();
  660. this.startRecordingAnswer();
  661. },
  662. // 修复 stopRecordingAnswer 方法,确保录制正确停止并上传
  663. stopRecordingAnswer() {
  664. this.clearCountdown();
  665. const recordingDuration = this.getRecordingDuration();
  666. const minimumDuration = 5;
  667. if (recordingDuration < minimumDuration) {
  668. common_vendor.index.showToast({
  669. title: "录制时间过短,请至少录制5秒",
  670. icon: "none",
  671. duration: 2e3
  672. });
  673. return;
  674. }
  675. this.showStopRecordingButton = false;
  676. if (this.recordingTimer) {
  677. clearInterval(this.recordingTimer);
  678. this.recordingTimer = null;
  679. }
  680. const systemInfo = common_vendor.index.getSystemInfoSync();
  681. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  682. if (isMiniProgram) {
  683. this.stopMiniProgramRecording();
  684. } else {
  685. this.stopBrowserRecording();
  686. }
  687. this.isRecording = false;
  688. },
  689. // 添加新方法:开始录制用户回答
  690. startRecordingAnswer() {
  691. console.log("开始录制用户回答");
  692. this.isRecording = true;
  693. this.recordingStartTime = Date.now();
  694. this.recordingTimerCount = 0;
  695. this.remainingTime = this.maxRecordingTime;
  696. this.recordingTimer = setInterval(() => {
  697. this.recordingTimerCount++;
  698. this.remainingTime = Math.max(0, this.maxRecordingTime - this.recordingTimerCount);
  699. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount) + " / " + this.formatTime(this.maxRecordingTime);
  700. if (this.recordingTimerCount >= this.maxRecordingTime) {
  701. console.log("已达到最大录制时间(60秒),自动停止录制");
  702. this.stopRecordingAnswer();
  703. }
  704. }, 1e3);
  705. const systemInfo = common_vendor.index.getSystemInfoSync();
  706. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  707. if (isMiniProgram) {
  708. this.startMiniProgramRecording();
  709. } else {
  710. this.startBrowserRecording();
  711. }
  712. this.showStopRecordingButton = true;
  713. },
  714. // 添加一个新方法:重置相机组件
  715. resetCamera() {
  716. console.log("重置相机组件");
  717. this.useMiniProgramCameraComponent = false;
  718. if (this.cameraContext) {
  719. this.cameraContext = null;
  720. }
  721. setTimeout(() => {
  722. this.useMiniProgramCameraComponent = true;
  723. setTimeout(() => {
  724. this.cameraContext = common_vendor.index.createCameraContext();
  725. console.log("相机组件已重置");
  726. }, 500);
  727. }, 500);
  728. },
  729. // 修改 startMiniProgramRecording 方法
  730. startMiniProgramRecording() {
  731. console.log("开始小程序录制方法");
  732. const systemInfo = common_vendor.index.getSystemInfoSync();
  733. const isIOS = systemInfo.platform === "ios";
  734. if (isIOS) {
  735. this.resetCamera();
  736. setTimeout(() => {
  737. this.actualStartRecording(isIOS);
  738. }, 1e3);
  739. } else {
  740. this.actualStartRecording(isIOS);
  741. }
  742. },
  743. // 添加新方法:实际开始录制
  744. actualStartRecording(isIOS) {
  745. if (!this.cameraContext) {
  746. this.cameraContext = common_vendor.index.createCameraContext();
  747. console.log("创建新的相机上下文");
  748. }
  749. common_vendor.index.getSetting({
  750. success: (res) => {
  751. const hasRecordAuth = res.authSetting["scope.record"];
  752. const hasCameraAuth = res.authSetting["scope.camera"];
  753. if (!hasRecordAuth || !hasCameraAuth) {
  754. console.warn("缺少必要权限,请求权限");
  755. this.requestMiniProgramPermissions();
  756. return;
  757. }
  758. if (isIOS) {
  759. console.log("iOS: 检查相机状态");
  760. const options = {
  761. timeout: 3e5,
  762. // 300秒超时 (5分钟)
  763. quality: "low",
  764. // 降低质量
  765. compressed: true,
  766. success: () => {
  767. console.log("iOS录制开始成功");
  768. },
  769. fail: (err) => {
  770. console.error("iOS录制失败:", err);
  771. this.useAlternativeRecordingMethod();
  772. }
  773. };
  774. try {
  775. console.log("尝试开始录制");
  776. this.recorder = this.cameraContext.startRecord(options);
  777. } catch (e) {
  778. console.error("开始录制异常:", e);
  779. this.useAlternativeRecordingMethod();
  780. }
  781. } else {
  782. const options = {
  783. timeout: 3e5,
  784. // 300秒超时 (5分钟)
  785. quality: "medium",
  786. compressed: true,
  787. success: () => {
  788. console.log("Android录制开始成功");
  789. },
  790. fail: (err) => {
  791. console.error("Android录制失败:", err);
  792. common_vendor.index.showToast({
  793. title: "录制失败,请检查相机权限",
  794. icon: "none"
  795. });
  796. this.proceedToNextQuestion();
  797. }
  798. };
  799. this.recorder = this.cameraContext.startRecord(options);
  800. }
  801. }
  802. });
  803. },
  804. // 添加新方法:使用替代录制方法
  805. useAlternativeRecordingMethod() {
  806. console.log("使用替代录制方法");
  807. common_vendor.index.showActionSheet({
  808. itemList: ["使用相册中的视频", "跳过此问题"],
  809. success: (res) => {
  810. if (res.tapIndex === 0) {
  811. common_vendor.index.chooseVideo({
  812. sourceType: ["album"],
  813. maxDuration: 300,
  814. // 从60秒改为300秒
  815. camera: "front",
  816. success: (res2) => {
  817. console.log("选择视频成功:", res2.tempFilePath);
  818. this.isRecording = false;
  819. this.showStopRecordingButton = false;
  820. this.uploadRecordedVideo(res2.tempFilePath);
  821. },
  822. fail: () => {
  823. console.log("用户取消选择视频");
  824. this.proceedToNextQuestion();
  825. }
  826. });
  827. } else {
  828. console.log("用户选择跳过问题");
  829. this.proceedToNextQuestion();
  830. }
  831. },
  832. fail: () => {
  833. console.log("操作取消");
  834. this.proceedToNextQuestion();
  835. }
  836. });
  837. },
  838. // 添加新方法:请求小程序权限
  839. requestMiniProgramPermissions() {
  840. common_vendor.index.authorize({
  841. scope: "scope.record",
  842. success: () => {
  843. console.log("录音权限已获取");
  844. common_vendor.index.authorize({
  845. scope: "scope.camera",
  846. success: () => {
  847. console.log("相机权限已获取");
  848. this.startMiniProgramRecording();
  849. },
  850. fail: (err) => {
  851. console.error("相机权限获取失败:", err);
  852. this.showPermissionDialog("相机");
  853. }
  854. });
  855. },
  856. fail: (err) => {
  857. console.error("录音权限获取失败:", err);
  858. this.showPermissionDialog("录音");
  859. }
  860. });
  861. },
  862. // 修改浏览器环境下的录制方法
  863. startBrowserRecording() {
  864. if (!this.cameraStream) {
  865. console.error("没有可用的摄像头流");
  866. common_vendor.index.showToast({
  867. title: "录制失败,摄像头未就绪",
  868. icon: "none"
  869. });
  870. this.proceedToNextQuestion();
  871. return;
  872. }
  873. try {
  874. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  875. if (!hasAudio) {
  876. console.warn("警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流");
  877. navigator.mediaDevices.getUserMedia({
  878. audio: {
  879. echoCancellation: true,
  880. noiseSuppression: true,
  881. autoGainControl: true
  882. },
  883. video: true
  884. }).then((newStream) => {
  885. const audioTracks = newStream.getAudioTracks();
  886. if (audioTracks.length > 0) {
  887. console.log("成功获取音频轨道:", audioTracks[0].label);
  888. const videoTrack = this.cameraStream.getVideoTracks()[0];
  889. const audioTrack = newStream.getAudioTracks()[0];
  890. const combinedStream = new MediaStream();
  891. if (videoTrack)
  892. combinedStream.addTrack(videoTrack);
  893. if (audioTrack)
  894. combinedStream.addTrack(audioTrack);
  895. this.cameraStream = combinedStream;
  896. const videoElement = this.$refs.userCameraVideo;
  897. if (videoElement) {
  898. videoElement.srcObject = combinedStream;
  899. videoElement.muted = true;
  900. }
  901. this.setupMediaRecorder(combinedStream);
  902. } else {
  903. console.warn("仍然无法获取音频轨道");
  904. this.setupMediaRecorder(this.cameraStream);
  905. }
  906. }).catch((err) => {
  907. console.error("获取音频失败:", err);
  908. this.setupMediaRecorder(this.cameraStream);
  909. });
  910. } else {
  911. console.log("检测到音频轨道,直接使用");
  912. this.setupMediaRecorder(this.cameraStream);
  913. }
  914. } catch (error) {
  915. console.error("浏览器录制失败:", error);
  916. common_vendor.index.showToast({
  917. title: "录制失败,浏览器可能不支持此功能",
  918. icon: "none"
  919. });
  920. this.proceedToNextQuestion();
  921. }
  922. },
  923. // 修改 setupMediaRecorder 方法
  924. setupMediaRecorder(stream) {
  925. const videoTracks = stream.getVideoTracks();
  926. const audioTracks = stream.getAudioTracks();
  927. console.log("设置MediaRecorder - 视频轨道:", videoTracks.length, "音频轨道:", audioTracks.length);
  928. let mimeType = "";
  929. const supportedTypes = [
  930. "video/webm;codecs=vp9,opus",
  931. "video/webm;codecs=vp8,opus",
  932. "video/webm;codecs=h264,opus",
  933. "video/mp4;codecs=h264,aac",
  934. "video/webm",
  935. "video/mp4"
  936. ];
  937. for (const type of supportedTypes) {
  938. if (MediaRecorder.isTypeSupported(type)) {
  939. mimeType = type;
  940. console.log("使用支持的MIME类型:", mimeType);
  941. break;
  942. }
  943. }
  944. const options = {
  945. mimeType: mimeType || "",
  946. audioBitsPerSecond: 64e3,
  947. // 降低音频比特率
  948. videoBitsPerSecond: 1e6
  949. // 降低视频比特率到1Mbps
  950. };
  951. try {
  952. this.mediaRecorder = new MediaRecorder(stream, options);
  953. console.log("MediaRecorder创建成功,使用选项:", options);
  954. } catch (e) {
  955. console.warn("使用指定选项创建MediaRecorder失败,尝试使用默认选项");
  956. this.mediaRecorder = new MediaRecorder(stream);
  957. }
  958. this.recordedChunks = [];
  959. this.mediaRecorder.ondataavailable = (event) => {
  960. if (event.data && event.data.size > 0) {
  961. this.recordedChunks.push(event.data);
  962. console.log(`收到数据块: ${event.data.size} 字节`);
  963. }
  964. };
  965. this.mediaRecorder.onstop = async () => {
  966. console.log("MediaRecorder停止,数据块数量:", this.recordedChunks.length);
  967. if (this.recordedChunks.length === 0) {
  968. console.error("没有录制到数据");
  969. common_vendor.index.showToast({
  970. title: "录制失败,未捕获到数据",
  971. icon: "none"
  972. });
  973. this.proceedToNextQuestion();
  974. return;
  975. }
  976. const mimeType2 = this.mediaRecorder.mimeType || "video/webm";
  977. const blob = new Blob(this.recordedChunks, { type: mimeType2 });
  978. console.log("创建Blob,原始大小:", blob.size, "类型:", mimeType2);
  979. common_vendor.index.showLoading({
  980. title: "正在处理视频...",
  981. mask: true
  982. });
  983. try {
  984. const compressedBlob = await this.compressVideo(blob);
  985. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  986. const file = new File([compressedBlob], fileName, { type: mimeType2 });
  987. common_vendor.index.hideLoading();
  988. this.uploadRecordedVideo(file);
  989. } catch (error) {
  990. console.error("视频处理失败:", error);
  991. common_vendor.index.hideLoading();
  992. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  993. const file = new File([blob], fileName, { type: mimeType2 });
  994. this.uploadRecordedVideo(file);
  995. }
  996. };
  997. this.mediaRecorder.onerror = (event) => {
  998. console.error("MediaRecorder错误:", event.error);
  999. };
  1000. try {
  1001. this.mediaRecorder.start(1e3);
  1002. console.log("MediaRecorder开始录制");
  1003. this.mediaRecorderTimeout = setTimeout(() => {
  1004. if (this.mediaRecorder && this.mediaRecorder.state === "recording") {
  1005. console.log("MediaRecorder备份超时机制触发,停止录制");
  1006. this.mediaRecorder.stop();
  1007. }
  1008. }, 3e5);
  1009. } catch (e) {
  1010. console.error("开始录制失败:", e);
  1011. }
  1012. },
  1013. // 添加新方法:停止录制用户回答
  1014. stopRecordingAnswer() {
  1015. console.log("停止录制用户回答");
  1016. if (this.countdownTimer) {
  1017. clearInterval(this.countdownTimer);
  1018. this.countdownTimer = null;
  1019. this.showCountdown = false;
  1020. }
  1021. const recordingDuration = this.getRecordingDuration();
  1022. const minimumDuration = 5;
  1023. if (recordingDuration < minimumDuration) {
  1024. common_vendor.index.showToast({
  1025. title: "录制时间过短,请至少录制5秒",
  1026. icon: "none",
  1027. duration: 2e3
  1028. });
  1029. return;
  1030. }
  1031. this.showStopRecordingButton = false;
  1032. if (this.recordingTimer) {
  1033. clearInterval(this.recordingTimer);
  1034. this.recordingTimer = null;
  1035. }
  1036. const systemInfo = common_vendor.index.getSystemInfoSync();
  1037. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1038. if (isMiniProgram) {
  1039. this.stopMiniProgramRecording();
  1040. } else {
  1041. this.stopBrowserRecording();
  1042. }
  1043. this.isRecording = false;
  1044. },
  1045. // 添加新方法:获取录制时长
  1046. getRecordingDuration() {
  1047. if (this.recordingStartTime) {
  1048. return (Date.now() - this.recordingStartTime) / 1e3;
  1049. }
  1050. if (this.mediaRecorder && this.$refs.userCameraVideo) {
  1051. return this.$refs.userCameraVideo.currentTime || 0;
  1052. }
  1053. if (this.recordingTimerCount) {
  1054. return this.recordingTimerCount;
  1055. }
  1056. return 0;
  1057. },
  1058. // 添加新方法:重置录制
  1059. resetRecording() {
  1060. if (this.recordingTimer) {
  1061. clearTimeout(this.recordingTimer);
  1062. }
  1063. this.recordingStartTime = Date.now();
  1064. this.recordingTimerCount = 0;
  1065. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  1066. this.mediaRecorder.stop();
  1067. this.recordedChunks = [];
  1068. setTimeout(() => {
  1069. this.startBrowserRecording();
  1070. }, 500);
  1071. } else if (this.cameraContext) {
  1072. this.cameraContext.stopRecord({
  1073. success: () => {
  1074. console.log("重置录制:停止当前录制成功");
  1075. setTimeout(() => {
  1076. this.startMiniProgramRecording();
  1077. }, 500);
  1078. },
  1079. fail: (err) => {
  1080. console.error("重置录制:停止当前录制失败", err);
  1081. this.startMiniProgramRecording();
  1082. }
  1083. });
  1084. }
  1085. common_vendor.index.showToast({
  1086. title: "请重新开始回答",
  1087. icon: "none",
  1088. duration: 2e3
  1089. });
  1090. },
  1091. // 修复 completeRecordingStop 方法,确保视频正确上传和处理
  1092. completeRecordingStop() {
  1093. console.log("完成录制停止");
  1094. this.showStopRecordingButton = false;
  1095. if (this.recordingTimer) {
  1096. clearInterval(this.recordingTimer);
  1097. this.recordingTimer = null;
  1098. }
  1099. this.isRecording = false;
  1100. this.showUploadStatus = true;
  1101. this.uploadStatusText = "正在处理视频...";
  1102. this.isUploading = true;
  1103. const systemInfo = common_vendor.index.getSystemInfoSync();
  1104. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1105. if (isMiniProgram) {
  1106. this.stopMiniProgramRecording();
  1107. } else {
  1108. this.stopBrowserRecording();
  1109. }
  1110. },
  1111. // 修复 stopMiniProgramRecording 方法,确保小程序环境下正确停止录制
  1112. stopMiniProgramRecording() {
  1113. console.log("停止小程序录制");
  1114. if (!this.cameraContext) {
  1115. console.error("相机上下文不存在");
  1116. this.handleRecordingError(new Error("相机上下文不存在"));
  1117. return;
  1118. }
  1119. try {
  1120. common_vendor.index.showLoading({
  1121. title: "正在处理视频...",
  1122. mask: true
  1123. });
  1124. this.cameraContext.stopRecord({
  1125. success: (res) => {
  1126. console.log("停止录制成功:", res);
  1127. common_vendor.index.hideLoading();
  1128. const tempVideoPath = res.tempVideoPath || res.tempFilePath;
  1129. if (tempVideoPath) {
  1130. console.log("获取到视频临时路径:", tempVideoPath);
  1131. this.uploadRecordedVideo(tempVideoPath);
  1132. } else {
  1133. console.error("未获取到视频临时路径");
  1134. this.handleRecordingError(new Error("未获取到视频临时路径"));
  1135. }
  1136. },
  1137. fail: (err) => {
  1138. console.error("停止录制失败:", err);
  1139. common_vendor.index.hideLoading();
  1140. this.handleRecordingError(err);
  1141. }
  1142. });
  1143. } catch (e) {
  1144. console.error("停止录制异常:", e);
  1145. common_vendor.index.hideLoading();
  1146. this.handleRecordingError(e);
  1147. }
  1148. },
  1149. // 添加新方法:停止浏览器录制
  1150. stopBrowserRecording() {
  1151. if (this.mediaRecorder && this.mediaRecorder.state !== "inactive") {
  1152. this.mediaRecorder.stop();
  1153. console.log("浏览器录制停止成功");
  1154. } else {
  1155. console.error("MediaRecorder不存在或已经停止");
  1156. this.proceedToNextQuestion();
  1157. }
  1158. },
  1159. // 修复 uploadRecordedVideo 方法,确保视频正确上传到服务器
  1160. uploadRecordedVideo(videoPath) {
  1161. console.log("开始上传录制的视频:", videoPath);
  1162. this.lastVideoToRetry = videoPath;
  1163. this.showUploadStatus = true;
  1164. this.uploadStatusText = "正在上传视频...";
  1165. this.isUploading = true;
  1166. this.saveUploadStatus();
  1167. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || "1";
  1168. const openid = common_vendor.index.getStorageSync("userInfo") ? JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid : "";
  1169. const application_id = common_vendor.index.getStorageSync("appId") || "";
  1170. const uploadTask = common_vendor.index.uploadFile({
  1171. url: `${common_config.apiBaseUrl}/api/upload/`,
  1172. filePath: videoPath,
  1173. name: "file",
  1174. formData: {
  1175. tenant_id,
  1176. openid,
  1177. application_id,
  1178. biz_type: "interview_video",
  1179. video_duration: this.recordingTimerCount || 0,
  1180. has_audio: "true"
  1181. // 明确标记包含音频
  1182. },
  1183. success: (uploadRes) => {
  1184. var _a, _b;
  1185. console.log("视频上传成功:", uploadRes);
  1186. try {
  1187. const result = typeof uploadRes.data === "string" ? JSON.parse(uploadRes.data) : uploadRes.data;
  1188. if ((result.success || result.code === 2e3) && (((_a = result.data) == null ? void 0 : _a.url) || ((_b = result.data) == null ? void 0 : _b.permanent_link))) {
  1189. const videoUrl = result.data.url || result.data.permanent_link || "";
  1190. console.log("视频URL:", videoUrl);
  1191. this.uploadStatusText = "视频处理完成,正在分析...";
  1192. this.saveUploadStatus();
  1193. this.submitToAiVoiceInteraction(videoUrl);
  1194. } else {
  1195. console.error("上传响应格式错误:", result);
  1196. const errorMsg = result.msg || result.message || "上传响应格式错误";
  1197. this.handleUploadError(new Error(errorMsg));
  1198. }
  1199. } catch (e) {
  1200. console.error("解析上传响应失败:", e);
  1201. this.handleUploadError(e);
  1202. }
  1203. },
  1204. fail: (err) => {
  1205. console.error("视频上传失败:", err);
  1206. let errorMessage = "网络错误,请检查网络连接";
  1207. if (err.errMsg) {
  1208. if (err.errMsg.includes("timeout")) {
  1209. errorMessage = "上传超时,请检查网络连接";
  1210. } else if (err.errMsg.includes("fail")) {
  1211. errorMessage = "上传失败: " + err.errMsg;
  1212. }
  1213. }
  1214. this.handleUploadError(new Error(errorMessage));
  1215. },
  1216. complete: () => {
  1217. this.showUploadStatus = false;
  1218. this.isUploading = false;
  1219. this.saveUploadStatus();
  1220. }
  1221. });
  1222. uploadTask.onProgressUpdate((res) => {
  1223. console.log("上传进度:", res.progress);
  1224. this.uploadStatusText = `正在上传视频... ${res.progress}%`;
  1225. if (res.progress < 10 && res.totalTimeMs > 1e4) {
  1226. this.uploadStatusText = `上传速度较慢,请耐心等待... ${res.progress}%`;
  1227. }
  1228. });
  1229. },
  1230. // 修改 processUploadQueue 方法,添加后台上传支持
  1231. processUploadQueue() {
  1232. if (this.uploadQueue.length === 0) {
  1233. this.isUploading = false;
  1234. this.showUploadStatus = false;
  1235. try {
  1236. common_vendor.index.removeStorageSync("videoUploadStatus");
  1237. } catch (e) {
  1238. console.error("清除上传状态失败:", e);
  1239. }
  1240. this.notifyUploadComplete();
  1241. return;
  1242. }
  1243. this.isUploading = true;
  1244. this.showUploadStatus = true;
  1245. const task = this.uploadQueue[0];
  1246. this.uploadStatus[task.id] = "uploading";
  1247. this.updateUploadStatusText();
  1248. task.attempts++;
  1249. if (typeof task.file !== "string") {
  1250. this.uploadFileWithXHR(task);
  1251. } else {
  1252. this.uploadFileWithUni(task);
  1253. }
  1254. this.saveUploadStatus();
  1255. },
  1256. // 添加新方法:发送上传完成通知
  1257. notifyUploadComplete() {
  1258. try {
  1259. common_vendor.index.$emit("videoUploadComplete", {
  1260. timestamp: Date.now()
  1261. });
  1262. console.log("发送上传完成通知");
  1263. } catch (e) {
  1264. console.error("发送上传完成通知失败:", e);
  1265. }
  1266. const systemInfo = common_vendor.index.getSystemInfoSync();
  1267. if (systemInfo.platform === "android" || systemInfo.platform === "ios") {
  1268. if (plus && plus.push) {
  1269. plus.push.createMessage("视频上传完成", "您的面试视频已成功上传", {});
  1270. }
  1271. }
  1272. },
  1273. // 添加新方法:使用XMLHttpRequest上传文件
  1274. uploadFileWithXHR(task) {
  1275. const userInfo = common_vendor.index.getStorageSync("userInfo");
  1276. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  1277. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || "1";
  1278. const formData = new FormData();
  1279. formData.append("file", task.file);
  1280. formData.append("openid", openid);
  1281. formData.append("tenant_id", tenant_id);
  1282. formData.append("application_id", common_vendor.index.getStorageSync("appId"));
  1283. formData.append("question_id", task.questionId);
  1284. formData.append("video_duration", this.recordingTimerCount || 0);
  1285. formData.append("has_audio", "true");
  1286. const xhr = new XMLHttpRequest();
  1287. xhr.open("POST", `${common_config.apiBaseUrl}/api/upload/`, true);
  1288. xhr.timeout = 12e4;
  1289. xhr.upload.onprogress = (event) => {
  1290. if (event.lengthComputable) {
  1291. const percentComplete = Math.round(event.loaded / event.total * 100);
  1292. this.uploadProgress[task.id] = percentComplete;
  1293. this.updateUploadStatusText();
  1294. }
  1295. };
  1296. xhr.onload = () => {
  1297. if (xhr.status === 200) {
  1298. try {
  1299. const res = JSON.parse(xhr.responseText);
  1300. console.log("上传响应:", res);
  1301. if (res.code === 2e3) {
  1302. const videoUrl = res.data.url || res.data.photoUrl || "";
  1303. if (videoUrl) {
  1304. this.uploadStatus[task.id] = "success";
  1305. this.updateUploadStatusText();
  1306. this.submitVideoToInterview(videoUrl, task);
  1307. } else {
  1308. this.handleUploadFailure(task, "视频URL获取失败");
  1309. }
  1310. } else {
  1311. this.handleUploadFailure(task, res.msg || "上传失败");
  1312. }
  1313. } catch (e) {
  1314. this.handleUploadFailure(task, "解析响应失败");
  1315. }
  1316. } else {
  1317. this.handleUploadFailure(task, "HTTP状态: " + xhr.status);
  1318. }
  1319. };
  1320. xhr.onerror = () => {
  1321. this.handleUploadFailure(task, "网络错误");
  1322. };
  1323. xhr.ontimeout = () => {
  1324. this.handleUploadFailure(task, "上传超时");
  1325. };
  1326. xhr.send(formData);
  1327. },
  1328. // 添加新方法:使用uni.uploadFile上传文件
  1329. uploadFileWithUni(task) {
  1330. const userInfo = common_vendor.index.getStorageSync("userInfo");
  1331. const openid = userInfo ? JSON.parse(userInfo).openid || "" : "";
  1332. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || "1";
  1333. const uploadTask = common_vendor.index.uploadFile({
  1334. url: `${common_config.apiBaseUrl}/api/upload/`,
  1335. filePath: task.file,
  1336. name: "file",
  1337. formData: {
  1338. openid,
  1339. tenant_id,
  1340. application_id: common_vendor.index.getStorageSync("appId"),
  1341. question_id: task.questionId,
  1342. video_duration: this.recordingTimerCount || 0,
  1343. has_audio: "true"
  1344. // 明确标记包含音频
  1345. },
  1346. success: (uploadRes) => {
  1347. try {
  1348. const res = JSON.parse(uploadRes.data);
  1349. console.log("上传响应:", res);
  1350. if (res.code === 2e3) {
  1351. const videoUrl = res.data.permanent_link || res.data.url || "";
  1352. if (videoUrl) {
  1353. this.uploadStatus[task.id] = "success";
  1354. this.updateUploadStatusText();
  1355. this.submitVideoToInterview(videoUrl, task);
  1356. } else {
  1357. this.handleUploadFailure(task, "视频URL获取失败");
  1358. }
  1359. } else {
  1360. this.handleUploadFailure(task, res.msg || "上传失败");
  1361. }
  1362. } catch (e) {
  1363. this.handleUploadFailure(task, "解析响应失败");
  1364. }
  1365. },
  1366. fail: (err) => {
  1367. this.handleUploadFailure(task, err.errMsg || "上传失败");
  1368. }
  1369. });
  1370. uploadTask.onProgressUpdate((res) => {
  1371. this.uploadProgress[task.id] = res.progress;
  1372. this.updateUploadStatusText();
  1373. });
  1374. },
  1375. // 添加新方法:处理上传失败
  1376. handleUploadFailure(task, errorMsg) {
  1377. console.error("上传失败:", errorMsg);
  1378. this.uploadStatus[task.id] = "failed";
  1379. this.updateUploadStatusText();
  1380. if (task.attempts < task.maxAttempts) {
  1381. console.log(`将在5秒后重试上传,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1382. setTimeout(() => {
  1383. this.uploadProgress[task.id] = 0;
  1384. if (typeof task.file !== "string") {
  1385. this.uploadFileWithXHR(task);
  1386. } else {
  1387. this.uploadFileWithUni(task);
  1388. }
  1389. }, 5e3);
  1390. } else {
  1391. console.log("超过最大重试次数,放弃上传");
  1392. common_vendor.index.showToast({
  1393. title: "视频上传失败,请稍后重试",
  1394. icon: "none",
  1395. duration: 2e3
  1396. });
  1397. this.uploadQueue.shift();
  1398. this.processUploadQueue();
  1399. }
  1400. },
  1401. // 修改 submitVideoToInterview 方法
  1402. submitVideoToInterview(videoUrl, task = null) {
  1403. console.log("提交视频URL到面试接口:", videoUrl);
  1404. let questionId;
  1405. if (task) {
  1406. questionId = task.questionId;
  1407. } else {
  1408. switch (this.currentVideoIndex) {
  1409. case 1:
  1410. questionId = 36;
  1411. break;
  1412. case 2:
  1413. questionId = 11;
  1414. break;
  1415. case 3:
  1416. questionId = 12;
  1417. break;
  1418. case 4:
  1419. questionId = 13;
  1420. break;
  1421. default:
  1422. questionId = 10;
  1423. }
  1424. }
  1425. ({
  1426. application_id: common_vendor.index.getStorageSync("appId"),
  1427. question_id: questionId,
  1428. video_url: videoUrl,
  1429. tenant_id: common_vendor.index.getStorageSync("tenant_id") || "1",
  1430. has_audio: true
  1431. // 明确标记包含音频
  1432. });
  1433. this.submitToAiVoiceInteraction(videoUrl);
  1434. },
  1435. // 添加新方法:处理提交失败
  1436. handleSubmitFailure(task, errorMsg) {
  1437. console.error("提交失败:", errorMsg);
  1438. this.uploadStatus[task.id] = "failed";
  1439. this.updateUploadStatusText();
  1440. if (task.attempts < task.maxAttempts) {
  1441. console.log(`将在5秒后重试提交,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1442. setTimeout(() => {
  1443. this.submitVideoToInterview(task.videoUrl, task);
  1444. }, 5e3);
  1445. } else {
  1446. console.log("超过最大重试次数,放弃提交");
  1447. common_vendor.index.showToast({
  1448. title: "视频提交失败,请稍后重试",
  1449. icon: "none",
  1450. duration: 2e3
  1451. });
  1452. this.uploadQueue.shift();
  1453. this.processUploadQueue();
  1454. }
  1455. },
  1456. // 添加新方法:更新上传状态文本
  1457. updateUploadStatusText() {
  1458. if (this.uploadQueue.length === 0) {
  1459. this.uploadStatusText = "";
  1460. return;
  1461. }
  1462. const currentTask = this.uploadQueue[0];
  1463. const progress = this.uploadProgress[currentTask.id] || 0;
  1464. const status = this.uploadStatus[currentTask.id] || "pending";
  1465. let statusText = "";
  1466. switch (status) {
  1467. case "pending":
  1468. statusText = "等待上传";
  1469. break;
  1470. case "uploading":
  1471. statusText = `上传中 ${progress}%`;
  1472. break;
  1473. case "success":
  1474. statusText = "上传成功,提交中...";
  1475. break;
  1476. case "failed":
  1477. statusText = `上传失败,${currentTask.attempts < currentTask.maxAttempts ? "即将重试" : "已放弃"}`;
  1478. break;
  1479. }
  1480. this.uploadStatusText = `问题${currentTask.questionId - 9}:${statusText}`;
  1481. if (this.uploadQueue.length > 1) {
  1482. this.uploadStatusText += ` (${this.uploadQueue.length}个视频待处理)`;
  1483. }
  1484. },
  1485. // 修改 proceedToNextQuestion 方法
  1486. proceedToNextQuestion() {
  1487. console.log("准备跳转到下一页面");
  1488. if (this.isPlayingAiVoice) {
  1489. console.log("AI语音正在播放,等待播放完成后再跳转");
  1490. return;
  1491. }
  1492. if (this.uploadQueue.length > 0) {
  1493. console.log("上传队列将在后台继续处理...");
  1494. this.saveUploadStatus();
  1495. }
  1496. },
  1497. // 修改 navigateToNextPage 方法
  1498. navigateToNextPage() {
  1499. console.log("准备导航到下一个页面");
  1500. if (this.isPlayingAiVoice) {
  1501. console.log("AI语音正在播放,等待播放完成后再跳转");
  1502. return;
  1503. }
  1504. console.log("导航到下一个页面");
  1505. try {
  1506. const currentJobDetail = common_vendor.index.getStorageSync("selectedJob");
  1507. const jobId = currentJobDetail ? JSON.parse(currentJobDetail).id : null;
  1508. if (jobId === 9 || jobId === 10) {
  1509. common_vendor.index.navigateTo({
  1510. url: "/pages/success/success",
  1511. success: () => {
  1512. console.log("成功跳转到成功页面");
  1513. },
  1514. fail: (err) => {
  1515. console.error("跳转失败:", err);
  1516. this.handleNavigationFailure();
  1517. }
  1518. });
  1519. return;
  1520. }
  1521. } catch (e) {
  1522. console.error("解析 selectedJob 失败:", e);
  1523. }
  1524. common_vendor.index.navigateTo({
  1525. url: "/pages/posture-guide/posture-guide?uploading=" + (this.uploadQueue.length > 0 ? "true" : "false"),
  1526. success: () => {
  1527. console.log("成功跳转到结果页面");
  1528. },
  1529. fail: (err) => {
  1530. console.error("跳转失败:", err);
  1531. this.handleNavigationFailure();
  1532. }
  1533. });
  1534. },
  1535. // 添加处理导航失败的方法
  1536. handleNavigationFailure() {
  1537. common_vendor.index.navigateTo({
  1538. url: "/pages/posture-guide/posture-guide",
  1539. fail: (err2) => {
  1540. console.error("备用跳转也失败:", err2);
  1541. common_vendor.index.navigateBack({
  1542. delta: 1
  1543. });
  1544. }
  1545. });
  1546. },
  1547. // 修改 handleAnswerButtonClick 方法
  1548. handleAnswerButtonClick() {
  1549. this.showAnswerButton = false;
  1550. common_vendor.index.showToast({
  1551. title: "面试完成",
  1552. icon: "success",
  1553. duration: 500
  1554. // 只显示0.5秒
  1555. });
  1556. if (this.lastUploadedVideoUrl) {
  1557. this.submitToAiVoiceInteraction(this.lastUploadedVideoUrl);
  1558. } else {
  1559. this.navigateToNextPage();
  1560. }
  1561. },
  1562. // 处理相机错误
  1563. handleCameraError(e) {
  1564. console.error("相机错误:", e);
  1565. const systemInfo = common_vendor.index.getSystemInfoSync();
  1566. const isIOS = systemInfo.platform === "ios";
  1567. if (isIOS) {
  1568. console.log("iOS相机错误,尝试重新初始化");
  1569. common_vendor.index.showToast({
  1570. title: "相机初始化中...",
  1571. icon: "loading",
  1572. duration: 2e3
  1573. });
  1574. this.resetCamera();
  1575. if (this.isRecording) {
  1576. this.isRecording = false;
  1577. this.showStopRecordingButton = false;
  1578. setTimeout(() => {
  1579. this.useAlternativeRecordingMethod();
  1580. }, 1e3);
  1581. }
  1582. } else {
  1583. common_vendor.index.showToast({
  1584. title: "相机初始化失败,请检查权限设置",
  1585. icon: "none"
  1586. });
  1587. this.tryFallbackOptions();
  1588. }
  1589. },
  1590. // 添加新方法:尝试备用选项
  1591. tryFallbackOptions() {
  1592. const systemInfo = common_vendor.index.getSystemInfoSync();
  1593. if (systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay") {
  1594. this.useMiniProgramCamera();
  1595. } else {
  1596. this.showStaticCameraPlaceholder();
  1597. }
  1598. },
  1599. // 添加新方法:使用小程序相机API
  1600. useMiniProgramCamera() {
  1601. console.log("尝试使用小程序相机组件");
  1602. this.useMiniProgramCameraComponent = true;
  1603. },
  1604. // 添加新方法:显示静态图像
  1605. showStaticCameraPlaceholder() {
  1606. console.log("显示静态摄像头占位图");
  1607. const img = document.createElement("img");
  1608. img.src = "/static/images/camera-placeholder.png";
  1609. img.className = "static-camera-image";
  1610. img.style.width = "100%";
  1611. img.style.height = "100%";
  1612. img.style.objectFit = "cover";
  1613. const container = this.$refs.userCameraVideo.parentNode;
  1614. container.appendChild(img);
  1615. },
  1616. // 处理视频时间更新事件
  1617. handleTimeUpdate(e) {
  1618. const currentTime = e.target.currentTime;
  1619. if (this.isRecording && this.recordingTimerCount) {
  1620. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  1621. }
  1622. let currentSubtitles;
  1623. if (this.currentVideoIndex === 0) {
  1624. currentSubtitles = this.subtitles;
  1625. } else if (this.currentVideoIndex === 1) {
  1626. currentSubtitles = this.secondVideoSubtitles;
  1627. } else if (this.currentVideoIndex === 2) {
  1628. currentSubtitles = this.thirdVideoSubtitles;
  1629. } else if (this.currentVideoIndex === 3) {
  1630. currentSubtitles = this.fourthVideoSubtitles;
  1631. } else if (this.currentVideoIndex === 4) {
  1632. currentSubtitles = this.fifthVideoSubtitles;
  1633. } else if (this.currentVideoIndex === 5) {
  1634. currentSubtitles = this.sixthVideoSubtitles;
  1635. } else {
  1636. currentSubtitles = [];
  1637. }
  1638. const subtitle = currentSubtitles.find(
  1639. (sub) => currentTime >= sub.startTime && currentTime < sub.endTime
  1640. );
  1641. this.currentSubtitle = subtitle ? subtitle.text : "";
  1642. },
  1643. // Add a new method to handle the "Start Recording" button click
  1644. handleStartRecordingClick() {
  1645. this.showStartRecordingButton = false;
  1646. this.clearCountdown();
  1647. this.startRecordingAnswer();
  1648. },
  1649. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  1650. checkAudioPermission() {
  1651. const systemInfo = common_vendor.index.getSystemInfoSync();
  1652. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1653. if (isMiniProgram) {
  1654. common_vendor.index.getSetting({
  1655. success: (res) => {
  1656. if (!res.authSetting["scope.record"]) {
  1657. common_vendor.index.authorize({
  1658. scope: "scope.record",
  1659. success: () => {
  1660. console.log("录音权限已获取");
  1661. },
  1662. fail: (err) => {
  1663. console.error("录音权限获取失败:", err);
  1664. this.showPermissionDialog("录音");
  1665. }
  1666. });
  1667. }
  1668. if (!res.authSetting["scope.camera"]) {
  1669. common_vendor.index.authorize({
  1670. scope: "scope.camera",
  1671. success: () => {
  1672. console.log("相机权限已获取");
  1673. },
  1674. fail: (err) => {
  1675. console.error("相机权限获取失败:", err);
  1676. this.showPermissionDialog("相机");
  1677. }
  1678. });
  1679. }
  1680. }
  1681. });
  1682. }
  1683. },
  1684. // 添加新方法:测试音频输入
  1685. testAudioInput() {
  1686. if (!this.cameraStream) {
  1687. console.warn("没有可用的媒体流,无法测试音频");
  1688. return;
  1689. }
  1690. const audioTracks = this.cameraStream.getAudioTracks();
  1691. if (audioTracks.length === 0) {
  1692. console.warn("没有检测到音频轨道,尝试重新获取");
  1693. this.tryGetAudioOnly();
  1694. return;
  1695. }
  1696. console.log("音频轨道信息:", audioTracks[0].getSettings());
  1697. try {
  1698. const AudioContext = window.AudioContext || window.webkitAudioContext;
  1699. if (!AudioContext) {
  1700. console.warn("浏览器不支持AudioContext");
  1701. return;
  1702. }
  1703. const audioContext = new AudioContext();
  1704. const analyser = audioContext.createAnalyser();
  1705. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  1706. microphone.connect(analyser);
  1707. analyser.fftSize = 256;
  1708. const bufferLength = analyser.frequencyBinCount;
  1709. const dataArray = new Uint8Array(bufferLength);
  1710. let silenceCounter = 0;
  1711. const checkAudio = () => {
  1712. if (this.isRecording)
  1713. return;
  1714. analyser.getByteFrequencyData(dataArray);
  1715. let sum = 0;
  1716. for (let i = 0; i < bufferLength; i++) {
  1717. sum += dataArray[i];
  1718. }
  1719. const average = sum / bufferLength;
  1720. if (average > 10) {
  1721. console.log("检测到音频输入,音量:", average);
  1722. silenceCounter = 0;
  1723. } else {
  1724. silenceCounter++;
  1725. if (silenceCounter > 10) {
  1726. console.warn("持续检测不到音频输入,可能麦克风未正常工作");
  1727. silenceCounter = 0;
  1728. }
  1729. }
  1730. requestAnimationFrame(checkAudio);
  1731. };
  1732. checkAudio();
  1733. } catch (e) {
  1734. console.error("音频测试失败:", e);
  1735. }
  1736. },
  1737. // 添加新方法:尝试单独获取音频
  1738. tryGetAudioOnly() {
  1739. navigator.mediaDevices.getUserMedia({ audio: true }).then((audioStream) => {
  1740. if (this.cameraStream) {
  1741. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1742. const audioTrack = audioStream.getAudioTracks()[0];
  1743. const combinedStream = new MediaStream();
  1744. if (videoTrack)
  1745. combinedStream.addTrack(videoTrack);
  1746. if (audioTrack)
  1747. combinedStream.addTrack(audioTrack);
  1748. this.cameraStream = combinedStream;
  1749. const videoElement = this.$refs.userCameraVideo;
  1750. if (videoElement) {
  1751. videoElement.srcObject = combinedStream;
  1752. videoElement.muted = true;
  1753. }
  1754. console.log("成功合并音频和视频轨道");
  1755. } else {
  1756. console.warn("没有视频流可合并");
  1757. }
  1758. }).catch((err) => {
  1759. console.error("单独获取音频失败:", err);
  1760. });
  1761. },
  1762. // 添加新方法:显示权限对话框
  1763. showPermissionDialog(permissionType) {
  1764. common_vendor.index.showModal({
  1765. title: "需要权限",
  1766. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  1767. confirmText: "去设置",
  1768. success: (res) => {
  1769. if (res.confirm) {
  1770. common_vendor.index.openSetting({
  1771. success: (settingRes) => {
  1772. console.log("设置页面打开成功", settingRes);
  1773. }
  1774. });
  1775. }
  1776. }
  1777. });
  1778. },
  1779. // 添加重试上传方法
  1780. retryVideoUpload() {
  1781. if (this.lastVideoToRetry) {
  1782. this.showRetryButton = false;
  1783. common_vendor.index.showLoading({
  1784. title: "正在重新提交...",
  1785. mask: true
  1786. });
  1787. this.submitVideoToInterview(this.lastVideoToRetry);
  1788. } else {
  1789. common_vendor.index.showToast({
  1790. title: "没有可重试的视频",
  1791. icon: "none"
  1792. });
  1793. }
  1794. },
  1795. // 添加一个新方法用于压缩视频
  1796. async compressVideo(videoBlob) {
  1797. if (videoBlob.size < 5 * 1024 * 1024) {
  1798. return videoBlob;
  1799. }
  1800. console.log("开始压缩视频,原始大小:", videoBlob.size);
  1801. const videoElement = document.createElement("video");
  1802. videoElement.muted = true;
  1803. videoElement.autoplay = false;
  1804. const canvas = document.createElement("canvas");
  1805. const ctx = canvas.getContext("2d");
  1806. videoElement.src = URL.createObjectURL(videoBlob);
  1807. return new Promise((resolve) => {
  1808. videoElement.onloadedmetadata = () => {
  1809. const width = Math.floor(videoElement.videoWidth / 2);
  1810. const height = Math.floor(videoElement.videoHeight / 2);
  1811. canvas.width = width;
  1812. canvas.height = height;
  1813. const stream = canvas.captureStream(15);
  1814. if (videoElement.captureStream) {
  1815. const originalStream = videoElement.captureStream();
  1816. const audioTracks = originalStream.getAudioTracks();
  1817. if (audioTracks.length > 0) {
  1818. stream.addTrack(audioTracks[0]);
  1819. }
  1820. }
  1821. const options = {
  1822. mimeType: "video/webm;codecs=vp8,opus",
  1823. audioBitsPerSecond: 64e3,
  1824. videoBitsPerSecond: 8e5
  1825. // 800kbps
  1826. };
  1827. const mediaRecorder = new MediaRecorder(stream, options);
  1828. const chunks = [];
  1829. mediaRecorder.ondataavailable = (e) => {
  1830. if (e.data.size > 0) {
  1831. chunks.push(e.data);
  1832. }
  1833. };
  1834. mediaRecorder.onstop = () => {
  1835. const compressedBlob = new Blob(chunks, { type: "video/webm" });
  1836. console.log("视频压缩完成,压缩后大小:", compressedBlob.size);
  1837. resolve(compressedBlob);
  1838. };
  1839. videoElement.onplay = () => {
  1840. mediaRecorder.start(100);
  1841. const drawFrame = () => {
  1842. if (videoElement.paused || videoElement.ended) {
  1843. mediaRecorder.stop();
  1844. return;
  1845. }
  1846. ctx.drawImage(videoElement, 0, 0, width, height);
  1847. requestAnimationFrame(drawFrame);
  1848. };
  1849. drawFrame();
  1850. };
  1851. videoElement.play();
  1852. };
  1853. });
  1854. },
  1855. // 修改 checkIOSCameraRecordPermission 方法
  1856. checkIOSCameraRecordPermission() {
  1857. const systemInfo = common_vendor.index.getSystemInfoSync();
  1858. if (systemInfo.platform !== "ios")
  1859. return;
  1860. common_vendor.index.getSetting({
  1861. success: (res) => {
  1862. if (!res.authSetting["scope.camera"]) {
  1863. common_vendor.index.authorize({
  1864. scope: "scope.camera",
  1865. success: () => {
  1866. console.log("iOS相机权限已获取");
  1867. },
  1868. fail: (err) => {
  1869. console.error("iOS相机权限获取失败:", err);
  1870. this.showPermissionDialog("相机");
  1871. }
  1872. });
  1873. }
  1874. if (!res.authSetting["scope.record"]) {
  1875. common_vendor.index.authorize({
  1876. scope: "scope.record",
  1877. success: () => {
  1878. console.log("iOS录音权限已获取");
  1879. },
  1880. fail: (err) => {
  1881. console.error("iOS录音权限获取失败:", err);
  1882. this.showPermissionDialog("录音");
  1883. }
  1884. });
  1885. }
  1886. }
  1887. });
  1888. },
  1889. // 添加新方法:检查并修复渲染问题
  1890. checkAndFixRenderingIssues() {
  1891. try {
  1892. if (typeof u !== "undefined" && u) {
  1893. if (!u.currentQuestion) {
  1894. console.log("修复: 创建缺失的currentQuestion对象");
  1895. u.currentQuestion = {};
  1896. }
  1897. if (u.currentQuestion && typeof u.currentQuestion.isImportant === "undefined") {
  1898. console.log("修复: 设置缺失的isImportant属性");
  1899. u.currentQuestion.isImportant = false;
  1900. }
  1901. }
  1902. } catch (e) {
  1903. console.log("防御性检查异常:", e);
  1904. }
  1905. },
  1906. // 添加格式化时间的辅助方法
  1907. formatTime(seconds) {
  1908. if (!seconds && seconds !== 0)
  1909. return "03:30";
  1910. const minutes = Math.floor(seconds / 60);
  1911. const remainingSeconds = seconds % 60;
  1912. return `${minutes.toString().padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
  1913. },
  1914. // 添加新方法:保存上传状态到本地存储
  1915. saveUploadStatus() {
  1916. const uploadStatus = {
  1917. isUploading: this.isUploading,
  1918. uploadQueue: this.uploadQueue.length,
  1919. timestamp: Date.now()
  1920. };
  1921. try {
  1922. common_vendor.index.setStorageSync("videoUploadStatus", JSON.stringify(uploadStatus));
  1923. } catch (e) {
  1924. console.error("保存上传状态失败:", e);
  1925. }
  1926. },
  1927. // 修改 submitToAiVoiceInteraction 方法
  1928. submitToAiVoiceInteraction(videoUrl) {
  1929. console.log("提交视频到AI语音交互接口:", videoUrl);
  1930. common_vendor.index.showLoading({
  1931. title: "面试官正在思考...",
  1932. mask: true
  1933. });
  1934. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || "1";
  1935. const application_id = common_vendor.index.getStorageSync("appId") || "98";
  1936. let positionConfigId = 1;
  1937. if (JSON.parse(common_vendor.index.getStorageSync("configData"))) {
  1938. positionConfigId = JSON.parse(common_vendor.index.getStorageSync("configData")).id;
  1939. }
  1940. const requestData = {
  1941. voice_url: videoUrl,
  1942. tenant_id,
  1943. application_id,
  1944. scene_type: "interview",
  1945. // 场景类型:面试
  1946. // voice_type: 'longxiaoxia', // 角色:龙小侠
  1947. position_config_id: positionConfigId,
  1948. conversation_history: this.conversationHistory
  1949. // 添加对话历史
  1950. };
  1951. const systemInfo = common_vendor.index.getSystemInfoSync();
  1952. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1953. if (isMiniProgram) {
  1954. common_vendor.index.request({
  1955. url: `${common_config.apiBaseUrl}/api/system/voice/mcp/interaction`,
  1956. // 使用新的接口地址
  1957. method: "POST",
  1958. data: requestData,
  1959. header: {
  1960. "content-type": "application/json"
  1961. // 修改为JSON格式
  1962. },
  1963. success: (res) => {
  1964. if (res.statusCode === 200) {
  1965. this.handleAiInteractionResponse(res.data);
  1966. } else {
  1967. this.handleAiInteractionError(res.data);
  1968. }
  1969. },
  1970. fail: (err) => {
  1971. this.handleAiInteractionError(err);
  1972. }
  1973. });
  1974. } else {
  1975. const xhr = new XMLHttpRequest();
  1976. xhr.open("POST", `${common_config.apiBaseUrl}/api/system/voice/mcp/interaction`, true);
  1977. xhr.timeout = 6e4;
  1978. xhr.setRequestHeader("Content-Type", "application/json");
  1979. xhr.onload = () => {
  1980. if (xhr.status === 200) {
  1981. try {
  1982. const response = JSON.parse(xhr.responseText);
  1983. this.handleAiInteractionResponse(response);
  1984. } catch (e) {
  1985. console.error("解析AI响应失败:", e);
  1986. this.handleAiInteractionError({ errMsg: "解析响应失败" });
  1987. }
  1988. } else {
  1989. this.handleAiInteractionError({ errMsg: "HTTP状态: " + xhr.status });
  1990. }
  1991. };
  1992. xhr.onerror = () => {
  1993. this.handleAiInteractionError({ errMsg: "网络错误" });
  1994. };
  1995. xhr.ontimeout = () => {
  1996. this.handleAiInteractionError({ errMsg: "请求超时" });
  1997. };
  1998. xhr.send(JSON.stringify(requestData));
  1999. }
  2000. },
  2001. // 添加处理AI交互响应的方法
  2002. handleAiInteractionResponse(data) {
  2003. console.log("AI语音交互接口响应:", data);
  2004. const systemInfo = common_vendor.index.getSystemInfoSync();
  2005. const isIOS = systemInfo.platform === "ios";
  2006. this.isPlayingAiVoice = true;
  2007. common_vendor.index.hideLoading();
  2008. this.hasTriedFallbackMethod = false;
  2009. if (data && data.success) {
  2010. this.aiText = data.ai_text || "";
  2011. this.transcribedText = data.transcribed_text ? data.transcribed_text.text : "";
  2012. this.conversationHistory.push({
  2013. role: "user",
  2014. content: this.transcribedText
  2015. });
  2016. this.conversationHistory.push({
  2017. role: "assistant",
  2018. content: this.aiText
  2019. });
  2020. const aiVoiceUrl = data.ai_voice_url || "";
  2021. if (aiVoiceUrl) {
  2022. this.aiVoiceUrl = aiVoiceUrl.startsWith("http") ? aiVoiceUrl : `${common_config.apiBaseUrl}${aiVoiceUrl}`;
  2023. console.log("AI语音URL:", this.aiVoiceUrl);
  2024. this.currentSubtitle = this.aiText;
  2025. const systemInfo2 = common_vendor.index.getSystemInfoSync();
  2026. const isMiniProgram = systemInfo2.uniPlatform && systemInfo2.uniPlatform.startsWith("mp-");
  2027. const isUnsupportedFormat = this.checkUnsupportedAudioFormat(this.aiVoiceUrl);
  2028. if (isUnsupportedFormat && isMiniProgram) {
  2029. console.warn("检测到小程序不支持的音频格式,直接使用文本显示");
  2030. this.displayTextResponse();
  2031. return;
  2032. }
  2033. if (isMiniProgram) {
  2034. if (isIOS) {
  2035. this.playIOSMiniProgramAudio();
  2036. } else {
  2037. this.playMiniProgramAudio();
  2038. }
  2039. } else {
  2040. this.playAiVoice();
  2041. }
  2042. this.showContinueQuestionButton = false;
  2043. this.showEndInterviewButton = false;
  2044. } else {
  2045. console.warn("未获取到AI语音URL,使用文本显示");
  2046. this.displayTextResponse();
  2047. }
  2048. } else {
  2049. console.error("AI语音交互失败:", data);
  2050. this.showContinueQuestionOptions();
  2051. this.checkPendingNavigation();
  2052. }
  2053. },
  2054. // 修改 handleAiInteractionError 方法,不要直接跳转,而是显示继续提问选项
  2055. handleAiInteractionError(err) {
  2056. console.error("AI语音交互请求失败:", err);
  2057. common_vendor.index.hideLoading();
  2058. common_vendor.index.showToast({
  2059. title: "AI处理失败,请重试",
  2060. icon: "none",
  2061. duration: 2e3
  2062. });
  2063. this.showContinueQuestionOptions();
  2064. },
  2065. // 添加新方法:播放AI语音
  2066. playAiVoice() {
  2067. console.log("开始播放AI语音:", this.aiVoiceUrl);
  2068. this.hasShownTextFallbackToast = false;
  2069. this.isPlayingAiVoice = true;
  2070. this.showContinueQuestionButton = false;
  2071. this.showEndInterviewButton = false;
  2072. this.currentSubtitle = this.aiText;
  2073. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === "") {
  2074. console.error("无效的音频URL,直接显示文本");
  2075. this.useTextFallbackAndNavigate();
  2076. return;
  2077. }
  2078. const systemInfo = common_vendor.index.getSystemInfoSync();
  2079. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  2080. const isIOS = systemInfo.platform === "ios";
  2081. if (isMiniProgram) {
  2082. if (isIOS) {
  2083. this.playIOSMiniProgramAudio();
  2084. } else {
  2085. this.playMiniProgramAudio();
  2086. }
  2087. } else if (isIOS) {
  2088. this.playAiVoiceForIOS();
  2089. } else {
  2090. this.playAiVoiceForAndroid();
  2091. }
  2092. },
  2093. // 新增方法:文本回退并导航
  2094. useTextFallbackAndNavigate() {
  2095. console.log("使用文本回退并准备导航");
  2096. this.isPlayingAiVoice = false;
  2097. if (!this.hasShownTextFallbackToast) {
  2098. this.hasShownTextFallbackToast = true;
  2099. common_vendor.index.showToast({
  2100. title: "为您显示文字回复",
  2101. icon: "none",
  2102. duration: 2e3
  2103. });
  2104. }
  2105. this.currentSubtitle = this.aiText || "抱歉,AI回复内容加载失败";
  2106. setTimeout(() => {
  2107. this.navigateToNextPage();
  2108. }, 5e3);
  2109. },
  2110. // 修改 handleIOSAudioPlaybackComplete 方法
  2111. handleIOSAudioPlaybackComplete() {
  2112. this.isPlayingAiVoice = false;
  2113. setTimeout(() => {
  2114. this.currentSubtitle = "";
  2115. }, 1e3);
  2116. if (this.aiAudioPlayer) {
  2117. try {
  2118. this.aiAudioPlayer.destroy();
  2119. } catch (e) {
  2120. console.error("销毁iOS音频播放器失败:", e);
  2121. }
  2122. this.aiAudioPlayer = null;
  2123. }
  2124. this.navigateToNextPage();
  2125. },
  2126. // 修改 playMiniProgramAudio 方法
  2127. playMiniProgramAudio() {
  2128. console.log("使用小程序原生音频API播放:", this.aiVoiceUrl);
  2129. try {
  2130. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2131. audioContext.autoplay = true;
  2132. audioContext.src = this.aiVoiceUrl;
  2133. audioContext.onPlay(() => {
  2134. console.log("AI语音开始播放");
  2135. this.currentSubtitle = this.aiText;
  2136. });
  2137. audioContext.onEnded(() => {
  2138. console.log("AI语音播放结束");
  2139. this.isPlayingAiVoice = false;
  2140. this.currentSubtitle = "";
  2141. audioContext.destroy();
  2142. this.navigateToNextPage();
  2143. });
  2144. audioContext.onError((err) => {
  2145. console.error("AI语音播放错误:", err);
  2146. audioContext.destroy();
  2147. this.useTextFallbackAndNavigate();
  2148. });
  2149. setTimeout(() => {
  2150. if (this.isPlayingAiVoice) {
  2151. console.warn("音频播放超时,使用文本回退");
  2152. this.useTextFallbackAndNavigate();
  2153. }
  2154. }, 5e3);
  2155. } catch (e) {
  2156. console.error("小程序播放AI语音失败:", e);
  2157. this.useTextFallbackAndNavigate();
  2158. }
  2159. },
  2160. // 新增方法:导航到下一页
  2161. navigateToNextPage() {
  2162. console.log("准备导航到下一页");
  2163. common_vendor.index.navigateTo({
  2164. url: "/pages/posture-guide/posture-guide",
  2165. fail: (err) => {
  2166. console.error("导航失败:", err);
  2167. common_vendor.index.showToast({
  2168. title: "页面跳转失败",
  2169. icon: "none",
  2170. duration: 2e3
  2171. });
  2172. }
  2173. });
  2174. },
  2175. // 新增方法:专门处理iOS小程序音频播放
  2176. playIOSMiniProgramAudio() {
  2177. console.log("iOS小程序环境:使用增强的音频处理方法");
  2178. this.hasShownTextFallbackToast = false;
  2179. try {
  2180. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2181. audioContext.autoplay = false;
  2182. audioContext.obeyMuteSwitch = true;
  2183. audioContext.volume = 1;
  2184. audioContext.onPlay(() => {
  2185. console.log("iOS小程序音频开始播放");
  2186. this.currentSubtitle = this.aiText;
  2187. });
  2188. audioContext.onEnded(() => {
  2189. console.log("iOS小程序音频播放结束");
  2190. this.handleIOSAudioComplete(audioContext);
  2191. });
  2192. audioContext.onStop(() => {
  2193. console.log("iOS小程序音频播放停止");
  2194. this.handleIOSAudioComplete(audioContext);
  2195. });
  2196. audioContext.onError((err) => {
  2197. console.error("iOS小程序音频播放错误:", err);
  2198. this.handleIOSMiniProgramError(audioContext, err);
  2199. });
  2200. this.preloadIOSMiniProgramAudio(this.aiVoiceUrl).then((localPath) => {
  2201. console.log("iOS小程序音频预下载成功:", localPath);
  2202. audioContext.src = localPath;
  2203. audioContext.play();
  2204. }).catch((error) => {
  2205. console.error("iOS小程序音频预下载失败:", error);
  2206. this.fallbackIOSMiniProgramPlay(audioContext);
  2207. });
  2208. } catch (e) {
  2209. console.error("iOS小程序音频初始化失败:", e);
  2210. this.useIOSTextFallback();
  2211. }
  2212. },
  2213. // 新增方法:iOS小程序音频预下载
  2214. preloadIOSMiniProgramAudio(audioUrl) {
  2215. return new Promise((resolve, reject) => {
  2216. console.log("开始预下载iOS小程序音频:", audioUrl);
  2217. common_vendor.index.downloadFile({
  2218. url: audioUrl,
  2219. timeout: 15e3,
  2220. // 15秒超时
  2221. success: (res) => {
  2222. if (res.statusCode === 200) {
  2223. console.log("iOS小程序音频下载成功:", res.tempFilePath);
  2224. resolve(res.tempFilePath);
  2225. } else {
  2226. console.error("iOS小程序音频下载失败,状态码:", res.statusCode);
  2227. reject(new Error(`下载失败,状态码: ${res.statusCode}`));
  2228. }
  2229. },
  2230. fail: (err) => {
  2231. console.error("iOS小程序音频下载请求失败:", err);
  2232. reject(err);
  2233. }
  2234. });
  2235. });
  2236. },
  2237. // 新增方法:iOS小程序错误处理
  2238. handleIOSMiniProgramError(audioContext, error) {
  2239. console.error("处理iOS小程序音频错误:", error);
  2240. if (audioContext) {
  2241. try {
  2242. audioContext.destroy();
  2243. } catch (e) {
  2244. console.error("销毁音频播放器失败:", e);
  2245. }
  2246. }
  2247. let errorMessage = "音频播放异常,显示文字";
  2248. if (error.errMsg && error.errMsg.includes("-11850")) {
  2249. console.log("iOS小程序检测到系统音频错误(-11850)");
  2250. errorMessage = "系统音频服务暂停,显示文字";
  2251. } else if (error.errCode === 10002) {
  2252. console.log("iOS小程序检测到errCode:10002,网络或资源问题");
  2253. errorMessage = "音频加载中断,显示文字";
  2254. } else if (error.errCode === 10001) {
  2255. console.log("iOS小程序检测到errCode:10001,系统错误");
  2256. errorMessage = "系统音频错误,显示文字";
  2257. } else if (error.errCode === 10003) {
  2258. console.log("iOS小程序检测到errCode:10003,文件错误");
  2259. errorMessage = "音频文件错误,显示文字";
  2260. } else if (error.errCode === 10004) {
  2261. console.log("iOS小程序检测到errCode:10004,格式不支持");
  2262. errorMessage = "音频格式不支持,显示文字";
  2263. }
  2264. common_vendor.index.showToast({
  2265. title: errorMessage,
  2266. icon: "none",
  2267. duration: 2e3
  2268. });
  2269. this.useIOSTextFallback();
  2270. },
  2271. // 新增方法:iOS小程序播放超时处理
  2272. handleIOSPlaybackTimeout(audioContext) {
  2273. console.log("iOS小程序音频播放超时处理");
  2274. if (audioContext) {
  2275. try {
  2276. audioContext.destroy();
  2277. } catch (e) {
  2278. console.error("销毁超时音频播放器失败:", e);
  2279. }
  2280. }
  2281. this.useIOSTextFallback();
  2282. },
  2283. // 新增方法:iOS小程序备选播放方法
  2284. fallbackIOSMiniProgramPlay(audioContext) {
  2285. console.log("iOS小程序使用备选播放方法");
  2286. try {
  2287. audioContext.src = this.aiVoiceUrl;
  2288. const fallbackTimeout = setTimeout(() => {
  2289. if (this.isPlayingAiVoice) {
  2290. console.log("iOS小程序备选方法也超时");
  2291. clearTimeout(fallbackTimeout);
  2292. this.handleIOSPlaybackTimeout(audioContext);
  2293. }
  2294. }, 3e3);
  2295. audioContext.play();
  2296. } catch (e) {
  2297. console.error("iOS小程序备选播放方法失败:", e);
  2298. this.handleIOSPlaybackTimeout(audioContext);
  2299. }
  2300. },
  2301. // 新增方法:统一的iOS音频完成处理
  2302. handleIOSAudioComplete(audioContext) {
  2303. console.log("iOS音频播放完成");
  2304. this.isPlayingAiVoice = false;
  2305. setTimeout(() => {
  2306. this.currentSubtitle = "";
  2307. }, 2e3);
  2308. if (audioContext) {
  2309. try {
  2310. audioContext.destroy();
  2311. } catch (e) {
  2312. console.error("销毁iOS音频播放器失败:", e);
  2313. }
  2314. }
  2315. this.showContinueQuestionOptions();
  2316. this.checkPendingNavigation();
  2317. },
  2318. // 移除原来的 playMiniProgramAudioForIOS 方法,因为已经被新的 playIOSMiniProgramAudio 替代
  2319. // playMiniProgramAudioForIOS() 方法可以删除或注释掉
  2320. // 保留原有的 useIOSTextFallback 方法,但确保它能正确处理所有iOS场景
  2321. useIOSTextFallback() {
  2322. console.log("iOS使用文本回退方案");
  2323. this.isPlayingAiVoice = false;
  2324. if (!this.hasShownTextFallbackToast) {
  2325. this.hasShownTextFallbackToast = true;
  2326. }
  2327. this.currentSubtitle = this.aiText || "抱歉,AI回复内容加载失败";
  2328. const textLength = this.aiText ? this.aiText.length : 20;
  2329. const readingTime = Math.max(5e3, Math.min(textLength * 100, 15e3));
  2330. console.log(`文本长度: ${textLength}, 预计阅读时间: ${readingTime}ms`);
  2331. setTimeout(() => {
  2332. this.handleIOSAudioComplete(null);
  2333. }, readingTime);
  2334. },
  2335. // 添加新方法:iOS音频播放完成处理
  2336. handleIOSAudioPlaybackComplete() {
  2337. this.isPlayingAiVoice = false;
  2338. setTimeout(() => {
  2339. this.currentSubtitle = "";
  2340. }, 2e3);
  2341. if (this.aiAudioPlayer) {
  2342. try {
  2343. this.aiAudioPlayer.destroy();
  2344. } catch (e) {
  2345. console.error("销毁iOS音频播放器失败:", e);
  2346. }
  2347. this.aiAudioPlayer = null;
  2348. }
  2349. this.showContinueQuestionOptions();
  2350. this.checkPendingNavigation();
  2351. },
  2352. // 添加新方法:iOS音频错误处理
  2353. handleIOSAudioError(error) {
  2354. console.error("iOS音频播放错误:", error);
  2355. switch (error.errCode) {
  2356. case 10001:
  2357. console.log("iOS音频系统错误,尝试重新播放");
  2358. this.retryIOSAudioPlay();
  2359. break;
  2360. case 10002:
  2361. console.log("iOS音频网络错误,使用备选方案");
  2362. this.handleIOS10002Error();
  2363. break;
  2364. case 10003:
  2365. console.log("iOS音频文件错误,使用文本回退");
  2366. this.useIOSTextFallback();
  2367. break;
  2368. case 10004:
  2369. console.log("iOS音频格式不支持,使用文本回退");
  2370. this.useIOSTextFallback();
  2371. break;
  2372. default:
  2373. console.log("iOS音频未知错误,使用文本回退");
  2374. this.useIOSTextFallback();
  2375. break;
  2376. }
  2377. },
  2378. // 添加新方法:重试iOS音频播放
  2379. retryIOSAudioPlay() {
  2380. console.log("重试iOS音频播放");
  2381. if (this.aiAudioPlayer) {
  2382. try {
  2383. this.aiAudioPlayer.destroy();
  2384. } catch (e) {
  2385. console.error("销毁播放器失败:", e);
  2386. }
  2387. this.aiAudioPlayer = null;
  2388. }
  2389. setTimeout(() => {
  2390. this.fallbackIOSAudioPlay();
  2391. }, 1e3);
  2392. },
  2393. // 添加新方法:iOS备选音频播放
  2394. fallbackIOSAudioPlay() {
  2395. console.log("iOS备选音频播放方案");
  2396. try {
  2397. this.aiAudioPlayer = common_vendor.index.createInnerAudioContext();
  2398. this.aiAudioPlayer.autoplay = true;
  2399. this.aiAudioPlayer.obeyMuteSwitch = true;
  2400. this.aiAudioPlayer.volume = 0.8;
  2401. this.aiAudioPlayer.src = this.aiVoiceUrl;
  2402. this.aiAudioPlayer.onPlay(() => {
  2403. console.log("iOS备选方案:音频开始播放");
  2404. this.currentSubtitle = this.aiText;
  2405. });
  2406. this.aiAudioPlayer.onEnded(() => {
  2407. console.log("iOS备选方案:音频播放结束");
  2408. this.handleIOSAudioPlaybackComplete();
  2409. });
  2410. this.aiAudioPlayer.onError((err) => {
  2411. console.error("iOS备选方案:音频播放错误:", err);
  2412. this.useIOSTextFallback();
  2413. });
  2414. setTimeout(() => {
  2415. if (this.isPlayingAiVoice && this.aiAudioPlayer) {
  2416. console.log("iOS音频播放超时,使用文本回退");
  2417. this.useIOSTextFallback();
  2418. }
  2419. }, 5e3);
  2420. } catch (e) {
  2421. console.error("iOS备选音频播放失败:", e);
  2422. this.useIOSTextFallback();
  2423. }
  2424. },
  2425. // 修改 playMiniProgramAudio 方法,添加iOS特殊处理
  2426. playMiniProgramAudio() {
  2427. console.log("使用小程序原生音频API播放:", this.aiVoiceUrl);
  2428. this.isPlayingAiVoice = true;
  2429. this.showContinueQuestionButton = false;
  2430. this.showEndInterviewButton = false;
  2431. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === "") {
  2432. console.error("无效的音频URL");
  2433. this.handleAudioPlaybackFailure();
  2434. return;
  2435. }
  2436. const systemInfo = common_vendor.index.getSystemInfoSync();
  2437. const isIOS = systemInfo.platform === "ios";
  2438. if (isIOS) {
  2439. this.playMiniProgramAudioForIOS();
  2440. return;
  2441. }
  2442. try {
  2443. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2444. audioContext.autoplay = false;
  2445. audioContext.obeyMuteSwitch = false;
  2446. audioContext.onPlay(() => {
  2447. console.log("AI语音开始播放");
  2448. });
  2449. audioContext.onEnded(() => {
  2450. console.log("AI语音播放结束");
  2451. this.isPlayingAiVoice = false;
  2452. this.currentSubtitle = "";
  2453. audioContext.destroy();
  2454. this.showContinueQuestionOptions();
  2455. });
  2456. audioContext.onError((err) => {
  2457. console.error("AI语音播放错误:", err);
  2458. this.isPlayingAiVoice = false;
  2459. this.currentSubtitle = "";
  2460. audioContext.destroy();
  2461. this.showContinueQuestionOptions();
  2462. });
  2463. audioContext.src = this.aiVoiceUrl;
  2464. const playTimeout = setTimeout(() => {
  2465. if (this.isPlayingAiVoice) {
  2466. console.warn("小程序音频播放超时,尝试备选方法");
  2467. this.tryMiniProgramFallbackAudioPlay();
  2468. }
  2469. }, 5e3);
  2470. setTimeout(() => {
  2471. audioContext.play();
  2472. clearTimeout(playTimeout);
  2473. }, 300);
  2474. } catch (e) {
  2475. console.error("小程序播放AI语音时发生错误:", e);
  2476. this.isPlayingAiVoice = false;
  2477. this.handleAudioPlaybackFailure();
  2478. }
  2479. },
  2480. // 添加新方法:iOS小程序音频播放
  2481. playMiniProgramAudioForIOS() {
  2482. console.log("iOS小程序环境:开始播放AI语音");
  2483. this.hasShownTextFallbackToast = false;
  2484. try {
  2485. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2486. audioContext.autoplay = false;
  2487. audioContext.obeyMuteSwitch = true;
  2488. audioContext.volume = 1;
  2489. audioContext.onError((err) => {
  2490. console.error("iOS小程序音频播放错误:", err);
  2491. if (err.errMsg && err.errMsg.includes("-11850")) {
  2492. console.log("iOS小程序检测到系统音频错误(-11850)");
  2493. } else if (err.errCode === 10002) {
  2494. console.log("iOS小程序检测到errCode:10002,网络或资源问题");
  2495. } else {
  2496. console.log("iOS小程序其他音频错误:", err.errCode);
  2497. }
  2498. this.useIOSMiniProgramTextFallback(audioContext);
  2499. });
  2500. audioContext.src = this.aiVoiceUrl;
  2501. audioContext.onPlay(() => {
  2502. console.log("iOS小程序音频开始播放");
  2503. this.currentSubtitle = this.aiText;
  2504. });
  2505. audioContext.onEnded(() => {
  2506. console.log("iOS小程序音频播放结束");
  2507. this.handleIOSMiniProgramAudioComplete(audioContext);
  2508. });
  2509. const playTimeout = setTimeout(() => {
  2510. if (this.isPlayingAiVoice) {
  2511. console.log("iOS小程序音频播放超时,切换到文本显示");
  2512. clearTimeout(playTimeout);
  2513. this.useIOSMiniProgramTextFallback(audioContext);
  2514. }
  2515. }, 3e3);
  2516. audioContext.play();
  2517. } catch (e) {
  2518. console.error("iOS小程序音频播放初始化失败:", e);
  2519. this.handleAudioPlaybackFailure();
  2520. }
  2521. },
  2522. // 添加新方法:iOS小程序音频播放完成
  2523. handleIOSMiniProgramAudioComplete(audioContext) {
  2524. this.isPlayingAiVoice = false;
  2525. setTimeout(() => {
  2526. this.currentSubtitle = "";
  2527. }, 2e3);
  2528. if (audioContext) {
  2529. try {
  2530. audioContext.destroy();
  2531. } catch (e) {
  2532. console.error("销毁iOS小程序音频播放器失败:", e);
  2533. }
  2534. }
  2535. this.showContinueQuestionOptions();
  2536. this.checkPendingNavigation();
  2537. },
  2538. // 添加新方法:iOS小程序音频错误处理
  2539. handleIOSMiniProgramAudioError(audioContext, error) {
  2540. console.error("iOS小程序音频错误:", error);
  2541. if (error.errCode === 10002) {
  2542. this.useIOSMiniProgramTextFallback(audioContext);
  2543. } else {
  2544. this.useIOSMiniProgramTextFallback(audioContext);
  2545. }
  2546. },
  2547. // 添加新方法:iOS小程序文本回退
  2548. useIOSMiniProgramTextFallback(audioContext) {
  2549. console.log("iOS小程序使用文本回退方案");
  2550. if (audioContext) {
  2551. try {
  2552. audioContext.destroy();
  2553. } catch (e) {
  2554. console.error("销毁音频播放器失败:", e);
  2555. }
  2556. }
  2557. this.isPlayingAiVoice = false;
  2558. this.currentSubtitle = this.aiText || "抱歉,AI回复内容加载失败";
  2559. const textLength = this.aiText ? this.aiText.length : 20;
  2560. const readingTime = Math.max(6e3, Math.min(textLength * 120, 18e3));
  2561. setTimeout(() => {
  2562. this.handleIOSMiniProgramAudioComplete(null);
  2563. }, readingTime);
  2564. },
  2565. // 添加新方法:处理音频播放错误
  2566. handleAudioPlaybackError(error) {
  2567. console.error("音频播放错误:", error);
  2568. if (!this.hasTriedFallbackMethod) {
  2569. this.hasTriedFallbackMethod = true;
  2570. this.tryFallbackAudioPlay();
  2571. return;
  2572. }
  2573. this.handleAudioPlaybackFailure();
  2574. },
  2575. // 添加新方法:尝试备选音频播放方法
  2576. tryFallbackAudioPlay() {
  2577. console.log("使用备选方法播放音频:", this.aiVoiceUrl);
  2578. if (this.aiAudioPlayer) {
  2579. try {
  2580. this.aiAudioPlayer.destroy();
  2581. } catch (e) {
  2582. console.error("销毁音频播放器失败:", e);
  2583. }
  2584. this.aiAudioPlayer = null;
  2585. }
  2586. common_vendor.index.downloadFile({
  2587. url: this.aiVoiceUrl,
  2588. success: (res) => {
  2589. if (res.statusCode === 200) {
  2590. console.log("音频文件下载成功:", res.tempFilePath);
  2591. const newAudioPlayer = common_vendor.index.createInnerAudioContext();
  2592. this.aiAudioPlayer = newAudioPlayer;
  2593. newAudioPlayer.autoplay = true;
  2594. newAudioPlayer.src = res.tempFilePath;
  2595. newAudioPlayer.onPlay(() => {
  2596. console.log("备选方法:音频开始播放");
  2597. });
  2598. newAudioPlayer.onEnded(() => {
  2599. console.log("备选方法:音频播放结束");
  2600. this.isPlayingAiVoice = false;
  2601. this.currentSubtitle = "";
  2602. newAudioPlayer.destroy();
  2603. this.aiAudioPlayer = null;
  2604. this.showContinueQuestionOptions();
  2605. this.checkPendingNavigation();
  2606. });
  2607. newAudioPlayer.onError((err) => {
  2608. console.error("备选方法:音频播放错误:", err);
  2609. this.isPlayingAiVoice = false;
  2610. this.currentSubtitle = "";
  2611. newAudioPlayer.destroy();
  2612. this.aiAudioPlayer = null;
  2613. this.showContinueQuestionOptions();
  2614. this.checkPendingNavigation();
  2615. });
  2616. } else {
  2617. console.error("音频文件下载失败:", res);
  2618. this.handleAudioPlaybackFailure();
  2619. }
  2620. },
  2621. fail: (err) => {
  2622. console.error("音频文件下载请求失败:", err);
  2623. this.handleAudioPlaybackFailure();
  2624. }
  2625. });
  2626. },
  2627. // 添加新方法:处理音频播放失败的情况
  2628. handleAudioPlaybackFailure() {
  2629. this.isPlayingAiVoice = false;
  2630. this.currentSubtitle = this.aiText;
  2631. setTimeout(() => {
  2632. this.currentSubtitle = "";
  2633. this.showContinueQuestionOptions();
  2634. this.checkPendingNavigation();
  2635. }, 1e4);
  2636. },
  2637. // 添加新方法:从视频中提取音频
  2638. async extractAudioFromVideo(videoBlob) {
  2639. console.log("开始从视频中提取音频");
  2640. return new Promise((resolve, reject) => {
  2641. try {
  2642. const videoElement = document.createElement("video");
  2643. videoElement.muted = false;
  2644. videoElement.autoplay = false;
  2645. const audioContext = new (window.AudioContext || window.webkitAudioContext)();
  2646. let audioDestination = null;
  2647. let mediaRecorder = null;
  2648. const audioChunks = [];
  2649. videoElement.src = URL.createObjectURL(videoBlob);
  2650. videoElement.onloadedmetadata = () => {
  2651. console.log("视频元数据已加载,视频时长:", videoElement.duration);
  2652. const source = audioContext.createMediaElementSource(videoElement);
  2653. audioDestination = audioContext.createMediaStreamDestination();
  2654. source.connect(audioDestination);
  2655. mediaRecorder = new MediaRecorder(audioDestination.stream);
  2656. mediaRecorder.ondataavailable = (event) => {
  2657. if (event.data.size > 0) {
  2658. audioChunks.push(event.data);
  2659. }
  2660. };
  2661. mediaRecorder.onstop = () => {
  2662. const audioBlob = new Blob(audioChunks, { type: "audio/wav" });
  2663. console.log("音频提取完成,大小:", audioBlob.size);
  2664. resolve(audioBlob);
  2665. };
  2666. videoElement.onplay = () => {
  2667. mediaRecorder.start(100);
  2668. };
  2669. videoElement.onended = () => {
  2670. mediaRecorder.stop();
  2671. audioContext.close();
  2672. };
  2673. videoElement.play();
  2674. };
  2675. videoElement.onerror = (err) => {
  2676. console.error("视频加载失败:", err);
  2677. reject(new Error("视频加载失败"));
  2678. };
  2679. } catch (err) {
  2680. console.error("提取音频失败:", err);
  2681. reject(err);
  2682. }
  2683. });
  2684. },
  2685. // 添加新方法:上传音频文件
  2686. async uploadAudioFile(audioBlob, videoUrl) {
  2687. console.log("准备上传音频文件");
  2688. const formData = new FormData();
  2689. const fileName = `audio_${Date.now()}.wav`;
  2690. const audioFile = new File([audioBlob], fileName, { type: "audio/wav" });
  2691. formData.append("audio_file", audioFile);
  2692. formData.append("video_url", videoUrl);
  2693. formData.append("tenant_id", common_vendor.index.getStorageSync("tenant_id") || "1");
  2694. formData.append("openid", common_vendor.index.getStorageSync("userInfo") ? JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid : "");
  2695. formData.append("application_id", common_vendor.index.getStorageSync("appId") || "");
  2696. return new Promise((resolve, reject) => {
  2697. const xhr = new XMLHttpRequest();
  2698. xhr.open("POST", `${common_config.apiBaseUrl}/api/voice_ai_interaction`, true);
  2699. xhr.timeout = 6e4;
  2700. xhr.onload = () => {
  2701. if (xhr.status === 200) {
  2702. try {
  2703. const response = JSON.parse(xhr.responseText);
  2704. resolve(response);
  2705. } catch (e) {
  2706. reject(new Error("解析响应失败"));
  2707. }
  2708. } else {
  2709. reject(new Error("HTTP状态: " + xhr.status));
  2710. }
  2711. };
  2712. xhr.onerror = () => {
  2713. reject(new Error("网络错误"));
  2714. };
  2715. xhr.ontimeout = () => {
  2716. reject(new Error("请求超时"));
  2717. };
  2718. xhr.send(formData);
  2719. });
  2720. },
  2721. // 添加一个新方法:使用小程序原生音频API
  2722. playMiniProgramAudio() {
  2723. console.log("使用小程序原生音频API播放:", this.aiVoiceUrl);
  2724. this.isPlayingAiVoice = true;
  2725. this.showContinueQuestionButton = false;
  2726. this.showEndInterviewButton = false;
  2727. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === "") {
  2728. console.error("无效的音频URL");
  2729. this.isPlayingAiVoice = false;
  2730. this.showContinueQuestionOptions();
  2731. return;
  2732. }
  2733. try {
  2734. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2735. audioContext.autoplay = false;
  2736. audioContext.obeyMuteSwitch = false;
  2737. audioContext.onPlay(() => {
  2738. console.log("AI语音开始播放");
  2739. });
  2740. audioContext.onEnded(() => {
  2741. console.log("AI语音播放结束");
  2742. this.isPlayingAiVoice = false;
  2743. this.currentSubtitle = "";
  2744. audioContext.destroy();
  2745. this.showContinueQuestionOptions();
  2746. });
  2747. audioContext.onError((err) => {
  2748. console.error("AI语音播放错误:", err);
  2749. this.isPlayingAiVoice = false;
  2750. this.currentSubtitle = "";
  2751. audioContext.destroy();
  2752. this.showContinueQuestionOptions();
  2753. });
  2754. audioContext.src = this.aiVoiceUrl;
  2755. const playTimeout = setTimeout(() => {
  2756. if (this.isPlayingAiVoice) {
  2757. console.warn("小程序音频播放超时,尝试备选方法");
  2758. this.tryMiniProgramFallbackAudioPlay();
  2759. }
  2760. }, 5e3);
  2761. setTimeout(() => {
  2762. audioContext.play();
  2763. clearTimeout(playTimeout);
  2764. }, 300);
  2765. } catch (e) {
  2766. console.error("小程序播放AI语音时发生错误:", e);
  2767. this.isPlayingAiVoice = false;
  2768. this.handleAudioPlaybackFailure();
  2769. }
  2770. },
  2771. // 添加新方法:小程序环境的备选音频播放方法
  2772. tryMiniProgramFallbackAudioPlay() {
  2773. console.log("使用小程序备选方法播放音频:", this.aiVoiceUrl);
  2774. common_vendor.index.downloadFile({
  2775. url: this.aiVoiceUrl,
  2776. success: (res) => {
  2777. if (res.statusCode === 200) {
  2778. console.log("小程序音频文件下载成功:", res.tempFilePath);
  2779. const newAudioContext = common_vendor.wx$1.createInnerAudioContext();
  2780. newAudioContext.autoplay = true;
  2781. newAudioContext.src = res.tempFilePath;
  2782. newAudioContext.onPlay(() => {
  2783. console.log("小程序备选方法:音频开始播放");
  2784. });
  2785. newAudioContext.onEnded(() => {
  2786. console.log("小程序备选方法:音频播放结束");
  2787. this.isPlayingAiVoice = false;
  2788. this.currentSubtitle = "";
  2789. newAudioContext.destroy();
  2790. this.showContinueQuestionOptions();
  2791. this.checkPendingNavigation();
  2792. });
  2793. newAudioContext.onError((err) => {
  2794. console.error("小程序备选方法:音频播放错误:", err);
  2795. this.isPlayingAiVoice = false;
  2796. this.currentSubtitle = "";
  2797. newAudioContext.destroy();
  2798. this.trySystemAudioPlayer();
  2799. });
  2800. } else {
  2801. console.error("小程序音频文件下载失败:", res);
  2802. this.handleAudioPlaybackFailure();
  2803. }
  2804. },
  2805. fail: (err) => {
  2806. console.error("小程序音频文件下载请求失败:", err);
  2807. this.handleAudioPlaybackFailure();
  2808. }
  2809. });
  2810. },
  2811. // 添加新方法:尝试使用系统播放器
  2812. trySystemAudioPlayer() {
  2813. console.log("尝试使用系统播放器播放音频");
  2814. const systemInfo = common_vendor.index.getSystemInfoSync();
  2815. if (systemInfo.platform === "android" || systemInfo.platform === "ios") {
  2816. if (typeof common_vendor.index.playVoice === "function") {
  2817. common_vendor.index.playVoice({
  2818. filePath: this.aiVoiceUrl,
  2819. success: () => {
  2820. console.log("系统播放器播放成功");
  2821. setTimeout(() => {
  2822. this.isPlayingAiVoice = false;
  2823. this.currentSubtitle = "";
  2824. this.showContinueQuestionOptions();
  2825. }, 5e3);
  2826. },
  2827. fail: (err) => {
  2828. console.error("系统播放器播放失败:", err);
  2829. this.handleAudioPlaybackFailure();
  2830. }
  2831. });
  2832. } else {
  2833. common_vendor.index.openDocument({
  2834. filePath: this.aiVoiceUrl,
  2835. fileType: "audio",
  2836. success: () => {
  2837. console.log("使用系统应用打开音频文件");
  2838. setTimeout(() => {
  2839. this.isPlayingAiVoice = false;
  2840. this.currentSubtitle = "";
  2841. this.showContinueQuestionOptions();
  2842. }, 8e3);
  2843. },
  2844. fail: (err) => {
  2845. console.error("无法使用系统应用打开音频:", err);
  2846. this.handleAudioPlaybackFailure();
  2847. }
  2848. });
  2849. }
  2850. } else {
  2851. this.handleAudioPlaybackFailure();
  2852. }
  2853. },
  2854. // 修改 showContinueQuestionOptions 方法,确保状态正确设置
  2855. showContinueQuestionOptions() {
  2856. if (this.isPlayingAiVoice) {
  2857. console.log("AI语音正在播放,暂不显示继续提问选项");
  2858. return;
  2859. }
  2860. this.questionRound++;
  2861. this.currentSubtitle = "";
  2862. console.log("准备跳转到下一页面");
  2863. setTimeout(() => {
  2864. this.navigateToNextPage();
  2865. }, 3e3);
  2866. this.isRecording = false;
  2867. if (this.recordingTimer) {
  2868. clearInterval(this.recordingTimer);
  2869. this.recordingTimer = null;
  2870. }
  2871. this.recordedChunks = [];
  2872. this.recordingTimerCount = 0;
  2873. this.remainingTime = this.maxRecordingTime;
  2874. },
  2875. // 修复 handleContinueQuestionClick 方法,确保状态正确重置
  2876. handleContinueQuestionClick() {
  2877. if (this.isPlayingAiVoice) {
  2878. return;
  2879. }
  2880. this.showContinueQuestionButton = false;
  2881. this.showEndInterviewButton = false;
  2882. this.isRecording = false;
  2883. this.recordingTimerCount = 0;
  2884. this.remainingTime = this.maxRecordingTime;
  2885. this.recordingTimeDisplay = "00:00 / " + this.formatTime(this.maxRecordingTime);
  2886. this.recordedChunks = [];
  2887. if (this.mediaRecorder && this.mediaRecorder.state === "recording") {
  2888. this.mediaRecorder.stop();
  2889. }
  2890. if (this.recorder) {
  2891. try {
  2892. this.cameraContext.stopRecord();
  2893. } catch (e) {
  2894. console.error("停止录制失败:", e);
  2895. }
  2896. this.recorder = null;
  2897. }
  2898. setTimeout(() => {
  2899. this.startRecordingAnswer();
  2900. }, 500);
  2901. },
  2902. // 添加新方法:处理结束面试按钮点击
  2903. handleEndInterviewClick() {
  2904. this.showContinueQuestionButton = false;
  2905. this.showEndInterviewButton = false;
  2906. this.navigateToNextPage();
  2907. },
  2908. // 添加新方法:处理录制错误
  2909. handleRecordingError(error) {
  2910. console.error("录制错误:", error);
  2911. this.isRecording = false;
  2912. this.showStopRecordingButton = false;
  2913. this.showStartRecordingButton = true;
  2914. },
  2915. // 添加新方法:处理上传错误
  2916. handleUploadError(error) {
  2917. console.error("上传错误:", error);
  2918. this.showRetryButton = true;
  2919. this.showUploadStatus = false;
  2920. this.isUploading = false;
  2921. },
  2922. // 添加新方法:检查是否有待处理的自动跳转
  2923. checkPendingNavigation() {
  2924. if (this.pendingAutoNavigation && !this.isPlayingAiVoice && !this.isUploading && !this.currentSubtitle) {
  2925. console.log("所有处理已完成,准备跳转");
  2926. this.pendingAutoNavigation = false;
  2927. setTimeout(() => {
  2928. this.navigateToNextPage();
  2929. }, 1e3);
  2930. }
  2931. },
  2932. // 添加新方法:显示文本回复
  2933. displayTextResponse() {
  2934. this.isPlayingAiVoice = true;
  2935. this.currentSubtitle = "";
  2936. const charDisplayTime = 200;
  2937. let currentIndex = 0;
  2938. const textInterval = setInterval(() => {
  2939. if (currentIndex <= this.aiText.length) {
  2940. this.currentSubtitle = this.aiText.substring(0, currentIndex);
  2941. currentIndex++;
  2942. } else {
  2943. clearInterval(textInterval);
  2944. setTimeout(() => {
  2945. this.isPlayingAiVoice = false;
  2946. this.currentSubtitle = "";
  2947. this.showContinueQuestionOptions();
  2948. this.checkPendingNavigation();
  2949. }, 3e3);
  2950. }
  2951. }, charDisplayTime);
  2952. },
  2953. // 添加新方法:检查不支持的音频格式
  2954. checkUnsupportedAudioFormat(url) {
  2955. if (!url)
  2956. return false;
  2957. const extension = url.split(".").pop().toLowerCase();
  2958. const systemInfo = common_vendor.index.getSystemInfoSync();
  2959. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  2960. if (isMiniProgram) {
  2961. const unsupportedFormats = ["ogg", "flac", "aac", "m4a", "opus"];
  2962. return unsupportedFormats.includes(extension);
  2963. }
  2964. return false;
  2965. },
  2966. // 修改文本回退方法
  2967. useIOSTextFallback() {
  2968. console.log("切换到文本显示模式");
  2969. this.isPlayingAiVoice = false;
  2970. if (!this.hasShownTextFallbackToast) {
  2971. this.hasShownTextFallbackToast = true;
  2972. }
  2973. this.currentSubtitle = this.aiText || "抱歉,无法获取AI回复内容";
  2974. const textLength = this.aiText ? this.aiText.length : 20;
  2975. const readingTime = Math.max(8e3, Math.min(textLength * 150, 2e4));
  2976. console.log(`文本长度: ${textLength}字,预计阅读时间: ${readingTime / 1e3}秒`);
  2977. setTimeout(() => {
  2978. this.navigateToNextPage();
  2979. }, readingTime);
  2980. }
  2981. }
  2982. };
  2983. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  2984. return common_vendor.e({
  2985. a: $data.videoUrl,
  2986. b: common_vendor.o((...args) => $options.handleVideoError && $options.handleVideoError(...args)),
  2987. c: common_vendor.o((...args) => $options.handleVideoEnded && $options.handleVideoEnded(...args)),
  2988. d: common_vendor.o((...args) => $options.handleTimeUpdate && $options.handleTimeUpdate(...args)),
  2989. e: $data.showAnswerButton
  2990. }, $data.showAnswerButton ? {
  2991. f: common_vendor.o((...args) => $options.handleAnswerButtonClick && $options.handleAnswerButtonClick(...args))
  2992. } : {}, {
  2993. g: $data.currentSubtitle
  2994. }, $data.currentSubtitle ? {
  2995. h: common_vendor.t($data.currentSubtitle)
  2996. } : {}, {
  2997. i: $data.useMiniProgramCameraComponent
  2998. }, $data.useMiniProgramCameraComponent ? {
  2999. j: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  3000. } : {}, {
  3001. k: $data.loading
  3002. }, $data.loading ? {} : {}, {
  3003. l: $data.showDebugInfo
  3004. }, $data.showDebugInfo ? common_vendor.e({
  3005. m: $data.assistantResponse
  3006. }, $data.assistantResponse ? {
  3007. n: common_vendor.t($data.assistantResponse)
  3008. } : {}, {
  3009. o: $data.audioTranscript
  3010. }, $data.audioTranscript ? {
  3011. p: common_vendor.t($data.audioTranscript)
  3012. } : {}, {
  3013. q: common_vendor.f($data.processedResponses, (item, index, i0) => {
  3014. return common_vendor.e({
  3015. a: item.role
  3016. }, item.role ? {
  3017. b: common_vendor.t(item.role)
  3018. } : {}, {
  3019. c: item.transcript
  3020. }, item.transcript ? {
  3021. d: common_vendor.t(item.transcript)
  3022. } : {}, {
  3023. e: index
  3024. });
  3025. })
  3026. }) : {}, {
  3027. r: $data.showStopRecordingButton
  3028. }, $data.showStopRecordingButton ? {
  3029. s: common_vendor.o((...args) => $options.stopRecordingAnswer && $options.stopRecordingAnswer(...args))
  3030. } : {}, {
  3031. t: $data.isRecording
  3032. }, $data.isRecording ? {
  3033. v: common_vendor.t($data.recordingTimeDisplay || "00:00 / 05:00")
  3034. } : {}, {
  3035. w: $data.showStartRecordingButton
  3036. }, $data.showStartRecordingButton ? {
  3037. x: common_vendor.o((...args) => $options.handleStartRecordingClick && $options.handleStartRecordingClick(...args))
  3038. } : {}, {
  3039. y: $data.showRetryButton
  3040. }, $data.showRetryButton ? {
  3041. z: common_vendor.o((...args) => $options.retryVideoUpload && $options.retryVideoUpload(...args))
  3042. } : {}, {
  3043. A: $data.showCountdown
  3044. }, $data.showCountdown ? {
  3045. B: common_vendor.t($data.countdownValue)
  3046. } : {}, {
  3047. C: $data.isPlayingAiVoice
  3048. }, $data.isPlayingAiVoice ? {} : {}, {
  3049. D: $data.showContinueQuestionButton
  3050. }, $data.showContinueQuestionButton ? {
  3051. E: common_vendor.o((...args) => $options.handleContinueQuestionClick && $options.handleContinueQuestionClick(...args))
  3052. } : {});
  3053. }
  3054. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-2a02c54e"]]);
  3055. wx.createPage(MiniProgramPage);