interview-question.js 113 KB

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