interview-question.js 95 KB

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