identity-verify.vue 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. <template>
  2. <div class="identity-verify-container">
  3. <div class="digital-human-container">
  4. <!-- AI数字人视频/图像显示区域 -->
  5. <div class="digital-human-video">
  6. <!-- <image v-if="!videoPlaying" src="/static/images/digital-human-placeholder.jpg" mode="aspectFit"></image> -->
  7. <video
  8. :src="videoUrl"
  9. id="myVideo"
  10. ref="videoPlayer"
  11. autoplay
  12. playsinline
  13. disablePictureInPicture
  14. controlsList="nodownload nofullscreen noremoteplayback"
  15. class="video-player"
  16. :controls="false"
  17. @error="handleVideoError"
  18. @ended="handleVideoEnded"
  19. @timeupdate="handleTimeUpdate">
  20. </video>
  21. <!-- 添加字幕覆盖层 -->
  22. <div class="subtitle-overlay" v-if="currentSubtitle">
  23. {{ currentSubtitle }}
  24. </div>
  25. <!-- 添加答题按钮 -->
  26. <div class="answer-button-container" v-if="showAnswerButton">
  27. <button class="answer-button" @click="handleAnswerButtonClick">
  28. 开始面试
  29. </button>
  30. </div>
  31. </div>
  32. <!-- 用户摄像头视频显示区域 -->
  33. <div class="user-camera-container">
  34. <!-- 在小程序环境中使用camera组件 -->
  35. <camera v-if="useMiniProgramCameraComponent"
  36. device-position="front"
  37. flash="off"
  38. class="user-camera-video"
  39. @error="handleCameraError"
  40. :record-audio="true">
  41. </camera>
  42. <!-- 在H5/App环境中使用video元素 -->
  43. <video v-else
  44. id="userCamera"
  45. ref="userCameraVideo"
  46. autoplay
  47. playsinline
  48. muted
  49. class="user-camera-video"
  50. :controls="false">
  51. </video>
  52. </div>
  53. <!-- 字幕/文本覆盖区域 -->
  54. <!-- <div class="subtitle-overlay" v-if="assistantResponse">
  55. {{ assistantResponse }}
  56. </div> -->
  57. </div>
  58. <!-- 加载状态 -->
  59. <div v-if="loading" class="loading">加载中...</div>
  60. <!-- 控制面板(可选,可以隐藏) -->
  61. <!-- 响应数据(可以设为隐藏,仅用于调试) -->
  62. <div v-if="showDebugInfo" class="response-container">
  63. <div v-if="assistantResponse" class="response-item">
  64. <div class="response-content">
  65. <span>助手回复: {{ assistantResponse }}</span>
  66. </div>
  67. </div>
  68. <div v-if="audioTranscript" class="response-item">
  69. <div class="response-content">
  70. <span>音频转写: {{ audioTranscript }}</span>
  71. </div>
  72. </div>
  73. <div v-for="(item, index) in processedResponses" :key="index" class="response-item">
  74. <div class="response-content">
  75. <span v-if="item.role">角色: {{ item.role }}</span>
  76. <span v-if="item.transcript">文本: {{ item.transcript }}</span>
  77. </div>
  78. </div>
  79. </div>
  80. <!-- 添加停止录制按钮 -->
  81. <div class="stop-recording-button-container" v-if="showStopRecordingButton">
  82. <button class="stop-recording-button" @click="stopRecordingAnswer">
  83. 完成作答
  84. </button>
  85. </div>
  86. <!-- 添加录制指示器 -->
  87. <div class="recording-indicator" v-if="isRecording">
  88. <div class="recording-dot"></div>
  89. <span class="recording-text">正在录制...</span>
  90. </div>
  91. <!-- 添加开始回答按钮 -->
  92. <div class="start-recording-button-container" v-if="showStartRecordingButton">
  93. <button class="start-recording-button" @click="handleStartRecordingClick">
  94. 开始作答
  95. </button>
  96. </div>
  97. </div>
  98. </template>
  99. <script>
  100. export default {
  101. name: 'IdentityVerify',
  102. data() {
  103. return {
  104. loading: false,
  105. responses: [],
  106. processedResponses: [],
  107. assistantResponse: '',
  108. audioTranscript: '',
  109. videoPlaying: false,
  110. showDebugInfo: false, // 设置为true可以显示调试信息
  111. videoUrl: 'http://121.36.251.245:9000/minlong/9c139c99-a613-49f2-986b-8a3ac20a9b1b.mp4', // 用于存储AI数字人视频URL
  112. showReplayButton: false,
  113. cameraStream: null, // 存储摄像头流
  114. cameraError: null, // 存储摄像头错误信息
  115. useMiniProgramCameraComponent: false, // 添加小程序相机组件标志
  116. cameraContext: null, // 添加相机上下文
  117. currentSubtitle: '',
  118. subtitles: [
  119. {
  120. startTime: 0, // 开始时间(秒)
  121. endTime: 5, // 结束时间(秒)
  122. text: '你好,我是本次面试的面试官,欢迎参加本公司的线上面试!'
  123. },
  124. {
  125. startTime: 5,
  126. endTime: 13,
  127. text: '面试预计需要15分钟,请你提前安排在网络良好、光线亮度合适、且相对安静的环境参加这次面试'
  128. },
  129. {
  130. startTime: 13,
  131. endTime: 20,
  132. text: '以免影响本次面试的结果。如果你在面试过程中遇到问题,请与我们的招聘人员联系。'
  133. }
  134. ],
  135. secondVideoSubtitles: [
  136. {
  137. startTime: 0,
  138. endTime: 10,
  139. text: '请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:'
  140. }
  141. ],
  142. thirdVideoSubtitles: [
  143. {
  144. startTime: 0,
  145. endTime: 3,
  146. text: '在工作中,你如何确保个人防护装备的正确使用?'
  147. }
  148. ],
  149. fourthVideoSubtitles: [
  150. {
  151. startTime: 0,
  152. endTime: 3,
  153. text: '描述一次你与团队合作改善生产流程的经历。'
  154. }
  155. ],
  156. fifthVideoSubtitles: [
  157. {
  158. startTime: 0,
  159. endTime: 6,
  160. text: '你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?'
  161. }
  162. ],
  163. sixthVideoSubtitles: [
  164. {
  165. startTime: 0,
  166. endTime: 5,
  167. text: '您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?'
  168. }
  169. ],
  170. showAnswerButton: false, // 控制答题按钮显示
  171. currentVideoIndex: 0, // 当前播放的视频索引
  172. videoList: [
  173. 'http://121.36.251.245:9000/minlong/9c139c99-a613-49f2-986b-8a3ac20a9b1b.mp4', // 第一段视频
  174. 'http://121.36.251.245:9000/minlong/15467aca-2c1e-4d40-bebc-11fc984f0a24.mp4', // 第二段视频
  175. 'http://121.36.251.245:9000/minlong/a96f295c-ecba-4b08-b627-0ed0dc72d727.mp4', // 第三段视频
  176. 'http://121.36.251.245:9000/minlong/e152eb38-7e03-4bb0-8b8d-73d51342d2aa.mp4',
  177. 'http://121.36.251.245:9000/minlong/6381eec3-1373-400f-aa8c-e229b60522ea.mp4',
  178. 'http://121.36.251.245:9000/minlong/2a11fe78-38d5-4af3-91c8-a080feb122e0.mp4'//结束
  179. ],
  180. isRecording: false,
  181. recordingTimer: null,
  182. showStopRecordingButton: false,
  183. mediaRecorder: null,
  184. recordedChunks: [],
  185. recorder: null,
  186. lastUploadedVideoUrl: '',
  187. showStartRecordingButton: false,
  188. }
  189. },
  190. mounted() {
  191. /* this.fetchData() */
  192. this.playDigitalHumanVideo();
  193. this.checkAudioPermission();
  194. this.initCamera();
  195. // 添加音频测试
  196. setTimeout(() => {
  197. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  198. this.testAudioInput();
  199. }
  200. }, 3000);
  201. },
  202. beforeDestroy() {
  203. // 组件销毁前停止摄像头
  204. this.stopUserCamera();
  205. },
  206. methods: {
  207. // 初始化相机
  208. async initCamera() {
  209. // 检查平台
  210. const systemInfo = uni.getSystemInfoSync();
  211. // 判断是否在小程序环境中
  212. const isMiniProgram = systemInfo.uniPlatform === 'mp-weixin' ||
  213. systemInfo.uniPlatform === 'mp-alipay' ||
  214. systemInfo.uniPlatform === 'mp-baidu';
  215. if (isMiniProgram) {
  216. // 小程序环境使用小程序的相机API
  217. this.useMiniProgramCameraComponent = true;
  218. // 创建相机上下文
  219. this.cameraContext = uni.createCameraContext();
  220. // 确保已获取录音权限
  221. uni.getSetting({
  222. success: (res) => {
  223. if (!res.authSetting['scope.record']) {
  224. uni.authorize({
  225. scope: 'scope.record',
  226. success: () => {
  227. console.log('录音权限已获取');
  228. },
  229. fail: (err) => {
  230. console.error('录音权限获取失败:', err);
  231. this.showPermissionDialog('录音');
  232. }
  233. });
  234. }
  235. if (!res.authSetting['scope.camera']) {
  236. uni.authorize({
  237. scope: 'scope.camera',
  238. success: () => {
  239. console.log('相机权限已获取');
  240. },
  241. fail: (err) => {
  242. console.error('相机权限获取失败:', err);
  243. this.showPermissionDialog('相机');
  244. }
  245. });
  246. }
  247. }
  248. });
  249. } else {
  250. // H5/App环境使用浏览器的API
  251. try {
  252. // 请求摄像头和麦克风权限并获取媒体流
  253. const constraints = {
  254. audio: {
  255. echoCancellation: true,
  256. noiseSuppression: true,
  257. autoGainControl: true
  258. },
  259. video: {
  260. width: { ideal: 640 },
  261. height: { ideal: 480 },
  262. facingMode: 'user'
  263. }
  264. };
  265. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  266. // 保存流以便后续使用
  267. this.cameraStream = stream;
  268. // 检查音频轨道
  269. const audioTracks = stream.getAudioTracks();
  270. console.log('音频轨道数量:', audioTracks.length);
  271. if (audioTracks.length > 0) {
  272. console.log('音频轨道已获取:', audioTracks[0].label);
  273. // 确保音频轨道已启用
  274. audioTracks[0].enabled = true;
  275. } else {
  276. console.warn('未检测到音频轨道,尝试单独获取音频');
  277. this.tryGetAudioOnly();
  278. }
  279. // 将流设置到视频元素
  280. const videoElement = this.$refs.userCameraVideo;
  281. if (videoElement) {
  282. videoElement.srcObject = stream;
  283. videoElement.muted = true; // 避免回声,但仍然录制声音
  284. }
  285. } catch (error) {
  286. console.error('获取摄像头失败:', error);
  287. this.cameraError = error.message || '无法访问摄像头';
  288. // 显示错误提示
  289. uni.showToast({
  290. title: '无法访问摄像头,请检查权限设置',
  291. icon: 'none'
  292. });
  293. }
  294. }
  295. },
  296. // 停止用户摄像头
  297. stopUserCamera() {
  298. if (this.cameraStream) {
  299. // 停止所有轨道
  300. this.cameraStream.getTracks().forEach(track => {
  301. track.stop();
  302. });
  303. this.cameraStream = null;
  304. }
  305. },
  306. async fetchData() {
  307. this.loading = true
  308. this.assistantResponse = ''
  309. this.audioTranscript = ''
  310. this.processedResponses = []
  311. try {
  312. // 使用uni.request代替fetch
  313. const requestTask = uni.request({
  314. url: 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions',
  315. method: 'POST',
  316. header: {
  317. 'Content-Type': 'application/json',
  318. 'Authorization': 'Bearer sk-9e1ec73a7d97493b8613c63f06b6110c'
  319. },
  320. data: {
  321. "model": "qwen-omni-turbo",
  322. "messages": [
  323. {
  324. "role": "user",
  325. "content": [
  326. {
  327. "type": "input_audio",
  328. "input_audio": {
  329. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  330. "format": "wav"
  331. }
  332. },
  333. {
  334. "type": "text",
  335. "text": "这段音频在说什么"
  336. }
  337. ]
  338. }
  339. ],
  340. "stream":true,
  341. "stream_options":{
  342. "include_usage":true
  343. },
  344. "modalities":["text","audio"],
  345. "audio":{"voice":"Cherry","format":"wav"}
  346. },
  347. success: (res) => {
  348. console.log('请求成功,响应数据:', res.data);
  349. // 检查响应数据是否包含多个JSON对象
  350. if (typeof res.data === 'string' && res.data.includes('data: {')) {
  351. // 处理包含多个JSON对象的情况
  352. const chunks = res.data.split('data: ').filter(chunk => chunk.trim() !== '');
  353. chunks.forEach(chunk => {
  354. this.handleStreamResponse(chunk);
  355. });
  356. } else {
  357. // 处理单个响应对象的情况
  358. this.handleStreamResponse(res.data);
  359. }
  360. // 模拟获取到数字人视频并播放
  361. this.playDigitalHumanVideo();
  362. },
  363. fail: (err) => {
  364. console.error('请求失败:', err);
  365. },
  366. complete: () => {
  367. this.loading = false;
  368. }
  369. });
  370. } catch (error) {
  371. console.error('获取数据失败:', error);
  372. this.loading = false;
  373. }
  374. },
  375. handleStreamResponse(data) {
  376. // 处理流式响应数据
  377. if (typeof data === 'string') {
  378. // 处理字符串格式的响应
  379. if (data === '[DONE]') return;
  380. try {
  381. // 移除可能存在的换行符和多余空格
  382. const cleanData = data.trim();
  383. // 检查是否是有效的JSON字符串
  384. if (cleanData.startsWith('{') && cleanData.endsWith('}')) {
  385. const jsonData = JSON.parse(cleanData);
  386. this.processStreamChunk(jsonData);
  387. }
  388. } catch (e) {
  389. console.error('解析JSON失败:', e, '原始数据:', data);
  390. }
  391. } else {
  392. // 处理对象格式的响应
  393. this.processStreamChunk(data);
  394. }
  395. },
  396. processStreamChunk(chunk) {
  397. if (chunk.choices && chunk.choices.length > 0) {
  398. const choice = chunk.choices[0];
  399. // 处理助手回复内容
  400. if (choice.delta && choice.delta.content) {
  401. this.assistantResponse += choice.delta.content;
  402. }
  403. // 处理音频转写内容
  404. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  405. this.audioTranscript += choice.delta.audio.transcript;
  406. }
  407. // 处理角色和音频转写
  408. if (choice.delta) {
  409. const result = {};
  410. if (choice.delta.role) {
  411. result.role = choice.delta.role;
  412. }
  413. if (choice.delta.audio && choice.delta.audio.transcript) {
  414. result.transcript = choice.delta.audio.transcript;
  415. }
  416. if (Object.keys(result).length > 0) {
  417. this.processedResponses.push(result);
  418. }
  419. }
  420. }
  421. },
  422. processResponseData() {
  423. // 处理返回的数据
  424. this.processedResponses = this.responses.map(item => {
  425. const result = {}
  426. // 处理角色信息
  427. if (item.delta && item.delta.role) {
  428. result.role = item.delta.role
  429. }
  430. // 处理音频转写文本
  431. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  432. result.transcript = item.delta.audio.transcript
  433. }
  434. return result
  435. }).filter(item => Object.keys(item).length > 0)
  436. },
  437. // 播放数字人视频
  438. playDigitalHumanVideo() {
  439. // 设置第一个视频
  440. this.videoUrl = this.videoList[this.currentVideoIndex];
  441. this.videoPlaying = true;
  442. // 使用 uni.createVideoContext 来控制视频
  443. this.$nextTick(() => {
  444. const videoContext = uni.createVideoContext('myVideo', this);
  445. if (videoContext) {
  446. videoContext.play();
  447. // 设置超时检查,确认视频是否真的在播放
  448. setTimeout(() => {
  449. if (this.videoPlaying && this.$refs.videoPlayer) {
  450. console.log('视频应该正在播放');
  451. } else {
  452. console.log('视频可能未成功播放,尝试替代方案');
  453. this.tryAlternativeVideoPath();
  454. }
  455. }, 1000);
  456. } else {
  457. console.error('无法创建视频上下文');
  458. this.tryAlternativeVideoPath();
  459. }
  460. });
  461. },
  462. // 修改 tryAlternativeVideoPath 方法
  463. tryAlternativeVideoPath() {
  464. console.log('尝试使用替代路径');
  465. // 尝试不同的路径格式
  466. const alternativePaths = [
  467. './static/demo.mp4',
  468. '../static/demo.mp4',
  469. 'static/demo.mp4',
  470. '/static/demo.mp4',
  471. // 添加绝对路径
  472. `${window.location.origin}/static/demo.mp4`
  473. ];
  474. // 获取当前路径索引
  475. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  476. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  477. // 设置下一个路径
  478. this.videoUrl = alternativePaths[nextPathIndex];
  479. console.log('尝试新路径:', this.videoUrl);
  480. this.$nextTick(() => {
  481. const videoContext = uni.createVideoContext('myVideo', this);
  482. if (videoContext) {
  483. videoContext.stop();
  484. videoContext.play();
  485. // 检查是否成功播放
  486. setTimeout(() => {
  487. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  488. console.log('所有路径均失败,尝试使用uni.getVideoInfo检查视频');
  489. this.checkVideoWithAPI();
  490. }
  491. }, 1000);
  492. }
  493. });
  494. },
  495. // 添加新方法:使用uni API检查视频
  496. checkVideoWithAPI() {
  497. // 尝试使用uni.getVideoInfo API检查视频是否可用
  498. uni.getVideoInfo({
  499. src: '/static/demo.mp4',
  500. success: (res) => {
  501. console.log('视频信息获取成功:', res);
  502. // 如果能获取到视频信息,再次尝试播放
  503. this.videoUrl = '/static/demo.mp4';
  504. this.$nextTick(() => {
  505. const videoContext = uni.createVideoContext('myVideo', this);
  506. if (videoContext) {
  507. videoContext.play();
  508. }
  509. });
  510. },
  511. fail: (err) => {
  512. console.error('视频信息获取失败:', err);
  513. // 最后尝试使用uni.chooseVideo API
  514. this.fallbackToLocalVideo();
  515. }
  516. });
  517. },
  518. // 添加新方法:回退到本地视频
  519. fallbackToLocalVideo() {
  520. console.log('尝试使用本地视频资源');
  521. // 检查平台
  522. const platform = uni.getSystemInfoSync().platform;
  523. if (platform === 'android' || platform === 'ios') {
  524. // 移动端可以尝试使用本地资源
  525. this.videoUrl = platform === 'android' ? 'android.resource://package_name/raw/demo' : 'file:///assets/demo.mp4';
  526. this.$nextTick(() => {
  527. const videoContext = uni.createVideoContext('myVideo', this);
  528. if (videoContext) {
  529. videoContext.play();
  530. }
  531. });
  532. } else {
  533. // 最终回退到静态图片
  534. this.videoPlaying = false;
  535. uni.showToast({
  536. title: '视频加载失败,显示静态图片',
  537. icon: 'none'
  538. });
  539. }
  540. },
  541. // 修改 handleVideoError 方法
  542. handleVideoError(e) {
  543. console.error('视频加载错误:', e);
  544. // 记录更详细的错误信息
  545. if (e && e.detail) {
  546. console.error('详细错误信息:', e.detail);
  547. }
  548. // 检查视频文件是否存在
  549. uni.getFileInfo({
  550. filePath: this.videoUrl.startsWith('/') ? this.videoUrl.substring(1) : this.videoUrl,
  551. success: (res) => {
  552. console.log('文件存在,大小:', res.size);
  553. // 文件存在但播放失败,可能是格式问题
  554. this.tryDifferentFormat();
  555. },
  556. fail: (err) => {
  557. console.error('文件不存在或无法访问:', err);
  558. // 尝试不同路径
  559. this.tryAlternativeVideoPath();
  560. }
  561. });
  562. // 如果多次尝试后仍然失败,显示错误信息
  563. uni.showToast({
  564. title: '视频加载失败,请检查文件是否存在',
  565. icon: 'none',
  566. duration: 2000
  567. });
  568. },
  569. // 添加新方法:尝试不同格式
  570. tryDifferentFormat() {
  571. console.log('尝试不同的视频格式');
  572. // 尝试不同的视频格式
  573. const formats = [
  574. { ext: 'mp4', mime: 'video/mp4' },
  575. { ext: 'webm', mime: 'video/webm' },
  576. { ext: 'ogg', mime: 'video/ogg' },
  577. { ext: 'mov', mime: 'video/quicktime' }
  578. ];
  579. // 获取当前文件名(不含扩展名)
  580. const currentPath = this.videoUrl;
  581. const basePath = currentPath.substring(0, currentPath.lastIndexOf('.')) || '/static/demo';
  582. // 尝试下一个格式
  583. let nextFormat = formats.find(f => !currentPath.endsWith(f.ext));
  584. if (nextFormat) {
  585. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  586. console.log('尝试新格式:', this.videoUrl);
  587. this.$nextTick(() => {
  588. const videoContext = uni.createVideoContext('myVideo', this);
  589. if (videoContext) {
  590. videoContext.stop();
  591. videoContext.play();
  592. }
  593. });
  594. } else {
  595. // 所有格式都尝试过了,使用内置资源
  596. this.useBuiltInResource();
  597. }
  598. },
  599. // 添加新方法:使用内置资源
  600. useBuiltInResource() {
  601. console.log('尝试使用内置资源');
  602. // 检查平台
  603. const platform = uni.getSystemInfoSync().platform;
  604. // 根据平台选择合适的视频源
  605. if (platform === 'windows') {
  606. // Windows平台特定处理
  607. // 尝试使用相对于应用根目录的路径
  608. const appRoot = process.env.UNI_INPUT_DIR || '';
  609. this.videoUrl = `./static/demo.mp4`;
  610. // 或者尝试使用file://协议
  611. // this.videoUrl = `file:///${appRoot.replace(/\\/g, '/')}/static/demo.mp4`;
  612. console.log('Windows平台尝试路径:', this.videoUrl);
  613. } else if (platform === 'android' || platform === 'ios') {
  614. // 移动端
  615. this.useNativeVideo();
  616. } else {
  617. // Web平台
  618. // 尝试使用完整URL
  619. const baseUrl = window.location.origin;
  620. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  621. console.log('Web平台尝试URL:', this.videoUrl);
  622. }
  623. this.$nextTick(() => {
  624. const videoContext = uni.createVideoContext('myVideo', this);
  625. if (videoContext) {
  626. videoContext.play();
  627. }
  628. });
  629. },
  630. // 添加新方法:使用原生视频能力
  631. useNativeVideo() {
  632. console.log('尝试使用原生视频能力');
  633. // 在移动端,可以尝试使用原生视频播放器
  634. uni.chooseVideo({
  635. sourceType: ['album'],
  636. success: (res) => {
  637. this.videoUrl = res.tempFilePath;
  638. this.$nextTick(() => {
  639. const videoContext = uni.createVideoContext('myVideo', this);
  640. if (videoContext) {
  641. videoContext.play();
  642. }
  643. });
  644. },
  645. fail: () => {
  646. // 如果用户取消选择,回退到静态图片
  647. this.videoPlaying = false;
  648. uni.showToast({
  649. title: '无法加载视频,显示静态图片',
  650. icon: 'none'
  651. });
  652. }
  653. });
  654. },
  655. // 处理视频结束事件
  656. handleVideoEnded() {
  657. console.log('视频播放结束');
  658. this.videoPlaying = false;
  659. // 对于所有视频,都显示相应的按钮
  660. if (this.currentVideoIndex >= 1) {
  661. // 对于问题视频,显示"开始回答"按钮
  662. this.showStartRecordingButton = true;
  663. } else {
  664. // 对于介绍视频(第一个视频),显示"开始面试"按钮
  665. this.showAnswerButton = true;
  666. }
  667. },
  668. // 添加新方法:开始录制用户回答
  669. startRecordingAnswer() {
  670. console.log('开始录制用户回答');
  671. this.isRecording = true;
  672. // 显示录制中的提示
  673. // uni.showLoading({
  674. // title: '正在录制...',
  675. // mask: true
  676. // });
  677. // 根据平台选择不同的录制方法
  678. const systemInfo = uni.getSystemInfoSync();
  679. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  680. if (isMiniProgram) {
  681. // 小程序环境使用小程序的录制API
  682. this.startMiniProgramRecording();
  683. } else {
  684. // H5/App环境使用MediaRecorder API
  685. this.startBrowserRecording();
  686. }
  687. // 显示停止录制按钮
  688. this.showStopRecordingButton = true;
  689. },
  690. // 修改小程序环境下的录制方法
  691. startMiniProgramRecording() {
  692. if (!this.cameraContext) {
  693. this.cameraContext = uni.createCameraContext();
  694. }
  695. // 确保有录音权限
  696. uni.getSetting({
  697. success: (res) => {
  698. const hasRecordAuth = res.authSetting['scope.record'];
  699. const hasCameraAuth = res.authSetting['scope.camera'];
  700. if (!hasRecordAuth || !hasCameraAuth) {
  701. console.warn('缺少必要权限,请求权限');
  702. this.requestMiniProgramPermissions();
  703. return;
  704. }
  705. // 开始录像,确保同时录制音频
  706. const options = {
  707. timeout: 60000, // 最长录制60秒
  708. success: () => {
  709. console.log('小程序录像开始成功');
  710. },
  711. fail: (err) => {
  712. console.error('小程序录像开始失败:', err);
  713. uni.showToast({
  714. title: '录制失败,请检查相机权限',
  715. icon: 'none'
  716. });
  717. this.proceedToNextQuestion();
  718. },
  719. timeoutCallback: () => {
  720. // 录制超时自动停止时的回调
  721. console.log('录制超时自动停止');
  722. this.stopRecordingAnswer();
  723. }
  724. };
  725. this.recorder = this.cameraContext.startRecord(options);
  726. }
  727. });
  728. },
  729. // 添加新方法:请求小程序权限
  730. requestMiniProgramPermissions() {
  731. // 请求录音权限
  732. uni.authorize({
  733. scope: 'scope.record',
  734. success: () => {
  735. console.log('录音权限已获取');
  736. // 请求相机权限
  737. uni.authorize({
  738. scope: 'scope.camera',
  739. success: () => {
  740. console.log('相机权限已获取');
  741. // 权限都获取到后,开始录制
  742. this.startMiniProgramRecording();
  743. },
  744. fail: (err) => {
  745. console.error('相机权限获取失败:', err);
  746. this.showPermissionDialog('相机');
  747. }
  748. });
  749. },
  750. fail: (err) => {
  751. console.error('录音权限获取失败:', err);
  752. this.showPermissionDialog('录音');
  753. }
  754. });
  755. },
  756. // 修改浏览器环境下的录制方法
  757. startBrowserRecording() {
  758. if (!this.cameraStream) {
  759. console.error('没有可用的摄像头流');
  760. uni.showToast({
  761. title: '录制失败,摄像头未就绪',
  762. icon: 'none'
  763. });
  764. this.proceedToNextQuestion();
  765. return;
  766. }
  767. try {
  768. // 检查流中是否包含音频轨道
  769. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  770. if (!hasAudio) {
  771. console.warn('警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流');
  772. // 尝试重新获取带音频的媒体流
  773. navigator.mediaDevices.getUserMedia({
  774. audio: {
  775. echoCancellation: true,
  776. noiseSuppression: true,
  777. autoGainControl: true
  778. },
  779. video: true
  780. })
  781. .then(newStream => {
  782. // 检查新流是否包含音频轨道
  783. const audioTracks = newStream.getAudioTracks();
  784. if (audioTracks.length > 0) {
  785. console.log('成功获取音频轨道:', audioTracks[0].label);
  786. // 合并视频和音频轨道
  787. const videoTrack = this.cameraStream.getVideoTracks()[0];
  788. const audioTrack = newStream.getAudioTracks()[0];
  789. // 创建新的媒体流,包含视频和音频轨道
  790. const combinedStream = new MediaStream();
  791. if (videoTrack) combinedStream.addTrack(videoTrack);
  792. if (audioTrack) combinedStream.addTrack(audioTrack);
  793. this.cameraStream = combinedStream;
  794. // 更新视频元素的源
  795. const videoElement = this.$refs.userCameraVideo;
  796. if (videoElement) {
  797. videoElement.srcObject = combinedStream;
  798. videoElement.muted = true; // 避免回声,但仍然录制声音
  799. }
  800. // 继续录制过程
  801. this.setupMediaRecorder(combinedStream);
  802. } else {
  803. console.warn('仍然无法获取音频轨道');
  804. // 使用现有流继续
  805. this.setupMediaRecorder(this.cameraStream);
  806. }
  807. })
  808. .catch(err => {
  809. console.error('获取音频失败:', err);
  810. // 使用现有流继续
  811. this.setupMediaRecorder(this.cameraStream);
  812. });
  813. } else {
  814. console.log('检测到音频轨道,直接使用');
  815. // 已有音频轨道,直接使用
  816. this.setupMediaRecorder(this.cameraStream);
  817. }
  818. } catch (error) {
  819. console.error('浏览器录制失败:', error);
  820. uni.showToast({
  821. title: '录制失败,浏览器可能不支持此功能',
  822. icon: 'none'
  823. });
  824. this.proceedToNextQuestion();
  825. }
  826. },
  827. // 修改 setupMediaRecorder 方法
  828. setupMediaRecorder(stream) {
  829. // 检查流中的轨道
  830. const videoTracks = stream.getVideoTracks();
  831. const audioTracks = stream.getAudioTracks();
  832. console.log('设置MediaRecorder - 视频轨道:', videoTracks.length, '音频轨道:', audioTracks.length);
  833. // 尝试使用支持度更广的编码格式
  834. let mimeType = '';
  835. const supportedTypes = [
  836. 'video/webm;codecs=vp9,opus',
  837. 'video/webm;codecs=vp8,opus',
  838. 'video/webm;codecs=h264,opus',
  839. 'video/mp4;codecs=h264,aac',
  840. 'video/webm',
  841. 'video/mp4'
  842. ];
  843. for (const type of supportedTypes) {
  844. if (MediaRecorder.isTypeSupported(type)) {
  845. mimeType = type;
  846. console.log('使用支持的MIME类型:', mimeType);
  847. break;
  848. }
  849. }
  850. // 创建MediaRecorder实例,设置较高的音频比特率
  851. const options = {
  852. mimeType: mimeType || '',
  853. audioBitsPerSecond: 128000,
  854. videoBitsPerSecond: 2500000
  855. };
  856. try {
  857. this.mediaRecorder = new MediaRecorder(stream, options);
  858. console.log('MediaRecorder创建成功,使用选项:', options);
  859. } catch (e) {
  860. console.warn('使用指定选项创建MediaRecorder失败,尝试使用默认选项');
  861. this.mediaRecorder = new MediaRecorder(stream);
  862. }
  863. // 存储录制的数据块
  864. this.recordedChunks = [];
  865. // 监听数据可用事件
  866. this.mediaRecorder.ondataavailable = (event) => {
  867. if (event.data && event.data.size > 0) {
  868. this.recordedChunks.push(event.data);
  869. console.log(`收到数据块: ${event.data.size} 字节`);
  870. }
  871. };
  872. // 监听录制停止事件
  873. this.mediaRecorder.onstop = () => {
  874. console.log('MediaRecorder停止,数据块数量:', this.recordedChunks.length);
  875. if (this.recordedChunks.length === 0) {
  876. console.error('没有录制到数据');
  877. uni.showToast({
  878. title: '录制失败,未捕获到数据',
  879. icon: 'none'
  880. });
  881. this.proceedToNextQuestion();
  882. return;
  883. }
  884. // 创建Blob对象
  885. const mimeType = this.mediaRecorder.mimeType || 'video/webm';
  886. const blob = new Blob(this.recordedChunks, { type: mimeType });
  887. console.log('创建Blob,大小:', blob.size, '类型:', mimeType);
  888. // 将Blob转换为File对象
  889. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  890. const file = new File([blob], fileName, { type: mimeType });
  891. // 上传文件
  892. this.uploadRecordedVideo(file);
  893. };
  894. // 监听错误
  895. this.mediaRecorder.onerror = (event) => {
  896. console.error('MediaRecorder错误:', event.error);
  897. };
  898. // 开始录制
  899. try {
  900. this.mediaRecorder.start(1000); // 每秒触发一次dataavailable事件
  901. console.log('MediaRecorder开始录制');
  902. } catch (e) {
  903. console.error('开始录制失败:', e);
  904. }
  905. },
  906. // 添加新方法:停止录制用户回答
  907. stopRecordingAnswer() {
  908. console.log('停止录制用户回答');
  909. this.isRecording = false;
  910. // 清除定时器
  911. if (this.recordingTimer) {
  912. clearTimeout(this.recordingTimer);
  913. this.recordingTimer = null;
  914. }
  915. // 隐藏录制中提示
  916. uni.hideLoading();
  917. // 隐藏停止录制按钮
  918. this.showStopRecordingButton = false;
  919. // 根据平台选择不同的停止录制方法
  920. const systemInfo = uni.getSystemInfoSync();
  921. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  922. if (isMiniProgram) {
  923. // 小程序环境停止录制
  924. this.stopMiniProgramRecording();
  925. } else {
  926. // H5/App环境停止录制
  927. this.stopBrowserRecording();
  928. }
  929. },
  930. // 添加新方法:停止小程序录制
  931. stopMiniProgramRecording() {
  932. if (!this.cameraContext) {
  933. console.error('相机上下文不存在');
  934. this.proceedToNextQuestion();
  935. return;
  936. }
  937. this.cameraContext.stopRecord({
  938. success: (res) => {
  939. console.log('小程序录像停止成功:', res);
  940. // 获取临时文件路径
  941. const tempFilePath = res.tempVideoPath;
  942. // 上传视频
  943. this.uploadRecordedVideo(tempFilePath);
  944. },
  945. fail: (err) => {
  946. console.error('小程序录像停止失败:', err);
  947. uni.showToast({
  948. title: '录制失败',
  949. icon: 'none'
  950. });
  951. this.proceedToNextQuestion();
  952. }
  953. });
  954. },
  955. // 添加新方法:停止浏览器录制
  956. stopBrowserRecording() {
  957. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  958. this.mediaRecorder.stop();
  959. console.log('浏览器录制停止成功');
  960. } else {
  961. console.error('MediaRecorder不存在或已经停止');
  962. this.proceedToNextQuestion();
  963. }
  964. },
  965. // 修改上传录制的视频方法
  966. uploadRecordedVideo(fileOrPath) {
  967. console.log('准备上传视频:', typeof fileOrPath === 'string' ? fileOrPath : fileOrPath.name);
  968. // 显示上传中提示
  969. uni.showLoading({
  970. title: '正在上传...',
  971. mask: true
  972. });
  973. // 获取用户信息
  974. const userInfo = uni.getStorageSync('userInfo');
  975. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  976. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  977. // 根据当前视频索引映射到正确的问题ID
  978. let questionId;
  979. switch(this.currentVideoIndex) {
  980. case 1: // 第一个问题
  981. questionId = 10;
  982. break;
  983. case 2: // 第二个问题
  984. questionId = 11;
  985. break;
  986. case 3: // 第三个问题
  987. questionId = 12;
  988. break;
  989. case 4: // 第四个问题
  990. questionId = 13;
  991. break;
  992. default:
  993. questionId = 10; // 默认值
  994. }
  995. // 检查是否在浏览器环境中使用File对象
  996. if (typeof fileOrPath !== 'string') {
  997. // 在浏览器环境中,我们需要将File对象转换为FormData
  998. const formData = new FormData();
  999. formData.append('file', fileOrPath);
  1000. formData.append('openid', openid);
  1001. formData.append('tenant_id', tenant_id);
  1002. formData.append('application_id', 1);
  1003. formData.append('question_id', questionId);
  1004. formData.append('video_duration', 0);
  1005. formData.append('has_audio', 'true'); // 明确标记视频包含音频
  1006. // 使用XMLHttpRequest直接上传
  1007. const xhr = new XMLHttpRequest();
  1008. xhr.open('POST', 'http://192.168.66.187:8083/api/system/upload/', true);
  1009. xhr.onload = () => {
  1010. uni.hideLoading();
  1011. if (xhr.status === 200) {
  1012. try {
  1013. const res = JSON.parse(xhr.responseText);
  1014. console.log('上传响应:', res);
  1015. if (res.code === 2000) {
  1016. // 获取上传后的视频URL
  1017. const videoUrl = res.data.url || res.data.photoUrl || '';
  1018. if (videoUrl) {
  1019. // 提交到面试接口
  1020. this.submitVideoToInterview(videoUrl);
  1021. } else {
  1022. uni.showToast({
  1023. title: '视频URL获取失败',
  1024. icon: 'none'
  1025. });
  1026. }
  1027. } else {
  1028. uni.showToast({
  1029. title: res.msg || '上传失败,请重试',
  1030. icon: 'none'
  1031. });
  1032. }
  1033. } catch (e) {
  1034. console.error('解析响应失败:', e);
  1035. uni.showToast({
  1036. title: '解析响应失败',
  1037. icon: 'none'
  1038. });
  1039. }
  1040. } else {
  1041. uni.showToast({
  1042. title: '上传失败,HTTP状态: ' + xhr.status,
  1043. icon: 'none'
  1044. });
  1045. }
  1046. };
  1047. xhr.onerror = () => {
  1048. uni.hideLoading();
  1049. console.error('上传网络错误');
  1050. uni.showToast({
  1051. title: '网络错误,请重试',
  1052. icon: 'none'
  1053. });
  1054. };
  1055. xhr.upload.onprogress = (event) => {
  1056. if (event.lengthComputable) {
  1057. const percentComplete = Math.round((event.loaded / event.total) * 100);
  1058. console.log('上传进度:', percentComplete + '%');
  1059. }
  1060. };
  1061. xhr.send(formData);
  1062. return;
  1063. }
  1064. // 对于小程序环境,使用uni.uploadFile
  1065. uni.uploadFile({
  1066. url: 'http://192.168.66.187:8083/api/system/upload/',
  1067. filePath: fileOrPath,
  1068. name: 'file',
  1069. formData: {
  1070. openid: openid,
  1071. tenant_id: tenant_id,
  1072. application_id: 1,
  1073. question_id: questionId,
  1074. video_duration: 0,
  1075. has_audio: 'true' // 明确标记视频包含音频
  1076. },
  1077. success: (uploadRes) => {
  1078. try {
  1079. const res = JSON.parse(uploadRes.data);
  1080. console.log('上传响应:', res);
  1081. if (res.code === 2000) {
  1082. // 获取上传后的视频URL
  1083. const videoUrl = res.data.permanent_link || res.data.url || '';
  1084. if (videoUrl) {
  1085. // 提交到面试接口
  1086. this.submitVideoToInterview(videoUrl);
  1087. } else {
  1088. uni.showToast({
  1089. title: '视频URL获取失败',
  1090. icon: 'none'
  1091. });
  1092. }
  1093. } else {
  1094. uni.showToast({
  1095. title: res.msg || '上传失败,请重试',
  1096. icon: 'none'
  1097. });
  1098. }
  1099. } catch (e) {
  1100. console.error('解析响应失败:', e);
  1101. uni.showToast({
  1102. title: '解析响应失败',
  1103. icon: 'none'
  1104. });
  1105. }
  1106. },
  1107. fail: (err) => {
  1108. console.error('上传失败:', err);
  1109. uni.showToast({
  1110. title: '上传失败,请重试',
  1111. icon: 'none'
  1112. });
  1113. },
  1114. complete: () => {
  1115. uni.hideLoading();
  1116. }
  1117. });
  1118. },
  1119. // 修改 submitVideoToInterview 方法
  1120. submitVideoToInterview(videoUrl) {
  1121. console.log('提交视频URL到面试接口:', videoUrl);
  1122. // 根据当前视频索引映射到正确的问题ID
  1123. let questionId;
  1124. switch(this.currentVideoIndex) {
  1125. case 1: // 第一个问题(索引1是第二个视频,第一个是介绍视频)
  1126. questionId = 10;
  1127. break;
  1128. case 2: // 第二个问题
  1129. questionId = 11;
  1130. break;
  1131. case 3: // 第三个问题
  1132. questionId = 12;
  1133. break;
  1134. case 4: // 第四个问题
  1135. questionId = 13;
  1136. break;
  1137. default:
  1138. questionId = 10; // 默认值,以防万一
  1139. }
  1140. // 准备请求参数,使用映射后的问题ID
  1141. const requestData = {
  1142. application_id: 1,
  1143. question_id: questionId,
  1144. video_url: videoUrl,
  1145. video_duration: 0,
  1146. tenant_id: uni.getStorageSync('tenant_id') || '1'
  1147. };
  1148. // 发送请求到面试接口
  1149. uni.request({
  1150. url: 'http://192.168.66.187:8083/api/job/upload_question_video',
  1151. method: 'POST',
  1152. data: requestData,
  1153. header: {
  1154. 'content-type': 'application/json'
  1155. },
  1156. success: (res) => {
  1157. console.log('面试接口提交成功:', res);
  1158. uni.hideLoading();
  1159. if (res.data.code === 0 || res.data.code === 2000) {
  1160. // 提交成功
  1161. uni.showToast({
  1162. title: '回答已提交',
  1163. icon: 'success'
  1164. });
  1165. // 保存最后上传的视频URL
  1166. this.lastUploadedVideoUrl = videoUrl;
  1167. // 延迟一下再进入下一题,让用户看到成功提示
  1168. setTimeout(() => {
  1169. // 检查是否完成了第五个视频问题(索引为4)
  1170. if (this.currentVideoIndex === 4) {
  1171. // 完成第五个问题后,跳转到相机页面
  1172. uni.navigateTo({
  1173. url: '/pages/camera/camera'
  1174. });
  1175. } else {
  1176. // 否则继续正常流程
  1177. this.proceedToNextQuestion();
  1178. }
  1179. }, 1500);
  1180. } else {
  1181. // 提交失败
  1182. uni.showToast({
  1183. title: res.data.msg || '提交失败,请重试',
  1184. icon: 'none'
  1185. });
  1186. }
  1187. },
  1188. fail: (err) => {
  1189. console.error('面试接口提交失败:', err);
  1190. uni.hideLoading();
  1191. uni.showToast({
  1192. title: '网络错误,请重试',
  1193. icon: 'none'
  1194. });
  1195. }
  1196. });
  1197. },
  1198. // 修改 proceedToNextQuestion 方法
  1199. proceedToNextQuestion() {
  1200. // 检查是否完成了第五个视频问题(索引为4)
  1201. if (this.currentVideoIndex === 4) {
  1202. // 完成第五个问题后,跳转到相机页面
  1203. uni.navigateTo({
  1204. url: '/pages/camera/camera'
  1205. });
  1206. return;
  1207. }
  1208. // 切换到下一个视频
  1209. this.currentVideoIndex++;
  1210. if (this.currentVideoIndex < this.videoList.length) {
  1211. // 还有下一段视频,播放它
  1212. this.videoUrl = this.videoList[this.currentVideoIndex];
  1213. this.videoPlaying = true;
  1214. // 重置当前字幕
  1215. this.currentSubtitle = '';
  1216. // 使用 nextTick 确保 DOM 更新后再播放视频
  1217. this.$nextTick(() => {
  1218. const videoContext = uni.createVideoContext('myVideo', this);
  1219. if (videoContext) {
  1220. videoContext.play();
  1221. }
  1222. });
  1223. } else {
  1224. // 所有视频都播放完毕,可以进行下一步操作
  1225. uni.showToast({
  1226. title: '面试完成',
  1227. icon: 'success',
  1228. duration: 2000
  1229. });
  1230. // 可以在这里添加面试完成后的逻辑,比如跳转到下一个页面
  1231. setTimeout(() => {
  1232. uni.navigateTo({
  1233. url: '/pages/interview-result/interview-result'
  1234. });
  1235. }, 2000);
  1236. }
  1237. },
  1238. // 修改 handleAnswerButtonClick 方法
  1239. handleAnswerButtonClick() {
  1240. // 隐藏答题按钮
  1241. this.showAnswerButton = false;
  1242. // 直接进入下一个问题
  1243. this.proceedToNextQuestion();
  1244. },
  1245. // 处理相机错误
  1246. handleCameraError(e) {
  1247. console.error('相机错误:', e);
  1248. // 显示错误提示
  1249. uni.showToast({
  1250. title: '相机初始化失败,请检查权限设置',
  1251. icon: 'none'
  1252. });
  1253. // 尝试备用选项
  1254. this.tryFallbackOptions();
  1255. },
  1256. // 添加新方法:尝试备用选项
  1257. tryFallbackOptions() {
  1258. // 检查环境
  1259. const systemInfo = uni.getSystemInfoSync();
  1260. // 在小程序环境中使用小程序API
  1261. if (systemInfo.uniPlatform === 'mp-weixin' || systemInfo.uniPlatform === 'mp-alipay') {
  1262. this.useMiniProgramCamera();
  1263. }
  1264. // 在H5环境中显示静态图像
  1265. else {
  1266. this.showStaticCameraPlaceholder();
  1267. }
  1268. },
  1269. // 添加新方法:使用小程序相机API
  1270. useMiniProgramCamera() {
  1271. console.log('尝试使用小程序相机组件');
  1272. // 这里需要在模板中添加小程序相机组件
  1273. // 并设置一个标志来控制显示
  1274. this.useMiniProgramCameraComponent = true;
  1275. },
  1276. // 添加新方法:显示静态图像
  1277. showStaticCameraPlaceholder() {
  1278. console.log('显示静态摄像头占位图');
  1279. // 创建一个图像元素
  1280. const img = document.createElement('img');
  1281. img.src = '/static/images/camera-placeholder.png'; // 确保有这个图片资源
  1282. img.className = 'static-camera-image';
  1283. img.style.width = '100%';
  1284. img.style.height = '100%';
  1285. img.style.objectFit = 'cover';
  1286. // 获取容器并添加图像
  1287. const container = this.$refs.userCameraVideo.parentNode;
  1288. container.appendChild(img);
  1289. },
  1290. // 处理视频时间更新事件
  1291. handleTimeUpdate(e) {
  1292. // 获取当前视频播放时间
  1293. const currentTime = e.target.currentTime;
  1294. // 根据当前播放的视频索引选择对应的字幕数组
  1295. let currentSubtitles;
  1296. if (this.currentVideoIndex === 0) {
  1297. currentSubtitles = this.subtitles;
  1298. } else if (this.currentVideoIndex === 1) {
  1299. currentSubtitles = this.secondVideoSubtitles;
  1300. }else if (this.currentVideoIndex === 2) {
  1301. currentSubtitles = this.thirdVideoSubtitles;
  1302. }else if (this.currentVideoIndex === 3) {
  1303. currentSubtitles = this.fourthVideoSubtitles;
  1304. }else if (this.currentVideoIndex === 4) {
  1305. currentSubtitles = this.fifthVideoSubtitles;
  1306. }else if (this.currentVideoIndex === 5) {
  1307. currentSubtitles = this.sixthVideoSubtitles;
  1308. }else {
  1309. // 如果有更多视频,可以继续添加条件
  1310. currentSubtitles = [];
  1311. }
  1312. // 查找当前时间应该显示的字幕
  1313. const subtitle = currentSubtitles.find(
  1314. sub => currentTime >= sub.startTime && currentTime < sub.endTime
  1315. );
  1316. // 更新当前字幕
  1317. this.currentSubtitle = subtitle ? subtitle.text : '';
  1318. },
  1319. // Add a new method to handle the "Start Recording" button click
  1320. handleStartRecordingClick() {
  1321. // 隐藏"开始回答"按钮
  1322. this.showStartRecordingButton = false;
  1323. // 开始录制
  1324. this.startRecordingAnswer();
  1325. },
  1326. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  1327. checkAudioPermission() {
  1328. // 在小程序环境中
  1329. const systemInfo = uni.getSystemInfoSync();
  1330. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1331. if (isMiniProgram) {
  1332. // 小程序环境下的权限请求
  1333. uni.getSetting({
  1334. success: (res) => {
  1335. if (!res.authSetting['scope.record']) {
  1336. uni.authorize({
  1337. scope: 'scope.record',
  1338. success: () => {
  1339. console.log('录音权限已获取');
  1340. },
  1341. fail: (err) => {
  1342. console.error('录音权限获取失败:', err);
  1343. this.showPermissionDialog('录音');
  1344. }
  1345. });
  1346. }
  1347. if (!res.authSetting['scope.camera']) {
  1348. uni.authorize({
  1349. scope: 'scope.camera',
  1350. success: () => {
  1351. console.log('相机权限已获取');
  1352. },
  1353. fail: (err) => {
  1354. console.error('相机权限获取失败:', err);
  1355. this.showPermissionDialog('相机');
  1356. }
  1357. });
  1358. }
  1359. }
  1360. });
  1361. } else {
  1362. // 浏览器环境下的权限请求
  1363. // ... 保持原有代码不变
  1364. }
  1365. },
  1366. // 添加新方法:测试音频输入
  1367. testAudioInput() {
  1368. if (!this.cameraStream) {
  1369. console.warn('没有可用的媒体流,无法测试音频');
  1370. return;
  1371. }
  1372. const audioTracks = this.cameraStream.getAudioTracks();
  1373. if (audioTracks.length === 0) {
  1374. console.warn('没有检测到音频轨道,尝试重新获取');
  1375. this.tryGetAudioOnly();
  1376. return;
  1377. }
  1378. console.log('音频轨道信息:', audioTracks[0].getSettings());
  1379. // 创建音频上下文和分析器
  1380. try {
  1381. const AudioContext = window.AudioContext || window.webkitAudioContext;
  1382. if (!AudioContext) {
  1383. console.warn('浏览器不支持AudioContext');
  1384. return;
  1385. }
  1386. const audioContext = new AudioContext();
  1387. const analyser = audioContext.createAnalyser();
  1388. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  1389. microphone.connect(analyser);
  1390. // 设置分析器
  1391. analyser.fftSize = 256;
  1392. const bufferLength = analyser.frequencyBinCount;
  1393. const dataArray = new Uint8Array(bufferLength);
  1394. // 检测音频输入
  1395. let silenceCounter = 0;
  1396. const checkAudio = () => {
  1397. if (this.isRecording) return; // 如果正在录制,停止检测
  1398. analyser.getByteFrequencyData(dataArray);
  1399. // 计算平均音量
  1400. let sum = 0;
  1401. for (let i = 0; i < bufferLength; i++) {
  1402. sum += dataArray[i];
  1403. }
  1404. const average = sum / bufferLength;
  1405. if (average > 10) {
  1406. console.log('检测到音频输入,音量:', average);
  1407. silenceCounter = 0;
  1408. } else {
  1409. silenceCounter++;
  1410. if (silenceCounter > 10) {
  1411. console.warn('持续检测不到音频输入,可能麦克风未正常工作');
  1412. silenceCounter = 0;
  1413. }
  1414. }
  1415. // 继续检测
  1416. requestAnimationFrame(checkAudio);
  1417. };
  1418. // 开始检测
  1419. checkAudio();
  1420. } catch (e) {
  1421. console.error('音频测试失败:', e);
  1422. }
  1423. },
  1424. // 添加新方法:尝试单独获取音频
  1425. tryGetAudioOnly() {
  1426. navigator.mediaDevices.getUserMedia({ audio: true })
  1427. .then(audioStream => {
  1428. // 如果已有视频流,合并音频和视频轨道
  1429. if (this.cameraStream) {
  1430. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1431. const audioTrack = audioStream.getAudioTracks()[0];
  1432. // 创建新的媒体流,包含视频和音频轨道
  1433. const combinedStream = new MediaStream();
  1434. if (videoTrack) combinedStream.addTrack(videoTrack);
  1435. if (audioTrack) combinedStream.addTrack(audioTrack);
  1436. // 更新摄像头流
  1437. this.cameraStream = combinedStream;
  1438. // 更新视频元素的源
  1439. const videoElement = this.$refs.userCameraVideo;
  1440. if (videoElement) {
  1441. videoElement.srcObject = combinedStream;
  1442. videoElement.muted = true; // 避免回声,但仍然录制声音
  1443. }
  1444. console.log('成功合并音频和视频轨道');
  1445. } else {
  1446. console.warn('没有视频流可合并');
  1447. }
  1448. })
  1449. .catch(err => {
  1450. console.error('单独获取音频失败:', err);
  1451. });
  1452. },
  1453. // 添加新方法:显示权限对话框
  1454. showPermissionDialog(permissionType) {
  1455. uni.showModal({
  1456. title: '需要权限',
  1457. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  1458. confirmText: '去设置',
  1459. success: (res) => {
  1460. if (res.confirm) {
  1461. uni.openSetting({
  1462. success: (settingRes) => {
  1463. console.log('设置页面打开成功', settingRes);
  1464. }
  1465. });
  1466. }
  1467. }
  1468. });
  1469. },
  1470. }
  1471. }
  1472. </script>
  1473. <style scoped>
  1474. .identity-verify-container {
  1475. padding: 0;
  1476. max-width: 100%;
  1477. margin: 0 auto;
  1478. height: 100vh;
  1479. display: flex;
  1480. flex-direction: column;
  1481. background-color: #f5f5f5;
  1482. }
  1483. .digital-human-container {
  1484. position: relative;
  1485. width: 100%;
  1486. height: 100vh;
  1487. overflow: hidden;
  1488. background-color: #f0f0f0;
  1489. }
  1490. .digital-human-video {
  1491. width: 100%;
  1492. height: 100%;
  1493. display: flex;
  1494. justify-content: center;
  1495. align-items: center;
  1496. }
  1497. /* 用户摄像头容器样式 */
  1498. .user-camera-container {
  1499. position: absolute;
  1500. top: 20px;
  1501. right: 20px;
  1502. width: 100px;
  1503. height: 150px;
  1504. border-radius: 8px;
  1505. overflow: hidden;
  1506. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  1507. z-index: 20;
  1508. border: 1px solid #fff;
  1509. }
  1510. /* 用户摄像头视频样式 */
  1511. .user-camera-video {
  1512. width: 100%;
  1513. height: 100%;
  1514. object-fit: cover;
  1515. background-color: #333;
  1516. }
  1517. .video-player {
  1518. width: 100%;
  1519. height: 100%;
  1520. object-fit: cover;
  1521. outline: none; /* 移除视频获得焦点时的轮廓 */
  1522. -webkit-tap-highlight-color: transparent; /* 移除移动设备上的点击高亮 */
  1523. }
  1524. /* 隐藏视频控制条 */
  1525. video::-webkit-media-controls {
  1526. display: none !important;
  1527. }
  1528. video::-webkit-media-controls-enclosure {
  1529. display: none !important;
  1530. }
  1531. video::-webkit-media-controls-panel {
  1532. display: none !important;
  1533. }
  1534. video::-webkit-media-controls-play-button {
  1535. display: none !important;
  1536. }
  1537. video::-webkit-media-controls-timeline {
  1538. display: none !important;
  1539. }
  1540. video::-webkit-media-controls-current-time-display {
  1541. display: none !important;
  1542. }
  1543. video::-webkit-media-controls-time-remaining-display {
  1544. display: none !important;
  1545. }
  1546. video::-webkit-media-controls-mute-button {
  1547. display: none !important;
  1548. }
  1549. video::-webkit-media-controls-volume-slider {
  1550. display: none !important;
  1551. }
  1552. video::-webkit-media-controls-fullscreen-button {
  1553. display: none !important;
  1554. }
  1555. .subtitle-overlay {
  1556. position: absolute;
  1557. bottom: 50px;
  1558. left: 0;
  1559. width: 100%;
  1560. padding: 15px;
  1561. background-color: rgba(0, 0, 0, 0.7);
  1562. color: white;
  1563. text-align: center;
  1564. font-size: 16px;
  1565. line-height: 1.5;
  1566. z-index: 10;
  1567. border-radius: 4px;
  1568. max-width: 90%;
  1569. margin: 0 auto;
  1570. left: 5%;
  1571. right: 5%;
  1572. }
  1573. .control-panel {
  1574. padding: 15px;
  1575. display: flex;
  1576. justify-content: center;
  1577. }
  1578. .control-button {
  1579. padding: 10px 20px;
  1580. background-color: #4CAF50;
  1581. color: white;
  1582. border: none;
  1583. border-radius: 4px;
  1584. cursor: pointer;
  1585. }
  1586. .loading {
  1587. text-align: center;
  1588. margin: 20px 0;
  1589. font-size: 16px;
  1590. }
  1591. .response-container {
  1592. margin-top: 20px;
  1593. padding: 0 20px;
  1594. display: none; /* 默认隐藏调试信息 */
  1595. }
  1596. /* 当showDebugInfo为true时显示 */
  1597. .showDebugInfo .response-container {
  1598. display: block;
  1599. }
  1600. .response-item {
  1601. padding: 10px;
  1602. border: 1px solid #eee;
  1603. border-radius: 4px;
  1604. margin-bottom: 10px;
  1605. background-color: #f9f9f9;
  1606. }
  1607. .response-content {
  1608. display: flex;
  1609. flex-direction: column;
  1610. }
  1611. .answer-button-container {
  1612. position: absolute;
  1613. top: 75%;
  1614. left: 50%;
  1615. transform: translate(-50%, -50%);
  1616. z-index: 20;
  1617. }
  1618. .answer-button {
  1619. width: 120px;
  1620. height: 120px;
  1621. border-radius: 50%;
  1622. background-color: #6c5ce7; /* 绿色背景,与图片中的颜色类似 */
  1623. color: white;
  1624. font-size: 18px;
  1625. border: none;
  1626. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1627. display: flex;
  1628. justify-content: center;
  1629. align-items: center;
  1630. cursor: pointer;
  1631. animation: pulse 2s infinite;
  1632. }
  1633. @keyframes pulse {
  1634. 0% {
  1635. transform: scale(1);
  1636. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1637. }
  1638. 50% {
  1639. transform: scale(1.05);
  1640. box-shadow: 0 8px 15px rgba(0, 0, 0, 0.4);
  1641. }
  1642. 100% {
  1643. transform: scale(1);
  1644. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1645. }
  1646. }
  1647. .stop-recording-button-container {
  1648. position: absolute;
  1649. top: 75%;
  1650. left: 50%;
  1651. transform: translate(-50%, -50%);
  1652. z-index: 20;
  1653. }
  1654. .stop-recording-button {
  1655. width: 120px;
  1656. height: 120px;
  1657. border-radius: 50%;
  1658. background-color: #e74c3c; /* 红色背景 */
  1659. color: white;
  1660. font-size: 18px;
  1661. border: none;
  1662. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1663. display: flex;
  1664. justify-content: center;
  1665. align-items: center;
  1666. cursor: pointer;
  1667. animation: pulse 2s infinite;
  1668. }
  1669. @keyframes pulse {
  1670. 0% {
  1671. transform: scale(1);
  1672. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1673. }
  1674. 50% {
  1675. transform: scale(1.05);
  1676. box-shadow: 0 8px 15px rgba(0, 0, 0, 0.4);
  1677. }
  1678. 100% {
  1679. transform: scale(1);
  1680. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1681. }
  1682. }
  1683. /* 录制中的指示器 */
  1684. .recording-indicator {
  1685. position: absolute;
  1686. top: 20px;
  1687. left: 20px;
  1688. display: flex;
  1689. align-items: center;
  1690. background-color: rgba(0, 0, 0, 0.6);
  1691. padding: 5px 10px;
  1692. border-radius: 15px;
  1693. z-index: 20;
  1694. }
  1695. .recording-dot {
  1696. width: 12px;
  1697. height: 12px;
  1698. background-color: #e74c3c;
  1699. border-radius: 50%;
  1700. margin-right: 8px;
  1701. animation: blink 1s infinite;
  1702. }
  1703. .recording-text {
  1704. color: white;
  1705. font-size: 14px;
  1706. }
  1707. @keyframes blink {
  1708. 0% { opacity: 1; }
  1709. 50% { opacity: 0.3; }
  1710. 100% { opacity: 1; }
  1711. }
  1712. .start-recording-button-container {
  1713. position: absolute;
  1714. top: 75%;
  1715. left: 50%;
  1716. transform: translate(-50%, -50%);
  1717. z-index: 20;
  1718. }
  1719. .start-recording-button {
  1720. width: 120px;
  1721. height: 120px;
  1722. border-radius: 50%;
  1723. background-color: #6c5ce7; /* 与开始面试按钮颜色保持一致 */
  1724. color: white;
  1725. font-size: 18px;
  1726. border: none;
  1727. box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
  1728. display: flex;
  1729. justify-content: center;
  1730. align-items: center;
  1731. cursor: pointer;
  1732. animation: pulse 2s infinite; /* 使用相同的动画 */
  1733. }
  1734. /* 可以删除pulse-green动画,直接使用与开始面试按钮相同的pulse动画 */
  1735. </style>