interview-question.vue 93 KB

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