interview-question.vue 89 KB

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