interview-question.js 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058
  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/success/success",
  1526. //'/pages/posture-guide/posture-guide?uploading=' + (this.uploadQueue.length > 0 ? 'true' : 'false'),
  1527. success: () => {
  1528. console.log("成功跳转到结果页面");
  1529. },
  1530. fail: (err) => {
  1531. console.error("跳转失败:", err);
  1532. this.handleNavigationFailure();
  1533. }
  1534. });
  1535. },
  1536. // 添加处理导航失败的方法
  1537. handleNavigationFailure() {
  1538. common_vendor.index.navigateTo({
  1539. url: "/pages/success/success",
  1540. //'/pages/posture-guide/posture-guide',
  1541. fail: (err2) => {
  1542. console.error("备用跳转也失败:", err2);
  1543. common_vendor.index.navigateBack({
  1544. delta: 1
  1545. });
  1546. }
  1547. });
  1548. },
  1549. // 修改 handleAnswerButtonClick 方法
  1550. handleAnswerButtonClick() {
  1551. this.showAnswerButton = false;
  1552. common_vendor.index.showToast({
  1553. title: "面试完成",
  1554. icon: "success",
  1555. duration: 500
  1556. // 只显示0.5秒
  1557. });
  1558. if (this.lastUploadedVideoUrl) {
  1559. this.submitToAiVoiceInteraction(this.lastUploadedVideoUrl);
  1560. } else {
  1561. this.navigateToNextPage();
  1562. }
  1563. },
  1564. // 处理相机错误
  1565. handleCameraError(e) {
  1566. console.error("相机错误:", e);
  1567. const systemInfo = common_vendor.index.getSystemInfoSync();
  1568. const isIOS = systemInfo.platform === "ios";
  1569. if (isIOS) {
  1570. console.log("iOS相机错误,尝试重新初始化");
  1571. common_vendor.index.showToast({
  1572. title: "相机初始化中...",
  1573. icon: "loading",
  1574. duration: 2e3
  1575. });
  1576. this.resetCamera();
  1577. if (this.isRecording) {
  1578. this.isRecording = false;
  1579. this.showStopRecordingButton = false;
  1580. setTimeout(() => {
  1581. this.useAlternativeRecordingMethod();
  1582. }, 1e3);
  1583. }
  1584. } else {
  1585. common_vendor.index.showToast({
  1586. title: "相机初始化失败,请检查权限设置",
  1587. icon: "none"
  1588. });
  1589. this.tryFallbackOptions();
  1590. }
  1591. },
  1592. // 添加新方法:尝试备用选项
  1593. tryFallbackOptions() {
  1594. const systemInfo = common_vendor.index.getSystemInfoSync();
  1595. if (systemInfo.uniPlatform === "mp-weixin" || systemInfo.uniPlatform === "mp-alipay") {
  1596. this.useMiniProgramCamera();
  1597. } else {
  1598. this.showStaticCameraPlaceholder();
  1599. }
  1600. },
  1601. // 添加新方法:使用小程序相机API
  1602. useMiniProgramCamera() {
  1603. console.log("尝试使用小程序相机组件");
  1604. this.useMiniProgramCameraComponent = true;
  1605. },
  1606. // 添加新方法:显示静态图像
  1607. showStaticCameraPlaceholder() {
  1608. console.log("显示静态摄像头占位图");
  1609. const img = document.createElement("img");
  1610. img.src = "/static/images/camera-placeholder.png";
  1611. img.className = "static-camera-image";
  1612. img.style.width = "100%";
  1613. img.style.height = "100%";
  1614. img.style.objectFit = "cover";
  1615. const container = this.$refs.userCameraVideo.parentNode;
  1616. container.appendChild(img);
  1617. },
  1618. // 处理视频时间更新事件
  1619. handleTimeUpdate(e) {
  1620. const currentTime = e.target.currentTime;
  1621. if (this.isRecording && this.recordingTimerCount) {
  1622. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  1623. }
  1624. let currentSubtitles;
  1625. if (this.currentVideoIndex === 0) {
  1626. currentSubtitles = this.subtitles;
  1627. } else if (this.currentVideoIndex === 1) {
  1628. currentSubtitles = this.secondVideoSubtitles;
  1629. } else if (this.currentVideoIndex === 2) {
  1630. currentSubtitles = this.thirdVideoSubtitles;
  1631. } else if (this.currentVideoIndex === 3) {
  1632. currentSubtitles = this.fourthVideoSubtitles;
  1633. } else if (this.currentVideoIndex === 4) {
  1634. currentSubtitles = this.fifthVideoSubtitles;
  1635. } else if (this.currentVideoIndex === 5) {
  1636. currentSubtitles = this.sixthVideoSubtitles;
  1637. } else {
  1638. currentSubtitles = [];
  1639. }
  1640. const subtitle = currentSubtitles.find(
  1641. (sub) => currentTime >= sub.startTime && currentTime < sub.endTime
  1642. );
  1643. this.currentSubtitle = subtitle ? subtitle.text : "";
  1644. },
  1645. // Add a new method to handle the "Start Recording" button click
  1646. handleStartRecordingClick() {
  1647. this.showStartRecordingButton = false;
  1648. this.clearCountdown();
  1649. this.startRecordingAnswer();
  1650. },
  1651. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  1652. checkAudioPermission() {
  1653. const systemInfo = common_vendor.index.getSystemInfoSync();
  1654. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1655. if (isMiniProgram) {
  1656. common_vendor.index.getSetting({
  1657. success: (res) => {
  1658. if (!res.authSetting["scope.record"]) {
  1659. common_vendor.index.authorize({
  1660. scope: "scope.record",
  1661. success: () => {
  1662. console.log("录音权限已获取");
  1663. },
  1664. fail: (err) => {
  1665. console.error("录音权限获取失败:", err);
  1666. this.showPermissionDialog("录音");
  1667. }
  1668. });
  1669. }
  1670. if (!res.authSetting["scope.camera"]) {
  1671. common_vendor.index.authorize({
  1672. scope: "scope.camera",
  1673. success: () => {
  1674. console.log("相机权限已获取");
  1675. },
  1676. fail: (err) => {
  1677. console.error("相机权限获取失败:", err);
  1678. this.showPermissionDialog("相机");
  1679. }
  1680. });
  1681. }
  1682. }
  1683. });
  1684. }
  1685. },
  1686. // 添加新方法:测试音频输入
  1687. testAudioInput() {
  1688. if (!this.cameraStream) {
  1689. console.warn("没有可用的媒体流,无法测试音频");
  1690. return;
  1691. }
  1692. const audioTracks = this.cameraStream.getAudioTracks();
  1693. if (audioTracks.length === 0) {
  1694. console.warn("没有检测到音频轨道,尝试重新获取");
  1695. this.tryGetAudioOnly();
  1696. return;
  1697. }
  1698. console.log("音频轨道信息:", audioTracks[0].getSettings());
  1699. try {
  1700. const AudioContext = window.AudioContext || window.webkitAudioContext;
  1701. if (!AudioContext) {
  1702. console.warn("浏览器不支持AudioContext");
  1703. return;
  1704. }
  1705. const audioContext = new AudioContext();
  1706. const analyser = audioContext.createAnalyser();
  1707. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  1708. microphone.connect(analyser);
  1709. analyser.fftSize = 256;
  1710. const bufferLength = analyser.frequencyBinCount;
  1711. const dataArray = new Uint8Array(bufferLength);
  1712. let silenceCounter = 0;
  1713. const checkAudio = () => {
  1714. if (this.isRecording)
  1715. return;
  1716. analyser.getByteFrequencyData(dataArray);
  1717. let sum = 0;
  1718. for (let i = 0; i < bufferLength; i++) {
  1719. sum += dataArray[i];
  1720. }
  1721. const average = sum / bufferLength;
  1722. if (average > 10) {
  1723. console.log("检测到音频输入,音量:", average);
  1724. silenceCounter = 0;
  1725. } else {
  1726. silenceCounter++;
  1727. if (silenceCounter > 10) {
  1728. console.warn("持续检测不到音频输入,可能麦克风未正常工作");
  1729. silenceCounter = 0;
  1730. }
  1731. }
  1732. requestAnimationFrame(checkAudio);
  1733. };
  1734. checkAudio();
  1735. } catch (e) {
  1736. console.error("音频测试失败:", e);
  1737. }
  1738. },
  1739. // 添加新方法:尝试单独获取音频
  1740. tryGetAudioOnly() {
  1741. navigator.mediaDevices.getUserMedia({ audio: true }).then((audioStream) => {
  1742. if (this.cameraStream) {
  1743. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1744. const audioTrack = audioStream.getAudioTracks()[0];
  1745. const combinedStream = new MediaStream();
  1746. if (videoTrack)
  1747. combinedStream.addTrack(videoTrack);
  1748. if (audioTrack)
  1749. combinedStream.addTrack(audioTrack);
  1750. this.cameraStream = combinedStream;
  1751. const videoElement = this.$refs.userCameraVideo;
  1752. if (videoElement) {
  1753. videoElement.srcObject = combinedStream;
  1754. videoElement.muted = true;
  1755. }
  1756. console.log("成功合并音频和视频轨道");
  1757. } else {
  1758. console.warn("没有视频流可合并");
  1759. }
  1760. }).catch((err) => {
  1761. console.error("单独获取音频失败:", err);
  1762. });
  1763. },
  1764. // 添加新方法:显示权限对话框
  1765. showPermissionDialog(permissionType) {
  1766. common_vendor.index.showModal({
  1767. title: "需要权限",
  1768. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  1769. confirmText: "去设置",
  1770. success: (res) => {
  1771. if (res.confirm) {
  1772. common_vendor.index.openSetting({
  1773. success: (settingRes) => {
  1774. console.log("设置页面打开成功", settingRes);
  1775. }
  1776. });
  1777. }
  1778. }
  1779. });
  1780. },
  1781. // 添加重试上传方法
  1782. retryVideoUpload() {
  1783. if (this.lastVideoToRetry) {
  1784. this.showRetryButton = false;
  1785. common_vendor.index.showLoading({
  1786. title: "正在重新提交...",
  1787. mask: true
  1788. });
  1789. this.submitVideoToInterview(this.lastVideoToRetry);
  1790. } else {
  1791. common_vendor.index.showToast({
  1792. title: "没有可重试的视频",
  1793. icon: "none"
  1794. });
  1795. }
  1796. },
  1797. // 添加一个新方法用于压缩视频
  1798. async compressVideo(videoBlob) {
  1799. if (videoBlob.size < 5 * 1024 * 1024) {
  1800. return videoBlob;
  1801. }
  1802. console.log("开始压缩视频,原始大小:", videoBlob.size);
  1803. const videoElement = document.createElement("video");
  1804. videoElement.muted = true;
  1805. videoElement.autoplay = false;
  1806. const canvas = document.createElement("canvas");
  1807. const ctx = canvas.getContext("2d");
  1808. videoElement.src = URL.createObjectURL(videoBlob);
  1809. return new Promise((resolve) => {
  1810. videoElement.onloadedmetadata = () => {
  1811. const width = Math.floor(videoElement.videoWidth / 2);
  1812. const height = Math.floor(videoElement.videoHeight / 2);
  1813. canvas.width = width;
  1814. canvas.height = height;
  1815. const stream = canvas.captureStream(15);
  1816. if (videoElement.captureStream) {
  1817. const originalStream = videoElement.captureStream();
  1818. const audioTracks = originalStream.getAudioTracks();
  1819. if (audioTracks.length > 0) {
  1820. stream.addTrack(audioTracks[0]);
  1821. }
  1822. }
  1823. const options = {
  1824. mimeType: "video/webm;codecs=vp8,opus",
  1825. audioBitsPerSecond: 64e3,
  1826. videoBitsPerSecond: 8e5
  1827. // 800kbps
  1828. };
  1829. const mediaRecorder = new MediaRecorder(stream, options);
  1830. const chunks = [];
  1831. mediaRecorder.ondataavailable = (e) => {
  1832. if (e.data.size > 0) {
  1833. chunks.push(e.data);
  1834. }
  1835. };
  1836. mediaRecorder.onstop = () => {
  1837. const compressedBlob = new Blob(chunks, { type: "video/webm" });
  1838. console.log("视频压缩完成,压缩后大小:", compressedBlob.size);
  1839. resolve(compressedBlob);
  1840. };
  1841. videoElement.onplay = () => {
  1842. mediaRecorder.start(100);
  1843. const drawFrame = () => {
  1844. if (videoElement.paused || videoElement.ended) {
  1845. mediaRecorder.stop();
  1846. return;
  1847. }
  1848. ctx.drawImage(videoElement, 0, 0, width, height);
  1849. requestAnimationFrame(drawFrame);
  1850. };
  1851. drawFrame();
  1852. };
  1853. videoElement.play();
  1854. };
  1855. });
  1856. },
  1857. // 修改 checkIOSCameraRecordPermission 方法
  1858. checkIOSCameraRecordPermission() {
  1859. const systemInfo = common_vendor.index.getSystemInfoSync();
  1860. if (systemInfo.platform !== "ios")
  1861. return;
  1862. common_vendor.index.getSetting({
  1863. success: (res) => {
  1864. if (!res.authSetting["scope.camera"]) {
  1865. common_vendor.index.authorize({
  1866. scope: "scope.camera",
  1867. success: () => {
  1868. console.log("iOS相机权限已获取");
  1869. },
  1870. fail: (err) => {
  1871. console.error("iOS相机权限获取失败:", err);
  1872. this.showPermissionDialog("相机");
  1873. }
  1874. });
  1875. }
  1876. if (!res.authSetting["scope.record"]) {
  1877. common_vendor.index.authorize({
  1878. scope: "scope.record",
  1879. success: () => {
  1880. console.log("iOS录音权限已获取");
  1881. },
  1882. fail: (err) => {
  1883. console.error("iOS录音权限获取失败:", err);
  1884. this.showPermissionDialog("录音");
  1885. }
  1886. });
  1887. }
  1888. }
  1889. });
  1890. },
  1891. // 添加新方法:检查并修复渲染问题
  1892. checkAndFixRenderingIssues() {
  1893. try {
  1894. if (typeof u !== "undefined" && u) {
  1895. if (!u.currentQuestion) {
  1896. console.log("修复: 创建缺失的currentQuestion对象");
  1897. u.currentQuestion = {};
  1898. }
  1899. if (u.currentQuestion && typeof u.currentQuestion.isImportant === "undefined") {
  1900. console.log("修复: 设置缺失的isImportant属性");
  1901. u.currentQuestion.isImportant = false;
  1902. }
  1903. }
  1904. } catch (e) {
  1905. console.log("防御性检查异常:", e);
  1906. }
  1907. },
  1908. // 添加格式化时间的辅助方法
  1909. formatTime(seconds) {
  1910. if (!seconds && seconds !== 0)
  1911. return "03:30";
  1912. const minutes = Math.floor(seconds / 60);
  1913. const remainingSeconds = seconds % 60;
  1914. return `${minutes.toString().padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
  1915. },
  1916. // 添加新方法:保存上传状态到本地存储
  1917. saveUploadStatus() {
  1918. const uploadStatus = {
  1919. isUploading: this.isUploading,
  1920. uploadQueue: this.uploadQueue.length,
  1921. timestamp: Date.now()
  1922. };
  1923. try {
  1924. common_vendor.index.setStorageSync("videoUploadStatus", JSON.stringify(uploadStatus));
  1925. } catch (e) {
  1926. console.error("保存上传状态失败:", e);
  1927. }
  1928. },
  1929. // 修改 submitToAiVoiceInteraction 方法
  1930. submitToAiVoiceInteraction(videoUrl) {
  1931. console.log("提交视频到AI语音交互接口:", videoUrl);
  1932. common_vendor.index.showLoading({
  1933. title: "面试官正在思考...",
  1934. mask: true
  1935. });
  1936. const tenant_id = common_vendor.index.getStorageSync("tenant_id") || "1";
  1937. const application_id = common_vendor.index.getStorageSync("appId") || "98";
  1938. let positionConfigId = 1;
  1939. if (JSON.parse(common_vendor.index.getStorageSync("configData"))) {
  1940. positionConfigId = JSON.parse(common_vendor.index.getStorageSync("configData")).id;
  1941. }
  1942. const requestData = {
  1943. voice_url: videoUrl,
  1944. tenant_id,
  1945. application_id,
  1946. scene_type: "interview",
  1947. // 场景类型:面试
  1948. // voice_type: 'longxiaoxia', // 角色:龙小侠
  1949. position_config_id: positionConfigId,
  1950. conversation_history: this.conversationHistory
  1951. // 添加对话历史
  1952. };
  1953. const systemInfo = common_vendor.index.getSystemInfoSync();
  1954. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  1955. if (isMiniProgram) {
  1956. common_vendor.index.request({
  1957. url: `${common_config.apiBaseUrl}/api/system/voice/mcp/interaction`,
  1958. // 使用新的接口地址
  1959. method: "POST",
  1960. data: requestData,
  1961. header: {
  1962. "content-type": "application/json"
  1963. // 修改为JSON格式
  1964. },
  1965. success: (res) => {
  1966. if (res.statusCode === 200) {
  1967. this.handleAiInteractionResponse(res.data);
  1968. } else {
  1969. this.handleAiInteractionError(res.data);
  1970. }
  1971. },
  1972. fail: (err) => {
  1973. this.handleAiInteractionError(err);
  1974. }
  1975. });
  1976. } else {
  1977. const xhr = new XMLHttpRequest();
  1978. xhr.open("POST", `${common_config.apiBaseUrl}/api/system/voice/mcp/interaction`, true);
  1979. xhr.timeout = 6e4;
  1980. xhr.setRequestHeader("Content-Type", "application/json");
  1981. xhr.onload = () => {
  1982. if (xhr.status === 200) {
  1983. try {
  1984. const response = JSON.parse(xhr.responseText);
  1985. this.handleAiInteractionResponse(response);
  1986. } catch (e) {
  1987. console.error("解析AI响应失败:", e);
  1988. this.handleAiInteractionError({ errMsg: "解析响应失败" });
  1989. }
  1990. } else {
  1991. this.handleAiInteractionError({ errMsg: "HTTP状态: " + xhr.status });
  1992. }
  1993. };
  1994. xhr.onerror = () => {
  1995. this.handleAiInteractionError({ errMsg: "网络错误" });
  1996. };
  1997. xhr.ontimeout = () => {
  1998. this.handleAiInteractionError({ errMsg: "请求超时" });
  1999. };
  2000. xhr.send(JSON.stringify(requestData));
  2001. }
  2002. },
  2003. // 添加处理AI交互响应的方法
  2004. handleAiInteractionResponse(data) {
  2005. console.log("AI语音交互接口响应:", data);
  2006. const systemInfo = common_vendor.index.getSystemInfoSync();
  2007. const isIOS = systemInfo.platform === "ios";
  2008. this.isPlayingAiVoice = true;
  2009. common_vendor.index.hideLoading();
  2010. this.hasTriedFallbackMethod = false;
  2011. if (data && data.success) {
  2012. this.aiText = data.ai_text || "";
  2013. this.transcribedText = data.transcribed_text ? data.transcribed_text.text : "";
  2014. this.conversationHistory.push({
  2015. role: "user",
  2016. content: this.transcribedText
  2017. });
  2018. this.conversationHistory.push({
  2019. role: "assistant",
  2020. content: this.aiText
  2021. });
  2022. const aiVoiceUrl = data.ai_voice_url || "";
  2023. if (aiVoiceUrl) {
  2024. this.aiVoiceUrl = aiVoiceUrl.startsWith("http") ? aiVoiceUrl : `${common_config.apiBaseUrl}${aiVoiceUrl}`;
  2025. console.log("AI语音URL:", this.aiVoiceUrl);
  2026. this.currentSubtitle = this.aiText;
  2027. const systemInfo2 = common_vendor.index.getSystemInfoSync();
  2028. const isMiniProgram = systemInfo2.uniPlatform && systemInfo2.uniPlatform.startsWith("mp-");
  2029. const isUnsupportedFormat = this.checkUnsupportedAudioFormat(this.aiVoiceUrl);
  2030. if (isUnsupportedFormat && isMiniProgram) {
  2031. console.warn("检测到小程序不支持的音频格式,直接使用文本显示");
  2032. this.displayTextResponse();
  2033. return;
  2034. }
  2035. if (isMiniProgram) {
  2036. if (isIOS) {
  2037. this.playIOSMiniProgramAudio();
  2038. } else {
  2039. this.playMiniProgramAudio();
  2040. }
  2041. } else {
  2042. this.playAiVoice();
  2043. }
  2044. this.showContinueQuestionButton = false;
  2045. this.showEndInterviewButton = false;
  2046. } else {
  2047. console.warn("未获取到AI语音URL,使用文本显示");
  2048. this.displayTextResponse();
  2049. }
  2050. } else {
  2051. console.error("AI语音交互失败:", data);
  2052. this.showContinueQuestionOptions();
  2053. this.checkPendingNavigation();
  2054. }
  2055. },
  2056. // 修改 handleAiInteractionError 方法,不要直接跳转,而是显示继续提问选项
  2057. handleAiInteractionError(err) {
  2058. console.error("AI语音交互请求失败:", err);
  2059. common_vendor.index.hideLoading();
  2060. common_vendor.index.showToast({
  2061. title: "AI处理失败,请重试",
  2062. icon: "none",
  2063. duration: 2e3
  2064. });
  2065. this.showContinueQuestionOptions();
  2066. },
  2067. // 添加新方法:播放AI语音
  2068. playAiVoice() {
  2069. console.log("开始播放AI语音:", this.aiVoiceUrl);
  2070. this.hasShownTextFallbackToast = false;
  2071. this.isPlayingAiVoice = true;
  2072. this.showContinueQuestionButton = false;
  2073. this.showEndInterviewButton = false;
  2074. this.currentSubtitle = this.aiText;
  2075. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === "") {
  2076. console.error("无效的音频URL,直接显示文本");
  2077. this.useTextFallbackAndNavigate();
  2078. return;
  2079. }
  2080. const systemInfo = common_vendor.index.getSystemInfoSync();
  2081. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  2082. const isIOS = systemInfo.platform === "ios";
  2083. if (isMiniProgram) {
  2084. if (isIOS) {
  2085. this.playIOSMiniProgramAudio();
  2086. } else {
  2087. this.playMiniProgramAudio();
  2088. }
  2089. } else if (isIOS) {
  2090. this.playAiVoiceForIOS();
  2091. } else {
  2092. this.playAiVoiceForAndroid();
  2093. }
  2094. },
  2095. // 新增方法:文本回退并导航
  2096. useTextFallbackAndNavigate() {
  2097. console.log("使用文本回退并准备导航");
  2098. this.isPlayingAiVoice = false;
  2099. if (!this.hasShownTextFallbackToast) {
  2100. this.hasShownTextFallbackToast = true;
  2101. common_vendor.index.showToast({
  2102. title: "为您显示文字回复",
  2103. icon: "none",
  2104. duration: 2e3
  2105. });
  2106. }
  2107. this.currentSubtitle = this.aiText || "抱歉,AI回复内容加载失败";
  2108. setTimeout(() => {
  2109. this.navigateToNextPage();
  2110. }, 5e3);
  2111. },
  2112. // 修改 handleIOSAudioPlaybackComplete 方法
  2113. handleIOSAudioPlaybackComplete() {
  2114. this.isPlayingAiVoice = false;
  2115. setTimeout(() => {
  2116. this.currentSubtitle = "";
  2117. }, 1e3);
  2118. if (this.aiAudioPlayer) {
  2119. try {
  2120. this.aiAudioPlayer.destroy();
  2121. } catch (e) {
  2122. console.error("销毁iOS音频播放器失败:", e);
  2123. }
  2124. this.aiAudioPlayer = null;
  2125. }
  2126. this.navigateToNextPage();
  2127. },
  2128. // 修改 playMiniProgramAudio 方法
  2129. playMiniProgramAudio() {
  2130. console.log("使用小程序原生音频API播放:", this.aiVoiceUrl);
  2131. try {
  2132. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2133. audioContext.autoplay = true;
  2134. audioContext.src = this.aiVoiceUrl;
  2135. audioContext.onPlay(() => {
  2136. console.log("AI语音开始播放");
  2137. this.currentSubtitle = this.aiText;
  2138. });
  2139. audioContext.onEnded(() => {
  2140. console.log("AI语音播放结束");
  2141. this.isPlayingAiVoice = false;
  2142. this.currentSubtitle = "";
  2143. audioContext.destroy();
  2144. this.navigateToNextPage();
  2145. });
  2146. audioContext.onError((err) => {
  2147. console.error("AI语音播放错误:", err);
  2148. audioContext.destroy();
  2149. this.useTextFallbackAndNavigate();
  2150. });
  2151. setTimeout(() => {
  2152. if (this.isPlayingAiVoice) {
  2153. console.warn("音频播放超时,使用文本回退");
  2154. this.useTextFallbackAndNavigate();
  2155. }
  2156. }, 5e3);
  2157. } catch (e) {
  2158. console.error("小程序播放AI语音失败:", e);
  2159. this.useTextFallbackAndNavigate();
  2160. }
  2161. },
  2162. // 新增方法:导航到下一页
  2163. navigateToNextPage() {
  2164. console.log("准备导航到下一页");
  2165. common_vendor.index.navigateTo({
  2166. url: "/pages/success/success",
  2167. //'/pages/posture-guide/posture-guide',
  2168. fail: (err) => {
  2169. console.error("导航失败:", err);
  2170. common_vendor.index.showToast({
  2171. title: "页面跳转失败",
  2172. icon: "none",
  2173. duration: 2e3
  2174. });
  2175. }
  2176. });
  2177. },
  2178. // 新增方法:专门处理iOS小程序音频播放
  2179. playIOSMiniProgramAudio() {
  2180. console.log("iOS小程序环境:使用增强的音频处理方法");
  2181. this.hasShownTextFallbackToast = false;
  2182. try {
  2183. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2184. audioContext.autoplay = false;
  2185. audioContext.obeyMuteSwitch = true;
  2186. audioContext.volume = 1;
  2187. audioContext.onPlay(() => {
  2188. console.log("iOS小程序音频开始播放");
  2189. this.currentSubtitle = this.aiText;
  2190. });
  2191. audioContext.onEnded(() => {
  2192. console.log("iOS小程序音频播放结束");
  2193. this.handleIOSAudioComplete(audioContext);
  2194. });
  2195. audioContext.onStop(() => {
  2196. console.log("iOS小程序音频播放停止");
  2197. this.handleIOSAudioComplete(audioContext);
  2198. });
  2199. audioContext.onError((err) => {
  2200. console.error("iOS小程序音频播放错误:", err);
  2201. this.handleIOSMiniProgramError(audioContext, err);
  2202. });
  2203. this.preloadIOSMiniProgramAudio(this.aiVoiceUrl).then((localPath) => {
  2204. console.log("iOS小程序音频预下载成功:", localPath);
  2205. audioContext.src = localPath;
  2206. audioContext.play();
  2207. }).catch((error) => {
  2208. console.error("iOS小程序音频预下载失败:", error);
  2209. this.fallbackIOSMiniProgramPlay(audioContext);
  2210. });
  2211. } catch (e) {
  2212. console.error("iOS小程序音频初始化失败:", e);
  2213. this.useIOSTextFallback();
  2214. }
  2215. },
  2216. // 新增方法:iOS小程序音频预下载
  2217. preloadIOSMiniProgramAudio(audioUrl) {
  2218. return new Promise((resolve, reject) => {
  2219. console.log("开始预下载iOS小程序音频:", audioUrl);
  2220. common_vendor.index.downloadFile({
  2221. url: audioUrl,
  2222. timeout: 15e3,
  2223. // 15秒超时
  2224. success: (res) => {
  2225. if (res.statusCode === 200) {
  2226. console.log("iOS小程序音频下载成功:", res.tempFilePath);
  2227. resolve(res.tempFilePath);
  2228. } else {
  2229. console.error("iOS小程序音频下载失败,状态码:", res.statusCode);
  2230. reject(new Error(`下载失败,状态码: ${res.statusCode}`));
  2231. }
  2232. },
  2233. fail: (err) => {
  2234. console.error("iOS小程序音频下载请求失败:", err);
  2235. reject(err);
  2236. }
  2237. });
  2238. });
  2239. },
  2240. // 新增方法:iOS小程序错误处理
  2241. handleIOSMiniProgramError(audioContext, error) {
  2242. console.error("处理iOS小程序音频错误:", error);
  2243. if (audioContext) {
  2244. try {
  2245. audioContext.destroy();
  2246. } catch (e) {
  2247. console.error("销毁音频播放器失败:", e);
  2248. }
  2249. }
  2250. let errorMessage = "音频播放异常,显示文字";
  2251. if (error.errMsg && error.errMsg.includes("-11850")) {
  2252. console.log("iOS小程序检测到系统音频错误(-11850)");
  2253. errorMessage = "系统音频服务暂停,显示文字";
  2254. } else if (error.errCode === 10002) {
  2255. console.log("iOS小程序检测到errCode:10002,网络或资源问题");
  2256. errorMessage = "音频加载中断,显示文字";
  2257. } else if (error.errCode === 10001) {
  2258. console.log("iOS小程序检测到errCode:10001,系统错误");
  2259. errorMessage = "系统音频错误,显示文字";
  2260. } else if (error.errCode === 10003) {
  2261. console.log("iOS小程序检测到errCode:10003,文件错误");
  2262. errorMessage = "音频文件错误,显示文字";
  2263. } else if (error.errCode === 10004) {
  2264. console.log("iOS小程序检测到errCode:10004,格式不支持");
  2265. errorMessage = "音频格式不支持,显示文字";
  2266. }
  2267. common_vendor.index.showToast({
  2268. title: errorMessage,
  2269. icon: "none",
  2270. duration: 2e3
  2271. });
  2272. this.useIOSTextFallback();
  2273. },
  2274. // 新增方法:iOS小程序播放超时处理
  2275. handleIOSPlaybackTimeout(audioContext) {
  2276. console.log("iOS小程序音频播放超时处理");
  2277. if (audioContext) {
  2278. try {
  2279. audioContext.destroy();
  2280. } catch (e) {
  2281. console.error("销毁超时音频播放器失败:", e);
  2282. }
  2283. }
  2284. this.useIOSTextFallback();
  2285. },
  2286. // 新增方法:iOS小程序备选播放方法
  2287. fallbackIOSMiniProgramPlay(audioContext) {
  2288. console.log("iOS小程序使用备选播放方法");
  2289. try {
  2290. audioContext.src = this.aiVoiceUrl;
  2291. const fallbackTimeout = setTimeout(() => {
  2292. if (this.isPlayingAiVoice) {
  2293. console.log("iOS小程序备选方法也超时");
  2294. clearTimeout(fallbackTimeout);
  2295. this.handleIOSPlaybackTimeout(audioContext);
  2296. }
  2297. }, 3e3);
  2298. audioContext.play();
  2299. } catch (e) {
  2300. console.error("iOS小程序备选播放方法失败:", e);
  2301. this.handleIOSPlaybackTimeout(audioContext);
  2302. }
  2303. },
  2304. // 新增方法:统一的iOS音频完成处理
  2305. handleIOSAudioComplete(audioContext) {
  2306. console.log("iOS音频播放完成");
  2307. this.isPlayingAiVoice = false;
  2308. setTimeout(() => {
  2309. this.currentSubtitle = "";
  2310. }, 2e3);
  2311. if (audioContext) {
  2312. try {
  2313. audioContext.destroy();
  2314. } catch (e) {
  2315. console.error("销毁iOS音频播放器失败:", e);
  2316. }
  2317. }
  2318. this.showContinueQuestionOptions();
  2319. this.checkPendingNavigation();
  2320. },
  2321. // 移除原来的 playMiniProgramAudioForIOS 方法,因为已经被新的 playIOSMiniProgramAudio 替代
  2322. // playMiniProgramAudioForIOS() 方法可以删除或注释掉
  2323. // 保留原有的 useIOSTextFallback 方法,但确保它能正确处理所有iOS场景
  2324. useIOSTextFallback() {
  2325. console.log("iOS使用文本回退方案");
  2326. this.isPlayingAiVoice = false;
  2327. if (!this.hasShownTextFallbackToast) {
  2328. this.hasShownTextFallbackToast = true;
  2329. }
  2330. this.currentSubtitle = this.aiText || "抱歉,AI回复内容加载失败";
  2331. const textLength = this.aiText ? this.aiText.length : 20;
  2332. const readingTime = Math.max(5e3, Math.min(textLength * 100, 15e3));
  2333. console.log(`文本长度: ${textLength}, 预计阅读时间: ${readingTime}ms`);
  2334. setTimeout(() => {
  2335. this.handleIOSAudioComplete(null);
  2336. }, readingTime);
  2337. },
  2338. // 添加新方法:iOS音频播放完成处理
  2339. handleIOSAudioPlaybackComplete() {
  2340. this.isPlayingAiVoice = false;
  2341. setTimeout(() => {
  2342. this.currentSubtitle = "";
  2343. }, 2e3);
  2344. if (this.aiAudioPlayer) {
  2345. try {
  2346. this.aiAudioPlayer.destroy();
  2347. } catch (e) {
  2348. console.error("销毁iOS音频播放器失败:", e);
  2349. }
  2350. this.aiAudioPlayer = null;
  2351. }
  2352. this.showContinueQuestionOptions();
  2353. this.checkPendingNavigation();
  2354. },
  2355. // 添加新方法:iOS音频错误处理
  2356. handleIOSAudioError(error) {
  2357. console.error("iOS音频播放错误:", error);
  2358. switch (error.errCode) {
  2359. case 10001:
  2360. console.log("iOS音频系统错误,尝试重新播放");
  2361. this.retryIOSAudioPlay();
  2362. break;
  2363. case 10002:
  2364. console.log("iOS音频网络错误,使用备选方案");
  2365. this.handleIOS10002Error();
  2366. break;
  2367. case 10003:
  2368. console.log("iOS音频文件错误,使用文本回退");
  2369. this.useIOSTextFallback();
  2370. break;
  2371. case 10004:
  2372. console.log("iOS音频格式不支持,使用文本回退");
  2373. this.useIOSTextFallback();
  2374. break;
  2375. default:
  2376. console.log("iOS音频未知错误,使用文本回退");
  2377. this.useIOSTextFallback();
  2378. break;
  2379. }
  2380. },
  2381. // 添加新方法:重试iOS音频播放
  2382. retryIOSAudioPlay() {
  2383. console.log("重试iOS音频播放");
  2384. if (this.aiAudioPlayer) {
  2385. try {
  2386. this.aiAudioPlayer.destroy();
  2387. } catch (e) {
  2388. console.error("销毁播放器失败:", e);
  2389. }
  2390. this.aiAudioPlayer = null;
  2391. }
  2392. setTimeout(() => {
  2393. this.fallbackIOSAudioPlay();
  2394. }, 1e3);
  2395. },
  2396. // 添加新方法:iOS备选音频播放
  2397. fallbackIOSAudioPlay() {
  2398. console.log("iOS备选音频播放方案");
  2399. try {
  2400. this.aiAudioPlayer = common_vendor.index.createInnerAudioContext();
  2401. this.aiAudioPlayer.autoplay = true;
  2402. this.aiAudioPlayer.obeyMuteSwitch = true;
  2403. this.aiAudioPlayer.volume = 0.8;
  2404. this.aiAudioPlayer.src = this.aiVoiceUrl;
  2405. this.aiAudioPlayer.onPlay(() => {
  2406. console.log("iOS备选方案:音频开始播放");
  2407. this.currentSubtitle = this.aiText;
  2408. });
  2409. this.aiAudioPlayer.onEnded(() => {
  2410. console.log("iOS备选方案:音频播放结束");
  2411. this.handleIOSAudioPlaybackComplete();
  2412. });
  2413. this.aiAudioPlayer.onError((err) => {
  2414. console.error("iOS备选方案:音频播放错误:", err);
  2415. this.useIOSTextFallback();
  2416. });
  2417. setTimeout(() => {
  2418. if (this.isPlayingAiVoice && this.aiAudioPlayer) {
  2419. console.log("iOS音频播放超时,使用文本回退");
  2420. this.useIOSTextFallback();
  2421. }
  2422. }, 5e3);
  2423. } catch (e) {
  2424. console.error("iOS备选音频播放失败:", e);
  2425. this.useIOSTextFallback();
  2426. }
  2427. },
  2428. // 修改 playMiniProgramAudio 方法,添加iOS特殊处理
  2429. playMiniProgramAudio() {
  2430. console.log("使用小程序原生音频API播放:", this.aiVoiceUrl);
  2431. this.isPlayingAiVoice = true;
  2432. this.showContinueQuestionButton = false;
  2433. this.showEndInterviewButton = false;
  2434. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === "") {
  2435. console.error("无效的音频URL");
  2436. this.handleAudioPlaybackFailure();
  2437. return;
  2438. }
  2439. const systemInfo = common_vendor.index.getSystemInfoSync();
  2440. const isIOS = systemInfo.platform === "ios";
  2441. if (isIOS) {
  2442. this.playMiniProgramAudioForIOS();
  2443. return;
  2444. }
  2445. try {
  2446. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2447. audioContext.autoplay = false;
  2448. audioContext.obeyMuteSwitch = false;
  2449. audioContext.onPlay(() => {
  2450. console.log("AI语音开始播放");
  2451. });
  2452. audioContext.onEnded(() => {
  2453. console.log("AI语音播放结束");
  2454. this.isPlayingAiVoice = false;
  2455. this.currentSubtitle = "";
  2456. audioContext.destroy();
  2457. this.showContinueQuestionOptions();
  2458. });
  2459. audioContext.onError((err) => {
  2460. console.error("AI语音播放错误:", err);
  2461. this.isPlayingAiVoice = false;
  2462. this.currentSubtitle = "";
  2463. audioContext.destroy();
  2464. this.showContinueQuestionOptions();
  2465. });
  2466. audioContext.src = this.aiVoiceUrl;
  2467. const playTimeout = setTimeout(() => {
  2468. if (this.isPlayingAiVoice) {
  2469. console.warn("小程序音频播放超时,尝试备选方法");
  2470. this.tryMiniProgramFallbackAudioPlay();
  2471. }
  2472. }, 5e3);
  2473. setTimeout(() => {
  2474. audioContext.play();
  2475. clearTimeout(playTimeout);
  2476. }, 300);
  2477. } catch (e) {
  2478. console.error("小程序播放AI语音时发生错误:", e);
  2479. this.isPlayingAiVoice = false;
  2480. this.handleAudioPlaybackFailure();
  2481. }
  2482. },
  2483. // 添加新方法:iOS小程序音频播放
  2484. playMiniProgramAudioForIOS() {
  2485. console.log("iOS小程序环境:开始播放AI语音");
  2486. this.hasShownTextFallbackToast = false;
  2487. try {
  2488. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2489. audioContext.autoplay = false;
  2490. audioContext.obeyMuteSwitch = true;
  2491. audioContext.volume = 1;
  2492. audioContext.onError((err) => {
  2493. console.error("iOS小程序音频播放错误:", err);
  2494. if (err.errMsg && err.errMsg.includes("-11850")) {
  2495. console.log("iOS小程序检测到系统音频错误(-11850)");
  2496. } else if (err.errCode === 10002) {
  2497. console.log("iOS小程序检测到errCode:10002,网络或资源问题");
  2498. } else {
  2499. console.log("iOS小程序其他音频错误:", err.errCode);
  2500. }
  2501. this.useIOSMiniProgramTextFallback(audioContext);
  2502. });
  2503. audioContext.src = this.aiVoiceUrl;
  2504. audioContext.onPlay(() => {
  2505. console.log("iOS小程序音频开始播放");
  2506. this.currentSubtitle = this.aiText;
  2507. });
  2508. audioContext.onEnded(() => {
  2509. console.log("iOS小程序音频播放结束");
  2510. this.handleIOSMiniProgramAudioComplete(audioContext);
  2511. });
  2512. const playTimeout = setTimeout(() => {
  2513. if (this.isPlayingAiVoice) {
  2514. console.log("iOS小程序音频播放超时,切换到文本显示");
  2515. clearTimeout(playTimeout);
  2516. this.useIOSMiniProgramTextFallback(audioContext);
  2517. }
  2518. }, 3e3);
  2519. audioContext.play();
  2520. } catch (e) {
  2521. console.error("iOS小程序音频播放初始化失败:", e);
  2522. this.handleAudioPlaybackFailure();
  2523. }
  2524. },
  2525. // 添加新方法:iOS小程序音频播放完成
  2526. handleIOSMiniProgramAudioComplete(audioContext) {
  2527. this.isPlayingAiVoice = false;
  2528. setTimeout(() => {
  2529. this.currentSubtitle = "";
  2530. }, 2e3);
  2531. if (audioContext) {
  2532. try {
  2533. audioContext.destroy();
  2534. } catch (e) {
  2535. console.error("销毁iOS小程序音频播放器失败:", e);
  2536. }
  2537. }
  2538. this.showContinueQuestionOptions();
  2539. this.checkPendingNavigation();
  2540. },
  2541. // 添加新方法:iOS小程序音频错误处理
  2542. handleIOSMiniProgramAudioError(audioContext, error) {
  2543. console.error("iOS小程序音频错误:", error);
  2544. if (error.errCode === 10002) {
  2545. this.useIOSMiniProgramTextFallback(audioContext);
  2546. } else {
  2547. this.useIOSMiniProgramTextFallback(audioContext);
  2548. }
  2549. },
  2550. // 添加新方法:iOS小程序文本回退
  2551. useIOSMiniProgramTextFallback(audioContext) {
  2552. console.log("iOS小程序使用文本回退方案");
  2553. if (audioContext) {
  2554. try {
  2555. audioContext.destroy();
  2556. } catch (e) {
  2557. console.error("销毁音频播放器失败:", e);
  2558. }
  2559. }
  2560. this.isPlayingAiVoice = false;
  2561. this.currentSubtitle = this.aiText || "抱歉,AI回复内容加载失败";
  2562. const textLength = this.aiText ? this.aiText.length : 20;
  2563. const readingTime = Math.max(6e3, Math.min(textLength * 120, 18e3));
  2564. setTimeout(() => {
  2565. this.handleIOSMiniProgramAudioComplete(null);
  2566. }, readingTime);
  2567. },
  2568. // 添加新方法:处理音频播放错误
  2569. handleAudioPlaybackError(error) {
  2570. console.error("音频播放错误:", error);
  2571. if (!this.hasTriedFallbackMethod) {
  2572. this.hasTriedFallbackMethod = true;
  2573. this.tryFallbackAudioPlay();
  2574. return;
  2575. }
  2576. this.handleAudioPlaybackFailure();
  2577. },
  2578. // 添加新方法:尝试备选音频播放方法
  2579. tryFallbackAudioPlay() {
  2580. console.log("使用备选方法播放音频:", this.aiVoiceUrl);
  2581. if (this.aiAudioPlayer) {
  2582. try {
  2583. this.aiAudioPlayer.destroy();
  2584. } catch (e) {
  2585. console.error("销毁音频播放器失败:", e);
  2586. }
  2587. this.aiAudioPlayer = null;
  2588. }
  2589. common_vendor.index.downloadFile({
  2590. url: this.aiVoiceUrl,
  2591. success: (res) => {
  2592. if (res.statusCode === 200) {
  2593. console.log("音频文件下载成功:", res.tempFilePath);
  2594. const newAudioPlayer = common_vendor.index.createInnerAudioContext();
  2595. this.aiAudioPlayer = newAudioPlayer;
  2596. newAudioPlayer.autoplay = true;
  2597. newAudioPlayer.src = res.tempFilePath;
  2598. newAudioPlayer.onPlay(() => {
  2599. console.log("备选方法:音频开始播放");
  2600. });
  2601. newAudioPlayer.onEnded(() => {
  2602. console.log("备选方法:音频播放结束");
  2603. this.isPlayingAiVoice = false;
  2604. this.currentSubtitle = "";
  2605. newAudioPlayer.destroy();
  2606. this.aiAudioPlayer = null;
  2607. this.showContinueQuestionOptions();
  2608. this.checkPendingNavigation();
  2609. });
  2610. newAudioPlayer.onError((err) => {
  2611. console.error("备选方法:音频播放错误:", err);
  2612. this.isPlayingAiVoice = false;
  2613. this.currentSubtitle = "";
  2614. newAudioPlayer.destroy();
  2615. this.aiAudioPlayer = null;
  2616. this.showContinueQuestionOptions();
  2617. this.checkPendingNavigation();
  2618. });
  2619. } else {
  2620. console.error("音频文件下载失败:", res);
  2621. this.handleAudioPlaybackFailure();
  2622. }
  2623. },
  2624. fail: (err) => {
  2625. console.error("音频文件下载请求失败:", err);
  2626. this.handleAudioPlaybackFailure();
  2627. }
  2628. });
  2629. },
  2630. // 添加新方法:处理音频播放失败的情况
  2631. handleAudioPlaybackFailure() {
  2632. this.isPlayingAiVoice = false;
  2633. this.currentSubtitle = this.aiText;
  2634. setTimeout(() => {
  2635. this.currentSubtitle = "";
  2636. this.showContinueQuestionOptions();
  2637. this.checkPendingNavigation();
  2638. }, 1e4);
  2639. },
  2640. // 添加新方法:从视频中提取音频
  2641. async extractAudioFromVideo(videoBlob) {
  2642. console.log("开始从视频中提取音频");
  2643. return new Promise((resolve, reject) => {
  2644. try {
  2645. const videoElement = document.createElement("video");
  2646. videoElement.muted = false;
  2647. videoElement.autoplay = false;
  2648. const audioContext = new (window.AudioContext || window.webkitAudioContext)();
  2649. let audioDestination = null;
  2650. let mediaRecorder = null;
  2651. const audioChunks = [];
  2652. videoElement.src = URL.createObjectURL(videoBlob);
  2653. videoElement.onloadedmetadata = () => {
  2654. console.log("视频元数据已加载,视频时长:", videoElement.duration);
  2655. const source = audioContext.createMediaElementSource(videoElement);
  2656. audioDestination = audioContext.createMediaStreamDestination();
  2657. source.connect(audioDestination);
  2658. mediaRecorder = new MediaRecorder(audioDestination.stream);
  2659. mediaRecorder.ondataavailable = (event) => {
  2660. if (event.data.size > 0) {
  2661. audioChunks.push(event.data);
  2662. }
  2663. };
  2664. mediaRecorder.onstop = () => {
  2665. const audioBlob = new Blob(audioChunks, { type: "audio/wav" });
  2666. console.log("音频提取完成,大小:", audioBlob.size);
  2667. resolve(audioBlob);
  2668. };
  2669. videoElement.onplay = () => {
  2670. mediaRecorder.start(100);
  2671. };
  2672. videoElement.onended = () => {
  2673. mediaRecorder.stop();
  2674. audioContext.close();
  2675. };
  2676. videoElement.play();
  2677. };
  2678. videoElement.onerror = (err) => {
  2679. console.error("视频加载失败:", err);
  2680. reject(new Error("视频加载失败"));
  2681. };
  2682. } catch (err) {
  2683. console.error("提取音频失败:", err);
  2684. reject(err);
  2685. }
  2686. });
  2687. },
  2688. // 添加新方法:上传音频文件
  2689. async uploadAudioFile(audioBlob, videoUrl) {
  2690. console.log("准备上传音频文件");
  2691. const formData = new FormData();
  2692. const fileName = `audio_${Date.now()}.wav`;
  2693. const audioFile = new File([audioBlob], fileName, { type: "audio/wav" });
  2694. formData.append("audio_file", audioFile);
  2695. formData.append("video_url", videoUrl);
  2696. formData.append("tenant_id", common_vendor.index.getStorageSync("tenant_id") || "1");
  2697. formData.append("openid", common_vendor.index.getStorageSync("userInfo") ? JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid : "");
  2698. formData.append("application_id", common_vendor.index.getStorageSync("appId") || "");
  2699. return new Promise((resolve, reject) => {
  2700. const xhr = new XMLHttpRequest();
  2701. xhr.open("POST", `${common_config.apiBaseUrl}/api/voice_ai_interaction`, true);
  2702. xhr.timeout = 6e4;
  2703. xhr.onload = () => {
  2704. if (xhr.status === 200) {
  2705. try {
  2706. const response = JSON.parse(xhr.responseText);
  2707. resolve(response);
  2708. } catch (e) {
  2709. reject(new Error("解析响应失败"));
  2710. }
  2711. } else {
  2712. reject(new Error("HTTP状态: " + xhr.status));
  2713. }
  2714. };
  2715. xhr.onerror = () => {
  2716. reject(new Error("网络错误"));
  2717. };
  2718. xhr.ontimeout = () => {
  2719. reject(new Error("请求超时"));
  2720. };
  2721. xhr.send(formData);
  2722. });
  2723. },
  2724. // 添加一个新方法:使用小程序原生音频API
  2725. playMiniProgramAudio() {
  2726. console.log("使用小程序原生音频API播放:", this.aiVoiceUrl);
  2727. this.isPlayingAiVoice = true;
  2728. this.showContinueQuestionButton = false;
  2729. this.showEndInterviewButton = false;
  2730. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === "") {
  2731. console.error("无效的音频URL");
  2732. this.isPlayingAiVoice = false;
  2733. this.showContinueQuestionOptions();
  2734. return;
  2735. }
  2736. try {
  2737. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2738. audioContext.autoplay = false;
  2739. audioContext.obeyMuteSwitch = false;
  2740. audioContext.onPlay(() => {
  2741. console.log("AI语音开始播放");
  2742. });
  2743. audioContext.onEnded(() => {
  2744. console.log("AI语音播放结束");
  2745. this.isPlayingAiVoice = false;
  2746. this.currentSubtitle = "";
  2747. audioContext.destroy();
  2748. this.showContinueQuestionOptions();
  2749. });
  2750. audioContext.onError((err) => {
  2751. console.error("AI语音播放错误:", err);
  2752. this.isPlayingAiVoice = false;
  2753. this.currentSubtitle = "";
  2754. audioContext.destroy();
  2755. this.showContinueQuestionOptions();
  2756. });
  2757. audioContext.src = this.aiVoiceUrl;
  2758. const playTimeout = setTimeout(() => {
  2759. if (this.isPlayingAiVoice) {
  2760. console.warn("小程序音频播放超时,尝试备选方法");
  2761. this.tryMiniProgramFallbackAudioPlay();
  2762. }
  2763. }, 5e3);
  2764. setTimeout(() => {
  2765. audioContext.play();
  2766. clearTimeout(playTimeout);
  2767. }, 300);
  2768. } catch (e) {
  2769. console.error("小程序播放AI语音时发生错误:", e);
  2770. this.isPlayingAiVoice = false;
  2771. this.handleAudioPlaybackFailure();
  2772. }
  2773. },
  2774. // 添加新方法:小程序环境的备选音频播放方法
  2775. tryMiniProgramFallbackAudioPlay() {
  2776. console.log("使用小程序备选方法播放音频:", this.aiVoiceUrl);
  2777. common_vendor.index.downloadFile({
  2778. url: this.aiVoiceUrl,
  2779. success: (res) => {
  2780. if (res.statusCode === 200) {
  2781. console.log("小程序音频文件下载成功:", res.tempFilePath);
  2782. const newAudioContext = common_vendor.wx$1.createInnerAudioContext();
  2783. newAudioContext.autoplay = true;
  2784. newAudioContext.src = res.tempFilePath;
  2785. newAudioContext.onPlay(() => {
  2786. console.log("小程序备选方法:音频开始播放");
  2787. });
  2788. newAudioContext.onEnded(() => {
  2789. console.log("小程序备选方法:音频播放结束");
  2790. this.isPlayingAiVoice = false;
  2791. this.currentSubtitle = "";
  2792. newAudioContext.destroy();
  2793. this.showContinueQuestionOptions();
  2794. this.checkPendingNavigation();
  2795. });
  2796. newAudioContext.onError((err) => {
  2797. console.error("小程序备选方法:音频播放错误:", err);
  2798. this.isPlayingAiVoice = false;
  2799. this.currentSubtitle = "";
  2800. newAudioContext.destroy();
  2801. this.trySystemAudioPlayer();
  2802. });
  2803. } else {
  2804. console.error("小程序音频文件下载失败:", res);
  2805. this.handleAudioPlaybackFailure();
  2806. }
  2807. },
  2808. fail: (err) => {
  2809. console.error("小程序音频文件下载请求失败:", err);
  2810. this.handleAudioPlaybackFailure();
  2811. }
  2812. });
  2813. },
  2814. // 添加新方法:尝试使用系统播放器
  2815. trySystemAudioPlayer() {
  2816. console.log("尝试使用系统播放器播放音频");
  2817. const systemInfo = common_vendor.index.getSystemInfoSync();
  2818. if (systemInfo.platform === "android" || systemInfo.platform === "ios") {
  2819. if (typeof common_vendor.index.playVoice === "function") {
  2820. common_vendor.index.playVoice({
  2821. filePath: this.aiVoiceUrl,
  2822. success: () => {
  2823. console.log("系统播放器播放成功");
  2824. setTimeout(() => {
  2825. this.isPlayingAiVoice = false;
  2826. this.currentSubtitle = "";
  2827. this.showContinueQuestionOptions();
  2828. }, 5e3);
  2829. },
  2830. fail: (err) => {
  2831. console.error("系统播放器播放失败:", err);
  2832. this.handleAudioPlaybackFailure();
  2833. }
  2834. });
  2835. } else {
  2836. common_vendor.index.openDocument({
  2837. filePath: this.aiVoiceUrl,
  2838. fileType: "audio",
  2839. success: () => {
  2840. console.log("使用系统应用打开音频文件");
  2841. setTimeout(() => {
  2842. this.isPlayingAiVoice = false;
  2843. this.currentSubtitle = "";
  2844. this.showContinueQuestionOptions();
  2845. }, 8e3);
  2846. },
  2847. fail: (err) => {
  2848. console.error("无法使用系统应用打开音频:", err);
  2849. this.handleAudioPlaybackFailure();
  2850. }
  2851. });
  2852. }
  2853. } else {
  2854. this.handleAudioPlaybackFailure();
  2855. }
  2856. },
  2857. // 修改 showContinueQuestionOptions 方法,确保状态正确设置
  2858. showContinueQuestionOptions() {
  2859. if (this.isPlayingAiVoice) {
  2860. console.log("AI语音正在播放,暂不显示继续提问选项");
  2861. return;
  2862. }
  2863. this.questionRound++;
  2864. this.currentSubtitle = "";
  2865. console.log("准备跳转到下一页面");
  2866. setTimeout(() => {
  2867. this.navigateToNextPage();
  2868. }, 3e3);
  2869. this.isRecording = false;
  2870. if (this.recordingTimer) {
  2871. clearInterval(this.recordingTimer);
  2872. this.recordingTimer = null;
  2873. }
  2874. this.recordedChunks = [];
  2875. this.recordingTimerCount = 0;
  2876. this.remainingTime = this.maxRecordingTime;
  2877. },
  2878. // 修复 handleContinueQuestionClick 方法,确保状态正确重置
  2879. handleContinueQuestionClick() {
  2880. if (this.isPlayingAiVoice) {
  2881. return;
  2882. }
  2883. this.showContinueQuestionButton = false;
  2884. this.showEndInterviewButton = false;
  2885. this.isRecording = false;
  2886. this.recordingTimerCount = 0;
  2887. this.remainingTime = this.maxRecordingTime;
  2888. this.recordingTimeDisplay = "00:00 / " + this.formatTime(this.maxRecordingTime);
  2889. this.recordedChunks = [];
  2890. if (this.mediaRecorder && this.mediaRecorder.state === "recording") {
  2891. this.mediaRecorder.stop();
  2892. }
  2893. if (this.recorder) {
  2894. try {
  2895. this.cameraContext.stopRecord();
  2896. } catch (e) {
  2897. console.error("停止录制失败:", e);
  2898. }
  2899. this.recorder = null;
  2900. }
  2901. setTimeout(() => {
  2902. this.startRecordingAnswer();
  2903. }, 500);
  2904. },
  2905. // 添加新方法:处理结束面试按钮点击
  2906. handleEndInterviewClick() {
  2907. this.showContinueQuestionButton = false;
  2908. this.showEndInterviewButton = false;
  2909. this.navigateToNextPage();
  2910. },
  2911. // 添加新方法:处理录制错误
  2912. handleRecordingError(error) {
  2913. console.error("录制错误:", error);
  2914. this.isRecording = false;
  2915. this.showStopRecordingButton = false;
  2916. this.showStartRecordingButton = true;
  2917. },
  2918. // 添加新方法:处理上传错误
  2919. handleUploadError(error) {
  2920. console.error("上传错误:", error);
  2921. this.showRetryButton = true;
  2922. this.showUploadStatus = false;
  2923. this.isUploading = false;
  2924. },
  2925. // 添加新方法:检查是否有待处理的自动跳转
  2926. checkPendingNavigation() {
  2927. if (this.pendingAutoNavigation && !this.isPlayingAiVoice && !this.isUploading && !this.currentSubtitle) {
  2928. console.log("所有处理已完成,准备跳转");
  2929. this.pendingAutoNavigation = false;
  2930. setTimeout(() => {
  2931. this.navigateToNextPage();
  2932. }, 1e3);
  2933. }
  2934. },
  2935. // 添加新方法:显示文本回复
  2936. displayTextResponse() {
  2937. this.isPlayingAiVoice = true;
  2938. this.currentSubtitle = "";
  2939. const charDisplayTime = 200;
  2940. let currentIndex = 0;
  2941. const textInterval = setInterval(() => {
  2942. if (currentIndex <= this.aiText.length) {
  2943. this.currentSubtitle = this.aiText.substring(0, currentIndex);
  2944. currentIndex++;
  2945. } else {
  2946. clearInterval(textInterval);
  2947. setTimeout(() => {
  2948. this.isPlayingAiVoice = false;
  2949. this.currentSubtitle = "";
  2950. this.showContinueQuestionOptions();
  2951. this.checkPendingNavigation();
  2952. }, 3e3);
  2953. }
  2954. }, charDisplayTime);
  2955. },
  2956. // 添加新方法:检查不支持的音频格式
  2957. checkUnsupportedAudioFormat(url) {
  2958. if (!url)
  2959. return false;
  2960. const extension = url.split(".").pop().toLowerCase();
  2961. const systemInfo = common_vendor.index.getSystemInfoSync();
  2962. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  2963. if (isMiniProgram) {
  2964. const unsupportedFormats = ["ogg", "flac", "aac", "m4a", "opus"];
  2965. return unsupportedFormats.includes(extension);
  2966. }
  2967. return false;
  2968. },
  2969. // 修改文本回退方法
  2970. useIOSTextFallback() {
  2971. console.log("切换到文本显示模式");
  2972. this.isPlayingAiVoice = false;
  2973. if (!this.hasShownTextFallbackToast) {
  2974. this.hasShownTextFallbackToast = true;
  2975. }
  2976. this.currentSubtitle = this.aiText || "抱歉,无法获取AI回复内容";
  2977. const textLength = this.aiText ? this.aiText.length : 20;
  2978. const readingTime = Math.max(8e3, Math.min(textLength * 150, 2e4));
  2979. console.log(`文本长度: ${textLength}字,预计阅读时间: ${readingTime / 1e3}秒`);
  2980. setTimeout(() => {
  2981. this.navigateToNextPage();
  2982. }, readingTime);
  2983. }
  2984. }
  2985. };
  2986. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  2987. return common_vendor.e({
  2988. a: $data.videoUrl,
  2989. b: common_vendor.o((...args) => $options.handleVideoError && $options.handleVideoError(...args)),
  2990. c: common_vendor.o((...args) => $options.handleVideoEnded && $options.handleVideoEnded(...args)),
  2991. d: common_vendor.o((...args) => $options.handleTimeUpdate && $options.handleTimeUpdate(...args)),
  2992. e: $data.showAnswerButton
  2993. }, $data.showAnswerButton ? {
  2994. f: common_vendor.o((...args) => $options.handleAnswerButtonClick && $options.handleAnswerButtonClick(...args))
  2995. } : {}, {
  2996. g: $data.currentSubtitle
  2997. }, $data.currentSubtitle ? {
  2998. h: common_vendor.t($data.currentSubtitle)
  2999. } : {}, {
  3000. i: $data.useMiniProgramCameraComponent
  3001. }, $data.useMiniProgramCameraComponent ? {
  3002. j: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  3003. } : {}, {
  3004. k: $data.loading
  3005. }, $data.loading ? {} : {}, {
  3006. l: $data.showDebugInfo
  3007. }, $data.showDebugInfo ? common_vendor.e({
  3008. m: $data.assistantResponse
  3009. }, $data.assistantResponse ? {
  3010. n: common_vendor.t($data.assistantResponse)
  3011. } : {}, {
  3012. o: $data.audioTranscript
  3013. }, $data.audioTranscript ? {
  3014. p: common_vendor.t($data.audioTranscript)
  3015. } : {}, {
  3016. q: common_vendor.f($data.processedResponses, (item, index, i0) => {
  3017. return common_vendor.e({
  3018. a: item.role
  3019. }, item.role ? {
  3020. b: common_vendor.t(item.role)
  3021. } : {}, {
  3022. c: item.transcript
  3023. }, item.transcript ? {
  3024. d: common_vendor.t(item.transcript)
  3025. } : {}, {
  3026. e: index
  3027. });
  3028. })
  3029. }) : {}, {
  3030. r: $data.showStopRecordingButton
  3031. }, $data.showStopRecordingButton ? {
  3032. s: common_vendor.o((...args) => $options.stopRecordingAnswer && $options.stopRecordingAnswer(...args))
  3033. } : {}, {
  3034. t: $data.isRecording
  3035. }, $data.isRecording ? {
  3036. v: common_vendor.t($data.recordingTimeDisplay || "00:00 / 05:00")
  3037. } : {}, {
  3038. w: $data.showStartRecordingButton
  3039. }, $data.showStartRecordingButton ? {
  3040. x: common_vendor.o((...args) => $options.handleStartRecordingClick && $options.handleStartRecordingClick(...args))
  3041. } : {}, {
  3042. y: $data.showRetryButton
  3043. }, $data.showRetryButton ? {
  3044. z: common_vendor.o((...args) => $options.retryVideoUpload && $options.retryVideoUpload(...args))
  3045. } : {}, {
  3046. A: $data.showCountdown
  3047. }, $data.showCountdown ? {
  3048. B: common_vendor.t($data.countdownValue)
  3049. } : {}, {
  3050. C: $data.isPlayingAiVoice
  3051. }, $data.isPlayingAiVoice ? {} : {}, {
  3052. D: $data.showContinueQuestionButton
  3053. }, $data.showContinueQuestionButton ? {
  3054. E: common_vendor.o((...args) => $options.handleContinueQuestionClick && $options.handleContinueQuestionClick(...args))
  3055. } : {});
  3056. }
  3057. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-2a02c54e"]]);
  3058. wx.createPage(MiniProgramPage);