interview-question.js 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  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. common_vendor.index.showToast({
  2052. title: "音频加载失败",
  2053. icon: "none",
  2054. duration: 2e3
  2055. });
  2056. this.isPlayingAiVoice = false;
  2057. this.showContinueQuestionOptions();
  2058. return;
  2059. }
  2060. try {
  2061. if (this.aiAudioPlayer) {
  2062. this.aiAudioPlayer.destroy();
  2063. }
  2064. this.aiAudioPlayer = common_vendor.index.createInnerAudioContext();
  2065. this.aiAudioPlayer.autoplay = false;
  2066. this.aiAudioPlayer.obeyMuteSwitch = false;
  2067. this.aiAudioPlayer.onPlay(() => {
  2068. console.log("AI语音开始播放");
  2069. common_vendor.index.showToast({
  2070. title: "正在播放面试官的回复",
  2071. icon: "none",
  2072. duration: 2e3
  2073. });
  2074. });
  2075. this.aiAudioPlayer.onEnded(() => {
  2076. console.log("AI语音播放结束");
  2077. this.isPlayingAiVoice = false;
  2078. this.currentSubtitle = "";
  2079. if (this.aiAudioPlayer) {
  2080. this.aiAudioPlayer.destroy();
  2081. this.aiAudioPlayer = null;
  2082. }
  2083. this.showContinueQuestionOptions();
  2084. this.checkPendingNavigation();
  2085. });
  2086. this.aiAudioPlayer.onError((err) => {
  2087. console.error("AI语音播放错误:", err);
  2088. if (!this.hasTriedFallbackMethod) {
  2089. this.hasTriedFallbackMethod = true;
  2090. console.log("尝试使用备选方法播放音频");
  2091. this.tryFallbackAudioPlay();
  2092. return;
  2093. }
  2094. this.isPlayingAiVoice = false;
  2095. this.currentSubtitle = "";
  2096. if (this.aiAudioPlayer) {
  2097. this.aiAudioPlayer.destroy();
  2098. this.aiAudioPlayer = null;
  2099. }
  2100. common_vendor.index.showToast({
  2101. title: "音频播放失败,请查看文字回复",
  2102. icon: "none",
  2103. duration: 2e3
  2104. });
  2105. this.showContinueQuestionOptions();
  2106. this.checkPendingNavigation();
  2107. });
  2108. this.aiAudioPlayer.onCanplay(() => {
  2109. console.log("音频已准备好播放");
  2110. });
  2111. this.aiAudioPlayer.src = this.aiVoiceUrl;
  2112. const playTimeout = setTimeout(() => {
  2113. if (this.isPlayingAiVoice && this.aiAudioPlayer) {
  2114. console.warn("音频播放超时,尝试备选方法");
  2115. this.tryFallbackAudioPlay();
  2116. }
  2117. }, 5e3);
  2118. setTimeout(() => {
  2119. if (this.aiAudioPlayer) {
  2120. this.aiAudioPlayer.play();
  2121. clearTimeout(playTimeout);
  2122. }
  2123. }, 300);
  2124. } catch (e) {
  2125. console.error("播放AI语音时发生错误:", e);
  2126. this.isPlayingAiVoice = false;
  2127. this.currentSubtitle = "";
  2128. if (this.aiAudioPlayer) {
  2129. try {
  2130. this.aiAudioPlayer.destroy();
  2131. } catch (destroyError) {
  2132. console.error("销毁音频播放器失败:", destroyError);
  2133. }
  2134. this.aiAudioPlayer = null;
  2135. }
  2136. common_vendor.index.showToast({
  2137. title: "音频播放失败,请查看文字回复",
  2138. icon: "none",
  2139. duration: 2e3
  2140. });
  2141. this.showContinueQuestionOptions();
  2142. this.checkPendingNavigation();
  2143. }
  2144. },
  2145. // 添加新方法:尝试备选音频播放方法
  2146. tryFallbackAudioPlay() {
  2147. console.log("使用备选方法播放音频:", this.aiVoiceUrl);
  2148. if (this.aiAudioPlayer) {
  2149. try {
  2150. this.aiAudioPlayer.destroy();
  2151. } catch (e) {
  2152. console.error("销毁音频播放器失败:", e);
  2153. }
  2154. this.aiAudioPlayer = null;
  2155. }
  2156. common_vendor.index.downloadFile({
  2157. url: this.aiVoiceUrl,
  2158. success: (res) => {
  2159. if (res.statusCode === 200) {
  2160. console.log("音频文件下载成功:", res.tempFilePath);
  2161. const newAudioPlayer = common_vendor.index.createInnerAudioContext();
  2162. this.aiAudioPlayer = newAudioPlayer;
  2163. newAudioPlayer.autoplay = true;
  2164. newAudioPlayer.src = res.tempFilePath;
  2165. newAudioPlayer.onPlay(() => {
  2166. console.log("备选方法:音频开始播放");
  2167. });
  2168. newAudioPlayer.onEnded(() => {
  2169. console.log("备选方法:音频播放结束");
  2170. this.isPlayingAiVoice = false;
  2171. this.currentSubtitle = "";
  2172. newAudioPlayer.destroy();
  2173. this.aiAudioPlayer = null;
  2174. this.showContinueQuestionOptions();
  2175. this.checkPendingNavigation();
  2176. });
  2177. newAudioPlayer.onError((err) => {
  2178. console.error("备选方法:音频播放错误:", err);
  2179. this.isPlayingAiVoice = false;
  2180. this.currentSubtitle = "";
  2181. newAudioPlayer.destroy();
  2182. this.aiAudioPlayer = null;
  2183. common_vendor.index.showToast({
  2184. title: "无法播放音频,请查看文字回复",
  2185. icon: "none",
  2186. duration: 2e3
  2187. });
  2188. this.showContinueQuestionOptions();
  2189. this.checkPendingNavigation();
  2190. });
  2191. } else {
  2192. console.error("音频文件下载失败:", res);
  2193. this.handleAudioPlaybackFailure();
  2194. }
  2195. },
  2196. fail: (err) => {
  2197. console.error("音频文件下载请求失败:", err);
  2198. this.handleAudioPlaybackFailure();
  2199. }
  2200. });
  2201. },
  2202. // 添加新方法:处理音频播放失败的情况
  2203. handleAudioPlaybackFailure() {
  2204. this.isPlayingAiVoice = false;
  2205. common_vendor.index.showToast({
  2206. title: "无法播放音频,将直接显示文字回复",
  2207. icon: "none",
  2208. duration: 2e3
  2209. });
  2210. this.currentSubtitle = this.aiText;
  2211. setTimeout(() => {
  2212. this.currentSubtitle = "";
  2213. this.showContinueQuestionOptions();
  2214. this.checkPendingNavigation();
  2215. }, 1e4);
  2216. },
  2217. // 添加新方法:从视频中提取音频
  2218. async extractAudioFromVideo(videoBlob) {
  2219. console.log("开始从视频中提取音频");
  2220. return new Promise((resolve, reject) => {
  2221. try {
  2222. const videoElement = document.createElement("video");
  2223. videoElement.muted = false;
  2224. videoElement.autoplay = false;
  2225. const audioContext = new (window.AudioContext || window.webkitAudioContext)();
  2226. let audioDestination = null;
  2227. let mediaRecorder = null;
  2228. const audioChunks = [];
  2229. videoElement.src = URL.createObjectURL(videoBlob);
  2230. videoElement.onloadedmetadata = () => {
  2231. console.log("视频元数据已加载,视频时长:", videoElement.duration);
  2232. const source = audioContext.createMediaElementSource(videoElement);
  2233. audioDestination = audioContext.createMediaStreamDestination();
  2234. source.connect(audioDestination);
  2235. mediaRecorder = new MediaRecorder(audioDestination.stream);
  2236. mediaRecorder.ondataavailable = (event) => {
  2237. if (event.data.size > 0) {
  2238. audioChunks.push(event.data);
  2239. }
  2240. };
  2241. mediaRecorder.onstop = () => {
  2242. const audioBlob = new Blob(audioChunks, { type: "audio/wav" });
  2243. console.log("音频提取完成,大小:", audioBlob.size);
  2244. resolve(audioBlob);
  2245. };
  2246. videoElement.onplay = () => {
  2247. mediaRecorder.start(100);
  2248. };
  2249. videoElement.onended = () => {
  2250. mediaRecorder.stop();
  2251. audioContext.close();
  2252. };
  2253. videoElement.play();
  2254. };
  2255. videoElement.onerror = (err) => {
  2256. console.error("视频加载失败:", err);
  2257. reject(new Error("视频加载失败"));
  2258. };
  2259. } catch (err) {
  2260. console.error("提取音频失败:", err);
  2261. reject(err);
  2262. }
  2263. });
  2264. },
  2265. // 添加新方法:上传音频文件
  2266. async uploadAudioFile(audioBlob, videoUrl) {
  2267. console.log("准备上传音频文件");
  2268. const formData = new FormData();
  2269. const fileName = `audio_${Date.now()}.wav`;
  2270. const audioFile = new File([audioBlob], fileName, { type: "audio/wav" });
  2271. formData.append("audio_file", audioFile);
  2272. formData.append("video_url", videoUrl);
  2273. formData.append("tenant_id", common_vendor.index.getStorageSync("tenant_id") || "1");
  2274. formData.append("openid", common_vendor.index.getStorageSync("userInfo") ? JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid : "");
  2275. formData.append("application_id", common_vendor.index.getStorageSync("appId") || "");
  2276. return new Promise((resolve, reject) => {
  2277. const xhr = new XMLHttpRequest();
  2278. xhr.open("POST", `${common_config.apiBaseUrl}/api/voice_ai_interaction`, true);
  2279. xhr.timeout = 6e4;
  2280. xhr.onload = () => {
  2281. if (xhr.status === 200) {
  2282. try {
  2283. const response = JSON.parse(xhr.responseText);
  2284. resolve(response);
  2285. } catch (e) {
  2286. reject(new Error("解析响应失败"));
  2287. }
  2288. } else {
  2289. reject(new Error("HTTP状态: " + xhr.status));
  2290. }
  2291. };
  2292. xhr.onerror = () => {
  2293. reject(new Error("网络错误"));
  2294. };
  2295. xhr.ontimeout = () => {
  2296. reject(new Error("请求超时"));
  2297. };
  2298. xhr.send(formData);
  2299. });
  2300. },
  2301. // 添加一个新方法:使用小程序原生音频API
  2302. playMiniProgramAudio() {
  2303. console.log("使用小程序原生音频API播放:", this.aiVoiceUrl);
  2304. this.isPlayingAiVoice = true;
  2305. this.showContinueQuestionButton = false;
  2306. this.showEndInterviewButton = false;
  2307. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === "") {
  2308. console.error("无效的音频URL");
  2309. common_vendor.index.showToast({
  2310. title: "音频加载失败",
  2311. icon: "none",
  2312. duration: 2e3
  2313. });
  2314. this.isPlayingAiVoice = false;
  2315. this.showContinueQuestionOptions();
  2316. return;
  2317. }
  2318. try {
  2319. const audioContext = common_vendor.wx$1.createInnerAudioContext();
  2320. audioContext.autoplay = false;
  2321. audioContext.obeyMuteSwitch = false;
  2322. audioContext.onPlay(() => {
  2323. console.log("AI语音开始播放");
  2324. common_vendor.index.showToast({
  2325. title: "正在播放AI回复",
  2326. icon: "none",
  2327. duration: 2e3
  2328. });
  2329. });
  2330. audioContext.onEnded(() => {
  2331. console.log("AI语音播放结束");
  2332. this.isPlayingAiVoice = false;
  2333. this.currentSubtitle = "";
  2334. audioContext.destroy();
  2335. this.showContinueQuestionOptions();
  2336. });
  2337. audioContext.onError((err) => {
  2338. console.error("AI语音播放错误:", err);
  2339. this.isPlayingAiVoice = false;
  2340. this.currentSubtitle = "";
  2341. audioContext.destroy();
  2342. this.showContinueQuestionOptions();
  2343. });
  2344. audioContext.src = this.aiVoiceUrl;
  2345. const playTimeout = setTimeout(() => {
  2346. if (this.isPlayingAiVoice) {
  2347. console.warn("小程序音频播放超时,尝试备选方法");
  2348. this.tryMiniProgramFallbackAudioPlay();
  2349. }
  2350. }, 5e3);
  2351. setTimeout(() => {
  2352. audioContext.play();
  2353. clearTimeout(playTimeout);
  2354. }, 300);
  2355. } catch (e) {
  2356. console.error("小程序播放AI语音时发生错误:", e);
  2357. this.isPlayingAiVoice = false;
  2358. common_vendor.index.showToast({
  2359. title: "音频播放失败,请查看文字回复",
  2360. icon: "none",
  2361. duration: 2e3
  2362. });
  2363. this.handleAudioPlaybackFailure();
  2364. }
  2365. },
  2366. // 添加新方法:小程序环境的备选音频播放方法
  2367. tryMiniProgramFallbackAudioPlay() {
  2368. console.log("使用小程序备选方法播放音频:", this.aiVoiceUrl);
  2369. common_vendor.index.downloadFile({
  2370. url: this.aiVoiceUrl,
  2371. success: (res) => {
  2372. if (res.statusCode === 200) {
  2373. console.log("小程序音频文件下载成功:", res.tempFilePath);
  2374. const newAudioContext = common_vendor.wx$1.createInnerAudioContext();
  2375. newAudioContext.autoplay = true;
  2376. newAudioContext.src = res.tempFilePath;
  2377. newAudioContext.onPlay(() => {
  2378. console.log("小程序备选方法:音频开始播放");
  2379. });
  2380. newAudioContext.onEnded(() => {
  2381. console.log("小程序备选方法:音频播放结束");
  2382. this.isPlayingAiVoice = false;
  2383. this.currentSubtitle = "";
  2384. newAudioContext.destroy();
  2385. this.showContinueQuestionOptions();
  2386. this.checkPendingNavigation();
  2387. });
  2388. newAudioContext.onError((err) => {
  2389. console.error("小程序备选方法:音频播放错误:", err);
  2390. this.isPlayingAiVoice = false;
  2391. this.currentSubtitle = "";
  2392. newAudioContext.destroy();
  2393. this.trySystemAudioPlayer();
  2394. });
  2395. } else {
  2396. console.error("小程序音频文件下载失败:", res);
  2397. this.handleAudioPlaybackFailure();
  2398. }
  2399. },
  2400. fail: (err) => {
  2401. console.error("小程序音频文件下载请求失败:", err);
  2402. this.handleAudioPlaybackFailure();
  2403. }
  2404. });
  2405. },
  2406. // 添加新方法:尝试使用系统播放器
  2407. trySystemAudioPlayer() {
  2408. console.log("尝试使用系统播放器播放音频");
  2409. const systemInfo = common_vendor.index.getSystemInfoSync();
  2410. if (systemInfo.platform === "android" || systemInfo.platform === "ios") {
  2411. common_vendor.index.showToast({
  2412. title: "正在使用系统播放器",
  2413. icon: "none",
  2414. duration: 2e3
  2415. });
  2416. if (typeof common_vendor.index.playVoice === "function") {
  2417. common_vendor.index.playVoice({
  2418. filePath: this.aiVoiceUrl,
  2419. success: () => {
  2420. console.log("系统播放器播放成功");
  2421. setTimeout(() => {
  2422. this.isPlayingAiVoice = false;
  2423. this.currentSubtitle = "";
  2424. this.showContinueQuestionOptions();
  2425. }, 5e3);
  2426. },
  2427. fail: (err) => {
  2428. console.error("系统播放器播放失败:", err);
  2429. this.handleAudioPlaybackFailure();
  2430. }
  2431. });
  2432. } else {
  2433. common_vendor.index.openDocument({
  2434. filePath: this.aiVoiceUrl,
  2435. fileType: "audio",
  2436. success: () => {
  2437. console.log("使用系统应用打开音频文件");
  2438. setTimeout(() => {
  2439. this.isPlayingAiVoice = false;
  2440. this.currentSubtitle = "";
  2441. this.showContinueQuestionOptions();
  2442. }, 8e3);
  2443. },
  2444. fail: (err) => {
  2445. console.error("无法使用系统应用打开音频:", err);
  2446. this.handleAudioPlaybackFailure();
  2447. }
  2448. });
  2449. }
  2450. } else {
  2451. this.handleAudioPlaybackFailure();
  2452. }
  2453. },
  2454. // 修改 showContinueQuestionOptions 方法,确保状态正确设置
  2455. showContinueQuestionOptions() {
  2456. if (this.isPlayingAiVoice) {
  2457. console.log("AI语音正在播放,暂不显示继续提问选项");
  2458. return;
  2459. }
  2460. this.questionRound++;
  2461. this.currentSubtitle = "";
  2462. if (this.questionRound < this.maxQuestionRounds) {
  2463. this.showContinueQuestionButton = true;
  2464. this.showEndInterviewButton = true;
  2465. common_vendor.index.showToast({
  2466. title: `第${this.questionRound}轮提问完成`,
  2467. icon: "none",
  2468. duration: 2e3
  2469. });
  2470. } else {
  2471. this.showContinueQuestionButton = false;
  2472. this.showEndInterviewButton = false;
  2473. if (this.isPlayingAiVoice || this.isUploading) {
  2474. console.log("等待AI回复或上传完成后再跳转");
  2475. this.pendingAutoNavigation = true;
  2476. } else {
  2477. setTimeout(() => {
  2478. this.navigateToNextPage();
  2479. }, 3e3);
  2480. }
  2481. }
  2482. this.isRecording = false;
  2483. if (this.recordingTimer) {
  2484. clearInterval(this.recordingTimer);
  2485. this.recordingTimer = null;
  2486. }
  2487. this.recordedChunks = [];
  2488. this.recordingTimerCount = 0;
  2489. this.remainingTime = this.maxRecordingTime;
  2490. },
  2491. // 修复 handleContinueQuestionClick 方法,确保状态正确重置
  2492. handleContinueQuestionClick() {
  2493. if (this.isPlayingAiVoice) {
  2494. common_vendor.index.showToast({
  2495. title: "请等待面试官回复完成",
  2496. icon: "none",
  2497. duration: 2e3
  2498. });
  2499. return;
  2500. }
  2501. this.showContinueQuestionButton = false;
  2502. this.showEndInterviewButton = false;
  2503. this.isRecording = false;
  2504. this.recordingTimerCount = 0;
  2505. this.remainingTime = this.maxRecordingTime;
  2506. this.recordingTimeDisplay = "00:00 / " + this.formatTime(this.maxRecordingTime);
  2507. this.recordedChunks = [];
  2508. if (this.mediaRecorder && this.mediaRecorder.state === "recording") {
  2509. this.mediaRecorder.stop();
  2510. }
  2511. if (this.recorder) {
  2512. try {
  2513. this.cameraContext.stopRecord();
  2514. } catch (e) {
  2515. console.error("停止录制失败:", e);
  2516. }
  2517. this.recorder = null;
  2518. }
  2519. setTimeout(() => {
  2520. this.startRecordingAnswer();
  2521. }, 500);
  2522. },
  2523. // 添加新方法:处理结束面试按钮点击
  2524. handleEndInterviewClick() {
  2525. this.showContinueQuestionButton = false;
  2526. this.showEndInterviewButton = false;
  2527. this.navigateToNextPage();
  2528. },
  2529. // 添加新方法:处理录制错误
  2530. handleRecordingError(error) {
  2531. console.error("录制错误:", error);
  2532. common_vendor.index.showToast({
  2533. title: "录制失败,请重试",
  2534. icon: "none",
  2535. duration: 2e3
  2536. });
  2537. this.isRecording = false;
  2538. this.showStopRecordingButton = false;
  2539. this.showStartRecordingButton = true;
  2540. },
  2541. // 添加新方法:处理上传错误
  2542. handleUploadError(error) {
  2543. console.error("上传错误:", error);
  2544. common_vendor.index.showToast({
  2545. title: "上传失败,请重试",
  2546. icon: "none",
  2547. duration: 2e3
  2548. });
  2549. this.showRetryButton = true;
  2550. this.showUploadStatus = false;
  2551. this.isUploading = false;
  2552. },
  2553. // 添加新方法:检查是否有待处理的自动跳转
  2554. checkPendingNavigation() {
  2555. if (this.pendingAutoNavigation && !this.isPlayingAiVoice && !this.isUploading) {
  2556. console.log("所有处理已完成,执行自动跳转");
  2557. this.pendingAutoNavigation = false;
  2558. setTimeout(() => {
  2559. this.navigateToNextPage();
  2560. }, 2e3);
  2561. }
  2562. },
  2563. // 添加新方法:显示文本回复
  2564. displayTextResponse() {
  2565. this.isPlayingAiVoice = true;
  2566. this.currentSubtitle = this.aiText;
  2567. common_vendor.index.showToast({
  2568. title: "显示面试官文字回复",
  2569. icon: "none",
  2570. duration: 2e3
  2571. });
  2572. const textLength = this.aiText.length;
  2573. const displayTime = Math.min(Math.max(textLength * 100, 5e3), 15e3);
  2574. setTimeout(() => {
  2575. this.isPlayingAiVoice = false;
  2576. this.currentSubtitle = "";
  2577. this.showContinueQuestionOptions();
  2578. this.checkPendingNavigation();
  2579. }, displayTime);
  2580. },
  2581. // 添加新方法:检查不支持的音频格式
  2582. checkUnsupportedAudioFormat(url) {
  2583. if (!url)
  2584. return false;
  2585. const extension = url.split(".").pop().toLowerCase();
  2586. const systemInfo = common_vendor.index.getSystemInfoSync();
  2587. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith("mp-");
  2588. if (isMiniProgram) {
  2589. const unsupportedFormats = ["ogg", "flac", "aac", "m4a", "opus"];
  2590. return unsupportedFormats.includes(extension);
  2591. }
  2592. return false;
  2593. }
  2594. }
  2595. };
  2596. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  2597. return common_vendor.e({
  2598. a: $data.videoUrl,
  2599. b: common_vendor.o((...args) => $options.handleVideoError && $options.handleVideoError(...args)),
  2600. c: common_vendor.o((...args) => $options.handleVideoEnded && $options.handleVideoEnded(...args)),
  2601. d: common_vendor.o((...args) => $options.handleTimeUpdate && $options.handleTimeUpdate(...args)),
  2602. e: $data.showAnswerButton
  2603. }, $data.showAnswerButton ? {
  2604. f: common_vendor.o((...args) => $options.handleAnswerButtonClick && $options.handleAnswerButtonClick(...args))
  2605. } : {}, {
  2606. g: $data.currentSubtitle
  2607. }, $data.currentSubtitle ? {
  2608. h: common_vendor.t($data.currentSubtitle)
  2609. } : {}, {
  2610. i: $data.useMiniProgramCameraComponent
  2611. }, $data.useMiniProgramCameraComponent ? {
  2612. j: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  2613. } : {}, {
  2614. k: $data.loading
  2615. }, $data.loading ? {} : {}, {
  2616. l: $data.showDebugInfo
  2617. }, $data.showDebugInfo ? common_vendor.e({
  2618. m: $data.assistantResponse
  2619. }, $data.assistantResponse ? {
  2620. n: common_vendor.t($data.assistantResponse)
  2621. } : {}, {
  2622. o: $data.audioTranscript
  2623. }, $data.audioTranscript ? {
  2624. p: common_vendor.t($data.audioTranscript)
  2625. } : {}, {
  2626. q: common_vendor.f($data.processedResponses, (item, index, i0) => {
  2627. return common_vendor.e({
  2628. a: item.role
  2629. }, item.role ? {
  2630. b: common_vendor.t(item.role)
  2631. } : {}, {
  2632. c: item.transcript
  2633. }, item.transcript ? {
  2634. d: common_vendor.t(item.transcript)
  2635. } : {}, {
  2636. e: index
  2637. });
  2638. })
  2639. }) : {}, {
  2640. r: $data.showStopRecordingButton
  2641. }, $data.showStopRecordingButton ? {
  2642. s: common_vendor.o((...args) => $options.stopRecordingAnswer && $options.stopRecordingAnswer(...args))
  2643. } : {}, {
  2644. t: $data.isRecording
  2645. }, $data.isRecording ? {
  2646. v: common_vendor.t($data.recordingTimeDisplay || "00:00 / 05:00")
  2647. } : {}, {
  2648. w: $data.showStartRecordingButton
  2649. }, $data.showStartRecordingButton ? {
  2650. x: common_vendor.o((...args) => $options.handleStartRecordingClick && $options.handleStartRecordingClick(...args))
  2651. } : {}, {
  2652. y: $data.showRetryButton
  2653. }, $data.showRetryButton ? {
  2654. z: common_vendor.o((...args) => $options.retryVideoUpload && $options.retryVideoUpload(...args))
  2655. } : {}, {
  2656. A: $data.showCountdown
  2657. }, $data.showCountdown ? {
  2658. B: common_vendor.t($data.countdownValue)
  2659. } : {}, {
  2660. C: $data.isPlayingAiVoice
  2661. }, $data.isPlayingAiVoice ? {} : {}, {
  2662. D: $data.showContinueQuestionButton
  2663. }, $data.showContinueQuestionButton ? {
  2664. E: common_vendor.o((...args) => $options.handleContinueQuestionClick && $options.handleContinueQuestionClick(...args))
  2665. } : {});
  2666. }
  2667. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-2a02c54e"]]);
  2668. wx.createPage(MiniProgramPage);