interview-question.vue 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061
  1. <template>
  2. <div class="identity-verify-container">
  3. <div class="digital-human-container">
  4. <!-- AI数字人视频/图像显示区域 -->
  5. <div class="digital-human-video">
  6. <!-- 添加GIF图片显示区域 -->
  7. <!-- <img v-if="showGif && gifUrl" :src="gifUrl" class="gif-player" alt="面试官GIF" /> -->
  8. <!-- <video
  9. v-if="showGif && gifUrl"
  10. :src="gifUrl"
  11. id="mpVideo"
  12. autoplay
  13. class="video-player"
  14. :controls="false"
  15. loop
  16. muted
  17. object-fit="contain"
  18. ></video> v-if="!showGif" -->
  19. <!-- 视频播放区域,当showGif为false时显示 -->
  20. <video
  21. :src="videoUrl"
  22. id="myVideo"
  23. ref="videoPlayer"
  24. autoplay
  25. playsinline
  26. disablePictureInPicture
  27. controlsList="nodownload nofullscreen noremoteplayback"
  28. class="video-player"
  29. :controls="false"
  30. @error="handleVideoError"
  31. @ended="handleVideoEnded"
  32. @timeupdate="handleTimeUpdate">
  33. </video>
  34. <div class="answer-button-container" v-if="showAnswerButton">
  35. <button class="answer-button" @click="handleAnswerButtonClick">
  36. 开始提问
  37. </button>
  38. </div>
  39. <!-- 添加字幕覆盖层 :data-time="formatTime(recordingTimerCount) || '03:30'"-->
  40. <div class="subtitle-overlay" v-if="currentSubtitle" >
  41. {{ currentSubtitle }}
  42. </div>
  43. <!-- 添加答题按钮 -->
  44. </div>
  45. <!-- 用户摄像头视频显示区域 -->
  46. <div class="user-camera-container">
  47. <!-- 在小程序环境中使用camera组件 -->
  48. <camera v-if="useMiniProgramCameraComponent"
  49. device-position="front"
  50. flash="off"
  51. class="user-camera-video"
  52. @error="handleCameraError"
  53. :record-audio="true"
  54. frame-size="small"
  55. resolution="low"
  56. :disable-zoom="true"
  57. :enable-camera="true"
  58. :enable-record="true"
  59. mode="normal">
  60. </camera>
  61. <!-- 在H5/App环境中使用video元素 -->
  62. <video v-else
  63. id="userCamera"
  64. ref="userCameraVideo"
  65. autoplay
  66. playsinline
  67. muted
  68. class="user-camera-video"
  69. :controls="false">
  70. </video>
  71. </div>
  72. <!-- 字幕/文本覆盖区域 -->
  73. <!-- <div class="subtitle-overlay" v-if="assistantResponse">
  74. {{ assistantResponse }}
  75. </div> -->
  76. </div>
  77. <!-- 加载状态 -->
  78. <div v-if="loading" class="loading">加载中...</div>
  79. <!-- 控制面板(可选,可以隐藏) -->
  80. <!-- 响应数据(可以设为隐藏,仅用于调试) -->
  81. <div v-if="showDebugInfo" class="response-container">
  82. <div v-if="assistantResponse" class="response-item">
  83. <div class="response-content">
  84. <span>助手回复: {{ assistantResponse }}</span>
  85. </div>
  86. </div>
  87. <div v-if="audioTranscript" class="response-item">
  88. <div class="response-content">
  89. <span>音频转写: {{ audioTranscript }}</span>
  90. </div>
  91. </div>
  92. <div v-for="(item, index) in processedResponses" :key="index" class="response-item">
  93. <div class="response-content">
  94. <span v-if="item.role">角色: {{ item.role }}</span>
  95. <span v-if="item.transcript">文本: {{ item.transcript }}</span>
  96. </div>
  97. </div>
  98. </div>
  99. <!-- 添加停止录制按钮 -->
  100. <div class="stop-recording-button-container" v-if="showStopRecordingButton">
  101. <button class="stop-recording-button" @click="stopRecordingAnswer">
  102. 完成提问
  103. </button>
  104. </div>
  105. <!-- 添加录制指示器 -->
  106. <div class="recording-indicator" v-if="isRecording">
  107. <div class="recording-dot"></div>
  108. <span class="recording-text">正在录制...</span>
  109. <span class="timer-text">{{ recordingTimeDisplay || '00:00 / 05:00' }}</span>
  110. </div>
  111. <!-- 添加开始回答按钮 -->
  112. <div class="start-recording-button-container" v-if="showStartRecordingButton">
  113. <button class="start-recording-button" @click="handleStartRecordingClick">
  114. 开始提问
  115. </button>
  116. </div>
  117. <!-- 添加重试上传按钮 -->
  118. <div class="retry-button-container" v-if="showRetryButton">
  119. <button class="retry-button" @click="retryVideoUpload">
  120. 重新上传
  121. </button>
  122. </div>
  123. <!-- 在模板部分添加录制时长显示 -->
  124. <!-- <div class="recording-timer" v-if="isRecording">
  125. <span class="timer-text">{{ recordingTimeDisplay || '00:00' }}</span>
  126. </div> -->
  127. <!-- 添加上传状态指示器 -->
  128. <!-- <div class="upload-status-indicator" v-if="showUploadStatus && uploadStatusText">
  129. <div class="upload-status-content">
  130. <div class="upload-status-icon" :class="{'uploading': isUploading}"></div>
  131. <span class="upload-status-text">{{ uploadStatusText }}</span>
  132. </div>
  133. </div> -->
  134. <!-- 修改录制时长显示 -->
  135. <!-- <div class="recording-timer" v-if="isRecording">
  136. <span class="timer-text">{{ recordingTimeDisplay || '00:00 / 05:00' }}</span>
  137. <span class="remaining-time" :class="{'warning': remainingTime <= 10}">
  138. 剩余: {{ formatTime(remainingTime) }}
  139. </span>
  140. </div> -->
  141. <!-- 在模板部分添加倒计时蒙层 -->
  142. <div class="countdown-overlay" v-if="showCountdown">
  143. <div class="countdown-content">
  144. <div class="countdown-number">{{ countdownValue }}</div>
  145. <div class="countdown-text">准备开始回答</div>
  146. </div>
  147. </div>
  148. <!-- 在template中添加蒙层 -->
  149. <div class="ai-voice-overlay" v-if="isPlayingAiVoice">
  150. <div class="ai-voice-content">
  151. <div class="ai-voice-icon"></div>
  152. <div class="ai-voice-text">面试官正在回复...</div>
  153. </div>
  154. </div>
  155. <!-- 在模板部分添加继续提问和结束面试按钮 -->
  156. <div class="continue-question-container" v-if="showContinueQuestionButton">
  157. <button class="continue-question-button" @click="handleContinueQuestionClick">
  158. 继续提问
  159. </button>
  160. </div>
  161. <!-- <div class="end-interview-container" v-if="showEndInterviewButton">
  162. <button class="end-interview-button" @click="handleEndInterviewClick">
  163. 结束提问
  164. </button>
  165. </div> -->
  166. <!-- 添加提问轮次显示 -->
  167. <!-- <div class="question-round-indicator" v-if="questionRound > 0">
  168. <span class="round-text">第 {{ questionRound }}/{{ maxQuestionRounds }} 轮提问</span>
  169. </div> -->
  170. </div>
  171. </template>
  172. <script>
  173. import { apiBaseUrl } from '@/common/config.js';
  174. export default {
  175. name: 'IdentityVerify',
  176. data() {
  177. return {
  178. loading: false,
  179. responses: [],
  180. processedResponses: [],
  181. assistantResponse: '',
  182. audioTranscript: '',
  183. videoPlaying: false,
  184. showDebugInfo: false, // 设置为true可以显示调试信息
  185. videoUrl: 'https://data.qicai321.com/minlong/1809457d-30c5-40bb-b6d8-a8c2e8e1e9e2.mp4', //'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/abdaa6fda8494e3a8613304743ed0433.mp4', // 用于存储AI数字人视频URL
  186. showReplayButton: false,
  187. cameraStream: null, // 存储摄像头流
  188. cameraError: null, // 存储摄像头错误信息
  189. useMiniProgramCameraComponent: false, // 添加小程序相机组件标志
  190. cameraContext: null, // 添加相机上下文
  191. currentSubtitle: '',
  192. subtitles: [
  193. {
  194. startTime: 0, // 开始时间(秒)
  195. endTime: 6, // 结束时间(秒)
  196. text: '您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?'
  197. },
  198. ],
  199. // secondVideoSubtitles: [
  200. // {
  201. // startTime: 0,
  202. // endTime: 10,
  203. // text: '请结合您的基本信息与过往履历进行简单的自我介绍,并讲一讲您有哪些优势胜任本岗位:'
  204. // }
  205. // ],
  206. // thirdVideoSubtitles: [
  207. // {
  208. // startTime: 0,
  209. // endTime: 4,
  210. // text: '在工作中,你如何确保个人防护装备的正确使用?'
  211. // }
  212. // ],
  213. // fourthVideoSubtitles: [
  214. // {
  215. // startTime: 0,
  216. // endTime: 4,
  217. // text: '描述一次你与团队合作改善生产流程的经历。'
  218. // }
  219. // ],
  220. // fifthVideoSubtitles: [
  221. // {
  222. // startTime: 0,
  223. // endTime: 6,
  224. // text: '你在团队合作中曾遇到过哪些挑战?如何解决团队内部的分歧?'
  225. // }
  226. // ],
  227. // sixthVideoSubtitles: [
  228. // {
  229. // startTime: 0,
  230. // endTime: 5,
  231. // text: '您已完成本次面试全部题目,请问您对于这个岗位还有什么想要了解的吗?'
  232. // }
  233. // ],
  234. showAnswerButton: false, // 控制答题按钮显示
  235. currentVideoIndex: 0, // 当前播放的视频索引
  236. videoList: [
  237. 'https://data.qicai321.com/minlong/1809457d-30c5-40bb-b6d8-a8c2e8e1e9e2.mp4'//结束
  238. ],
  239. isRecording: false,
  240. recordingTimer: null,
  241. showStopRecordingButton: false,
  242. mediaRecorder: null,
  243. recordedChunks: [],
  244. recorder: null,
  245. lastUploadedVideoUrl: '',
  246. showStartRecordingButton: false,
  247. showRetryButton: false, // 控制重试按钮显示
  248. lastVideoToRetry: null, // 存储上次失败的视频URL,用于重试
  249. recordingStartTime: null, // 录制开始时间
  250. recordingTimerCount: 0, // 录制计时器计数
  251. recordingTimeDisplay: '00:00', // 格式化的录制时间显示
  252. // 添加上传队列相关数据
  253. uploadQueue: [], // 存储待上传的视频
  254. isUploading: false, // 标记是否正在上传
  255. uploadProgress: {}, // 存储每个视频的上传进度
  256. uploadStatus: {}, // 存储每个视频的上传状态
  257. showUploadStatus: false, // 是否显示上传状态指示器
  258. uploadStatusText: '', // 上传状态文本
  259. mediaRecorderTimeout: null, // 用于存储MediaRecorder的超时机制
  260. maxRecordingTime: 300, // 最大录制时间(秒)- 从60秒改为300秒(5分钟)
  261. remainingTime: 300, // 剩余录制时间(秒)- 从60秒改为300秒
  262. // 修改初始视频索引,添加参数控制
  263. startFromLastVideo: false, // 是否从最后一个视频开始播放
  264. countdownValue: 10, // 倒计时数值
  265. showCountdown: false, // 是否显示倒计时蒙层
  266. countdownTimer: null,
  267. showGif: false, // 控制是否显示GIF
  268. gifUrl: '', // GIF图片的URL
  269. // 添加AI语音交互相关属性
  270. aiVoiceUrl: '', // AI语音URL
  271. aiText: '', // AI回复文本
  272. transcribedText: '', // 转写文本
  273. isPlayingAiVoice: false, // 是否正在播放AI语音
  274. aiAudioPlayer: null, // AI语音播放器
  275. // 添加新的状态变量
  276. showContinueQuestionButton: false, // 控制"继续提问"按钮显示
  277. showEndInterviewButton: false, // 控制"结束面试"按钮显示
  278. questionRound: 0, // 当前提问轮次
  279. maxQuestionRounds: 1, // 最大提问轮次(可配置)
  280. conversationHistory: [], // 对话历史记录
  281. pendingAutoNavigation: false, // 标记是否有待处理的自动跳转
  282. hasTriedFallbackMethod: false, // 标记是否尝试过备选方法播放音频
  283. hasShownTextFallbackToast: false, // 标记是否已显示文本回退提示
  284. }
  285. },
  286. onLoad(options) {
  287. // 检查是否有参数指定从最后一个视频开始
  288. if (options && options.startFromLast === 'true') {
  289. this.startFromLastVideo = true;
  290. // 设置为最后一个视频的索引
  291. this.currentVideoIndex = this.videoList.length - 1;
  292. }
  293. },
  294. mounted() {
  295. /* this.fetchData() */
  296. // 如果需要从最后一个视频开始,直接设置
  297. if (this.startFromLastVideo) {
  298. console.log('从最后一个视频开始播放');
  299. this.currentVideoIndex = this.videoList.length - 1;
  300. this.videoUrl = this.videoList[this.currentVideoIndex];
  301. }
  302. this.playDigitalHumanVideo();
  303. this.checkAudioPermission();
  304. this.initCamera();
  305. this.checkIOSCameraRecordPermission();
  306. // 添加防御性检查,避免渲染错误
  307. this.checkAndFixRenderingIssues();
  308. // 添加音频测试
  309. setTimeout(() => {
  310. if (this.cameraStream && !this.useMiniProgramCameraComponent) {
  311. this.testAudioInput();
  312. }
  313. }, 3000);
  314. uni.setKeepScreenOn({
  315. keepScreenOn: true
  316. });
  317. },
  318. beforeDestroy() {
  319. // 组件销毁前停止摄像头
  320. this.stopUserCamera();
  321. // 销毁AI音频播放器
  322. if (this.aiAudioPlayer) {
  323. try {
  324. this.aiAudioPlayer.destroy();
  325. } catch (e) {
  326. console.error('销毁音频播放器失败:', e);
  327. }
  328. this.aiAudioPlayer = null;
  329. }
  330. // 清除所有计时器
  331. if (this.recordingTimer) {
  332. clearInterval(this.recordingTimer);
  333. this.recordingTimer = null;
  334. }
  335. if (this.countdownTimer) {
  336. clearInterval(this.countdownTimer);
  337. this.countdownTimer = null;
  338. }
  339. if (this.mediaRecorderTimeout) {
  340. clearTimeout(this.mediaRecorderTimeout);
  341. this.mediaRecorderTimeout = null;
  342. }
  343. // 如果有正在进行的录制,停止录制
  344. if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
  345. try {
  346. this.mediaRecorder.stop();
  347. } catch (e) {
  348. console.error('停止MediaRecorder失败:', e);
  349. }
  350. }
  351. if (this.recorder) {
  352. try {
  353. this.cameraContext.stopRecord();
  354. } catch (e) {
  355. console.error('停止小程序录制失败:', e);
  356. }
  357. }
  358. // 关闭屏幕常亮
  359. uni.setKeepScreenOn({
  360. keepScreenOn: false
  361. });
  362. },
  363. methods: {
  364. // 初始化相机
  365. async initCamera() {
  366. // 检查平台
  367. const systemInfo = uni.getSystemInfoSync();
  368. // 判断是否在小程序环境中
  369. const isMiniProgram = systemInfo.uniPlatform === 'mp-weixin' ||
  370. systemInfo.uniPlatform === 'mp-alipay' ||
  371. systemInfo.uniPlatform === 'mp-baidu';
  372. if (isMiniProgram) {
  373. // 小程序环境使用小程序的相机API
  374. this.useMiniProgramCameraComponent = true;
  375. // 创建相机上下文
  376. this.cameraContext = uni.createCameraContext();
  377. // 确保已获取录音和相机权限
  378. uni.getSetting({
  379. success: (res) => {
  380. // 检查录音权限
  381. if (!res.authSetting['scope.record']) {
  382. uni.authorize({
  383. scope: 'scope.record',
  384. success: () => {
  385. console.log('录音权限已获取');
  386. },
  387. fail: (err) => {
  388. console.error('录音权限获取失败:', err);
  389. this.showPermissionDialog('录音');
  390. }
  391. });
  392. }
  393. // 检查相机权限
  394. if (!res.authSetting['scope.camera']) {
  395. uni.authorize({
  396. scope: 'scope.camera',
  397. success: () => {
  398. console.log('相机权限已获取');
  399. },
  400. fail: (err) => {
  401. console.error('相机权限获取失败:', err);
  402. this.showPermissionDialog('相机');
  403. }
  404. });
  405. }
  406. // 在iOS上,可能需要额外的权限检查
  407. const systemInfo = uni.getSystemInfoSync();
  408. if (systemInfo.platform === 'ios') {
  409. // 在iOS上,不要使用无效的权限范围
  410. // 移除以下代码块
  411. /*
  412. if (!res.authSetting['scope.camera.record']) {
  413. uni.authorize({
  414. scope: 'scope.camera.record',
  415. success: () => {
  416. console.log('相机录制权限已获取');
  417. },
  418. fail: (err) => {
  419. console.error('相机录制权限获取失败:', err);
  420. this.showPermissionDialog('相机录制');
  421. }
  422. });
  423. }
  424. */
  425. // 确保同时获取了相机和录音权限
  426. if (!res.authSetting['scope.camera'] || !res.authSetting['scope.record']) {
  427. console.log('iOS需要同时获取相机和录音权限');
  428. }
  429. }
  430. }
  431. });
  432. } else {
  433. // H5/App环境使用浏览器的API
  434. try {
  435. // 请求摄像头和麦克风权限并获取媒体流,添加更多约束
  436. const constraints = {
  437. audio: {
  438. echoCancellation: true,
  439. noiseSuppression: true,
  440. autoGainControl: true
  441. },
  442. video: {
  443. width: { ideal: 640, max: 1280 }, // 控制视频宽度
  444. height: { ideal: 480, max: 720 }, // 控制视频高度
  445. frameRate: { ideal: 15, max: 24 }, // 控制帧率
  446. facingMode: 'user'
  447. }
  448. };
  449. const stream = await navigator.mediaDevices.getUserMedia(constraints);
  450. // 保存流以便后续使用
  451. this.cameraStream = stream;
  452. // 检查音频轨道
  453. const audioTracks = stream.getAudioTracks();
  454. console.log('音频轨道数量:', audioTracks.length);
  455. if (audioTracks.length > 0) {
  456. console.log('音频轨道已获取:', audioTracks[0].label);
  457. // 确保音频轨道已启用
  458. audioTracks[0].enabled = true;
  459. } else {
  460. console.warn('未检测到音频轨道,尝试单独获取音频');
  461. this.tryGetAudioOnly();
  462. }
  463. // 将流设置到视频元素
  464. const videoElement = this.$refs.userCameraVideo;
  465. if (videoElement) {
  466. videoElement.srcObject = stream;
  467. videoElement.muted = true; // 避免回声,但仍然录制声音
  468. }
  469. } catch (error) {
  470. console.error('获取摄像头失败:', error);
  471. this.cameraError = error.message || '无法访问摄像头';
  472. // 显示错误提示
  473. uni.showToast({
  474. title: '无法访问摄像头,请检查权限设置',
  475. icon: 'none'
  476. });
  477. }
  478. }
  479. },
  480. // 停止用户摄像头
  481. stopUserCamera() {
  482. if (this.cameraStream) {
  483. // 停止所有轨道
  484. this.cameraStream.getTracks().forEach(track => {
  485. track.stop();
  486. });
  487. this.cameraStream = null;
  488. }
  489. },
  490. async fetchData() {
  491. this.loading = true
  492. this.assistantResponse = ''
  493. this.audioTranscript = ''
  494. this.processedResponses = []
  495. try {
  496. // 使用uni.request代替fetch
  497. const requestTask = uni.request({
  498. url: 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions',
  499. method: 'POST',
  500. header: {
  501. 'Content-Type': 'application/json',
  502. 'Authorization': 'Bearer sk-9e1ec73a7d97493b8613c63f06b6110c'
  503. },
  504. data: {
  505. "model": "qwen-omni-turbo",
  506. "messages": [
  507. {
  508. "role": "user",
  509. "content": [
  510. {
  511. "type": "input_audio",
  512. "input_audio": {
  513. "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
  514. "format": "wav"
  515. }
  516. },
  517. {
  518. "type": "text",
  519. "text": "这段音频在说什么"
  520. }
  521. ]
  522. }
  523. ],
  524. "stream":true,
  525. "stream_options":{
  526. "include_usage":true
  527. },
  528. "modalities":["text","audio"],
  529. "audio":{"voice":"Cherry","format":"wav"}
  530. },
  531. success: (res) => {
  532. console.log('请求成功,响应数据:', res.data);
  533. // 检查响应数据是否包含多个JSON对象
  534. if (typeof res.data === 'string' && res.data.includes('data: {')) {
  535. // 处理包含多个JSON对象的情况
  536. const chunks = res.data.split('data: ').filter(chunk => chunk.trim() !== '');
  537. chunks.forEach(chunk => {
  538. this.handleStreamResponse(chunk);
  539. });
  540. } else {
  541. // 处理单个响应对象的情况
  542. this.handleStreamResponse(res.data);
  543. }
  544. // 模拟获取到数字人视频并播放
  545. this.playDigitalHumanVideo();
  546. },
  547. fail: (err) => {
  548. console.error('请求失败:', err);
  549. },
  550. complete: () => {
  551. this.loading = false;
  552. }
  553. });
  554. } catch (error) {
  555. console.error('获取数据失败:', error);
  556. this.loading = false;
  557. }
  558. },
  559. handleStreamResponse(data) {
  560. // 处理流式响应数据
  561. if (typeof data === 'string') {
  562. // 处理字符串格式的响应
  563. if (data === '[DONE]') return;
  564. try {
  565. // 移除可能存在的换行符和多余空格
  566. const cleanData = data.trim();
  567. // 检查是否是有效的JSON字符串
  568. if (cleanData.startsWith('{') && cleanData.endsWith('}')) {
  569. const jsonData = JSON.parse(cleanData);
  570. this.processStreamChunk(jsonData);
  571. }
  572. } catch (e) {
  573. console.error('解析JSON失败:', e, '原始数据:', data);
  574. }
  575. } else {
  576. // 处理对象格式的响应
  577. this.processStreamChunk(data);
  578. }
  579. },
  580. processStreamChunk(chunk) {
  581. if (chunk.choices && chunk.choices.length > 0) {
  582. const choice = chunk.choices[0];
  583. // 处理助手回复内容
  584. if (choice.delta && choice.delta.content) {
  585. this.assistantResponse += choice.delta.content;
  586. }
  587. // 处理音频转写内容
  588. if (choice.delta && choice.delta.audio && choice.delta.audio.transcript) {
  589. this.audioTranscript += choice.delta.audio.transcript;
  590. }
  591. // 处理角色和音频转写
  592. if (choice.delta) {
  593. const result = {};
  594. if (choice.delta.role) {
  595. result.role = choice.delta.role;
  596. }
  597. if (choice.delta.audio && choice.delta.audio.transcript) {
  598. result.transcript = choice.delta.audio.transcript;
  599. }
  600. if (Object.keys(result).length > 0) {
  601. this.processedResponses.push(result);
  602. }
  603. }
  604. }
  605. },
  606. processResponseData() {
  607. // 处理返回的数据
  608. this.processedResponses = this.responses.map(item => {
  609. const result = {}
  610. // 处理角色信息
  611. if (item.delta && item.delta.role) {
  612. result.role = item.delta.role
  613. }
  614. // 处理音频转写文本
  615. if (item.delta && item.delta.audio && item.delta.audio.transcript) {
  616. result.transcript = item.delta.audio.transcript
  617. }
  618. return result
  619. }).filter(item => Object.keys(item).length > 0)
  620. },
  621. // 播放数字人视频
  622. playDigitalHumanVideo() {
  623. // 设置当前视频
  624. this.videoUrl = this.videoList[this.currentVideoIndex];
  625. this.videoPlaying = true;
  626. console.log(`播放视频 ${this.currentVideoIndex + 1}/${this.videoList.length}: ${this.videoUrl}`);
  627. // 使用 uni.createVideoContext 来控制视频
  628. this.$nextTick(() => {
  629. const videoContext = uni.createVideoContext('myVideo', this);
  630. if (videoContext) {
  631. videoContext.play();
  632. // 设置超时检查,确认视频是否真的在播放
  633. setTimeout(() => {
  634. if (this.videoPlaying && this.$refs.videoPlayer) {
  635. console.log('视频应该正在播放');
  636. } else {
  637. console.log('视频可能未成功播放,尝试替代方案');
  638. this.tryAlternativeVideoPath();
  639. }
  640. }, 1000);
  641. } else {
  642. console.error('无法创建视频上下文');
  643. this.tryAlternativeVideoPath();
  644. }
  645. });
  646. },
  647. // 修改 tryAlternativeVideoPath 方法
  648. tryAlternativeVideoPath() {
  649. console.log('尝试使用替代路径');
  650. // 尝试不同的路径格式
  651. const alternativePaths = [
  652. './static/demo.mp4',
  653. '../static/demo.mp4',
  654. 'static/demo.mp4',
  655. '/static/demo.mp4',
  656. // 添加绝对路径
  657. `${window.location.origin}/static/demo.mp4`
  658. ];
  659. // 获取当前路径索引
  660. const currentPathIndex = alternativePaths.indexOf(this.videoUrl);
  661. const nextPathIndex = (currentPathIndex + 1) % alternativePaths.length;
  662. // 设置下一个路径
  663. this.videoUrl = alternativePaths[nextPathIndex];
  664. console.log('尝试新路径:', this.videoUrl);
  665. this.$nextTick(() => {
  666. const videoContext = uni.createVideoContext('myVideo', this);
  667. if (videoContext) {
  668. videoContext.stop();
  669. videoContext.play();
  670. // 检查是否成功播放
  671. setTimeout(() => {
  672. if (nextPathIndex === alternativePaths.length - 1 && !this.videoPlaying) {
  673. console.log('所有路径均失败,尝试使用uni.getVideoInfo检查视频');
  674. this.checkVideoWithAPI();
  675. }
  676. }, 1000);
  677. }
  678. });
  679. },
  680. // 添加新方法:使用uni API检查视频
  681. checkVideoWithAPI() {
  682. // 尝试使用uni.getVideoInfo API检查视频是否可用
  683. uni.getVideoInfo({
  684. src: '/static/demo.mp4',
  685. success: (res) => {
  686. console.log('视频信息获取成功:', res);
  687. // 如果能获取到视频信息,再次尝试播放
  688. this.videoUrl = '/static/demo.mp4';
  689. this.$nextTick(() => {
  690. const videoContext = uni.createVideoContext('myVideo', this);
  691. if (videoContext) {
  692. videoContext.play();
  693. }
  694. });
  695. },
  696. fail: (err) => {
  697. console.error('视频信息获取失败:', err);
  698. // 最后尝试使用uni.chooseVideo API
  699. this.fallbackToLocalVideo();
  700. }
  701. });
  702. },
  703. // 添加新方法:回退到本地视频
  704. fallbackToLocalVideo() {
  705. console.log('尝试使用本地视频资源');
  706. // 检查平台
  707. const platform = uni.getSystemInfoSync().platform;
  708. if (platform === 'android' || platform === 'ios') {
  709. // 移动端可以尝试使用本地资源
  710. this.videoUrl = platform === 'android' ? 'android.resource://package_name/raw/demo' : 'file:///assets/demo.mp4';
  711. this.$nextTick(() => {
  712. const videoContext = uni.createVideoContext('myVideo', this);
  713. if (videoContext) {
  714. videoContext.play();
  715. }
  716. });
  717. } else {
  718. // 最终回退到静态图片
  719. this.videoPlaying = false;
  720. uni.showToast({
  721. title: '视频加载失败,显示静态图片',
  722. icon: 'none'
  723. });
  724. }
  725. },
  726. // 修改 handleVideoError 方法
  727. handleVideoError(e) {
  728. console.error('视频加载错误:', e);
  729. // 记录更详细的错误信息
  730. if (e && e.detail) {
  731. console.error('详细错误信息:', e.detail);
  732. }
  733. // 检查视频文件是否存在
  734. uni.getFileInfo({
  735. filePath: this.videoUrl.startsWith('/') ? this.videoUrl.substring(1) : this.videoUrl,
  736. success: (res) => {
  737. console.log('文件存在,大小:', res.size);
  738. // 文件存在但播放失败,可能是格式问题
  739. this.tryDifferentFormat();
  740. },
  741. fail: (err) => {
  742. console.error('文件不存在或无法访问:', err);
  743. // 尝试不同路径
  744. this.tryAlternativeVideoPath();
  745. }
  746. });
  747. // 如果多次尝试后仍然失败,显示错误信息
  748. uni.showToast({
  749. title: '视频加载失败,请检查文件是否存在',
  750. icon: 'none',
  751. duration: 2000
  752. });
  753. },
  754. // 添加新方法:尝试不同格式
  755. tryDifferentFormat() {
  756. console.log('尝试不同的视频格式');
  757. // 尝试不同的视频格式
  758. const formats = [
  759. { ext: 'mp4', mime: 'video/mp4' },
  760. { ext: 'webm', mime: 'video/webm' },
  761. { ext: 'ogg', mime: 'video/ogg' },
  762. { ext: 'mov', mime: 'video/quicktime' }
  763. ];
  764. // 获取当前文件名(不含扩展名)
  765. const currentPath = this.videoUrl;
  766. const basePath = currentPath.substring(0, currentPath.lastIndexOf('.')) || '/static/demo';
  767. // 尝试下一个格式
  768. let nextFormat = formats.find(f => !currentPath.endsWith(f.ext));
  769. if (nextFormat) {
  770. this.videoUrl = `${basePath}.${nextFormat.ext}`;
  771. console.log('尝试新格式:', this.videoUrl);
  772. this.$nextTick(() => {
  773. const videoContext = uni.createVideoContext('myVideo', this);
  774. if (videoContext) {
  775. videoContext.stop();
  776. videoContext.play();
  777. }
  778. });
  779. } else {
  780. // 所有格式都尝试过了,使用内置资源
  781. this.useBuiltInResource();
  782. }
  783. },
  784. // 添加新方法:使用内置资源
  785. useBuiltInResource() {
  786. console.log('尝试使用内置资源');
  787. // 检查平台
  788. const platform = uni.getSystemInfoSync().platform;
  789. // 根据平台选择合适的视频源
  790. if (platform === 'windows') {
  791. // Windows平台特定处理
  792. // 尝试使用相对于应用根目录的路径
  793. const appRoot = process.env.UNI_INPUT_DIR || '';
  794. this.videoUrl = `./static/demo.mp4`;
  795. // 或者尝试使用file://协议
  796. // this.videoUrl = `file:///${appRoot.replace(/\\/g, '/')}/static/demo.mp4`;
  797. console.log('Windows平台尝试路径:', this.videoUrl);
  798. } else if (platform === 'android' || platform === 'ios') {
  799. // 移动端
  800. this.useNativeVideo();
  801. } else {
  802. // Web平台
  803. // 尝试使用完整URL
  804. const baseUrl = window.location.origin;
  805. this.videoUrl = `${baseUrl}/static/demo.mp4`;
  806. console.log('Web平台尝试URL:', this.videoUrl);
  807. }
  808. this.$nextTick(() => {
  809. const videoContext = uni.createVideoContext('myVideo', this);
  810. if (videoContext) {
  811. videoContext.play();
  812. }
  813. });
  814. },
  815. // 添加新方法:使用原生视频能力
  816. useNativeVideo() {
  817. console.log('尝试使用原生视频能力');
  818. // 在移动端,可以尝试使用原生视频播放器
  819. uni.chooseVideo({
  820. sourceType: ['album'],
  821. success: (res) => {
  822. this.videoUrl = res.tempFilePath;
  823. this.$nextTick(() => {
  824. const videoContext = uni.createVideoContext('myVideo', this);
  825. if (videoContext) {
  826. videoContext.play();
  827. }
  828. });
  829. },
  830. fail: () => {
  831. // 如果用户取消选择,回退到静态图片
  832. this.videoPlaying = false;
  833. uni.showToast({
  834. title: '无法加载视频,显示静态图片',
  835. icon: 'none'
  836. });
  837. }
  838. });
  839. },
  840. // 处理视频结束事件
  841. handleVideoEnded() {
  842. console.log('视频播放结束,当前视频索引:', this.currentVideoIndex);
  843. this.videoPlaying = false;
  844. /* // 设置对应问题的GIF图片URL
  845. // if (this.currentVideoIndex === 0) {
  846. this.gifUrl = 'http://121.36.251.245:9000/minlong/cae2da8f92ce4af278d3929ab8c5973f.mp4';
  847. // } else if (this.currentVideoIndex === 1) {
  848. // this.gifUrl = 'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/5273282d971441249aeadf35a7574f01.gif';
  849. // } else if (this.currentVideoIndex === 2) {
  850. // this.gifUrl = 'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/5273282d971441249aeadf35a7574f01.gif';
  851. // } else if (this.currentVideoIndex === 3) {
  852. // this.gifUrl = 'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/5273282d971441249aeadf35a7574f01.gif';
  853. // } else if (this.currentVideoIndex === 4) {
  854. // this.gifUrl = 'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/5273282d971441249aeadf35a7574f01.gif';
  855. // } else if (this.currentVideoIndex === 5) {
  856. // this.gifUrl = 'http://121.36.251.245:9000/minlong/tenant_1/general_uploads/5273282d971441249aeadf35a7574f01.gif';
  857. // }
  858. // 显示GIF图片
  859. this.showGif = true; */
  860. // 视频结束后直接显示"开始作答"按钮
  861. this.showStartRecordingButton = true;
  862. this.startCountdown();
  863. this.showAnswerButton = false;
  864. },
  865. // 添加新方法:开始倒计时
  866. startCountdown() {
  867. // 显示倒计时蒙层
  868. this.showCountdown = true;
  869. this.countdownValue = 10;
  870. // 开始倒计时
  871. this.countdownTimer = setInterval(() => {
  872. this.countdownValue--;
  873. if (this.countdownValue <= 0) {
  874. // 倒计时结束,清除定时器
  875. this.clearCountdown();
  876. // 隐藏"开始回答"按钮
  877. this.showStartRecordingButton = false;
  878. // 开始录制
  879. this.startRecordingAnswer();
  880. }
  881. }, 1000);
  882. },
  883. // 添加新方法:清除倒计时
  884. clearCountdown() {
  885. if (this.countdownTimer) {
  886. clearInterval(this.countdownTimer);
  887. this.countdownTimer = null;
  888. }
  889. this.showCountdown = false;
  890. },
  891. // 修改 handleStartRecordingClick 方法,使其可以终止倒计时
  892. handleStartRecordingClick() {
  893. // 隐藏"开始回答"按钮
  894. this.showStartRecordingButton = false;
  895. // 如果倒计时正在进行,清除倒计时
  896. this.clearCountdown();
  897. // 直接开始录制
  898. this.startRecordingAnswer();
  899. },
  900. // 修复 stopRecordingAnswer 方法,确保录制正确停止并上传
  901. stopRecordingAnswer() {
  902. // 如果倒计时正在进行,先清除倒计时
  903. this.clearCountdown();
  904. // 检查录制时长
  905. const recordingDuration = this.getRecordingDuration();
  906. const minimumDuration = 5; // 最小录制时长(秒)
  907. if (recordingDuration < minimumDuration) {
  908. // 录制时间过短,显示提示
  909. uni.showToast({
  910. title: '录制时间过短,请至少录制5秒',
  911. icon: 'none',
  912. duration: 2000
  913. });
  914. // 不执行停止录制逻辑,继续录制
  915. return;
  916. }
  917. // 隐藏停止录制按钮
  918. this.showStopRecordingButton = false;
  919. // 停止录制计时器
  920. if (this.recordingTimer) {
  921. clearInterval(this.recordingTimer);
  922. this.recordingTimer = null;
  923. }
  924. // 根据平台选择不同的停止录制方法
  925. const systemInfo = uni.getSystemInfoSync();
  926. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  927. if (isMiniProgram) {
  928. // 小程序环境使用小程序的停止录制API
  929. this.stopMiniProgramRecording();
  930. } else {
  931. // H5/App环境使用MediaRecorder API
  932. this.stopBrowserRecording();
  933. }
  934. // 标记录制已停止
  935. this.isRecording = false;
  936. },
  937. // 添加新方法:开始录制用户回答
  938. startRecordingAnswer() {
  939. console.log('开始录制用户回答');
  940. this.isRecording = true;
  941. // 记录录制开始时间
  942. this.recordingStartTime = Date.now();
  943. this.recordingTimerCount = 0;
  944. this.remainingTime = this.maxRecordingTime;
  945. // 启动计时器,每秒更新计时
  946. this.recordingTimer = setInterval(() => {
  947. this.recordingTimerCount++;
  948. this.remainingTime = Math.max(0, this.maxRecordingTime - this.recordingTimerCount);
  949. // 更新UI显示录制时长和剩余时间
  950. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount) +
  951. ' / ' + this.formatTime(this.maxRecordingTime);
  952. // 检查是否达到最大录制时间
  953. if (this.recordingTimerCount >= this.maxRecordingTime) {
  954. console.log('已达到最大录制时间(60秒),自动停止录制');
  955. this.stopRecordingAnswer();
  956. }
  957. }, 1000);
  958. // 显示录制中的提示
  959. // uni.showLoading({
  960. // title: '正在录制...',
  961. // mask: true
  962. // });
  963. // 根据平台选择不同的录制方法
  964. const systemInfo = uni.getSystemInfoSync();
  965. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  966. if (isMiniProgram) {
  967. // 小程序环境使用小程序的录制API
  968. this.startMiniProgramRecording();
  969. } else {
  970. // H5/App环境使用MediaRecorder API
  971. this.startBrowserRecording();
  972. }
  973. // 显示停止录制按钮
  974. this.showStopRecordingButton = true;
  975. },
  976. // 添加一个新方法:重置相机组件
  977. resetCamera() {
  978. console.log('重置相机组件');
  979. // 先完全移除相机组件
  980. this.useMiniProgramCameraComponent = false;
  981. // 释放相机上下文
  982. if (this.cameraContext) {
  983. this.cameraContext = null;
  984. }
  985. // 延迟后重新创建相机组件
  986. setTimeout(() => {
  987. this.useMiniProgramCameraComponent = true;
  988. // 再次延迟创建相机上下文,确保组件已完全渲染
  989. setTimeout(() => {
  990. this.cameraContext = uni.createCameraContext();
  991. console.log('相机组件已重置');
  992. }, 500);
  993. }, 500);
  994. },
  995. // 修改 startMiniProgramRecording 方法
  996. startMiniProgramRecording() {
  997. console.log('开始小程序录制方法');
  998. // 获取平台信息
  999. const systemInfo = uni.getSystemInfoSync();
  1000. const isIOS = systemInfo.platform === 'ios';
  1001. // 在iOS上,先重置相机
  1002. if (isIOS) {
  1003. // 先重置相机组件
  1004. this.resetCamera();
  1005. // 延迟执行录制,给相机组件足够的初始化时间
  1006. setTimeout(() => {
  1007. this.actualStartRecording(isIOS);
  1008. }, 1000);
  1009. } else {
  1010. // Android直接开始
  1011. this.actualStartRecording(isIOS);
  1012. }
  1013. },
  1014. // 添加新方法:实际开始录制
  1015. actualStartRecording(isIOS) {
  1016. if (!this.cameraContext) {
  1017. this.cameraContext = uni.createCameraContext();
  1018. console.log('创建新的相机上下文');
  1019. }
  1020. // 确保有录音权限
  1021. uni.getSetting({
  1022. success: (res) => {
  1023. const hasRecordAuth = res.authSetting['scope.record'];
  1024. const hasCameraAuth = res.authSetting['scope.camera'];
  1025. if (!hasRecordAuth || !hasCameraAuth) {
  1026. console.warn('缺少必要权限,请求权限');
  1027. this.requestMiniProgramPermissions();
  1028. return;
  1029. }
  1030. // 在iOS上,先检查相机状态
  1031. if (isIOS) {
  1032. console.log('iOS: 检查相机状态');
  1033. // 使用最简单的选项,设置最大录制时间为5分钟
  1034. const options = {
  1035. timeout: 300000, // 300秒超时 (5分钟)
  1036. quality: 'low', // 降低质量
  1037. compressed: true,
  1038. success: () => {
  1039. console.log('iOS录制开始成功');
  1040. },
  1041. fail: (err) => {
  1042. console.error('iOS录制失败:', err);
  1043. // 如果失败,尝试使用不同的方法
  1044. this.useAlternativeRecordingMethod();
  1045. }
  1046. };
  1047. try {
  1048. console.log('尝试开始录制');
  1049. this.recorder = this.cameraContext.startRecord(options);
  1050. } catch (e) {
  1051. console.error('开始录制异常:', e);
  1052. this.useAlternativeRecordingMethod();
  1053. }
  1054. } else {
  1055. // Android使用标准选项,设置最大录制时间为5分钟
  1056. const options = {
  1057. timeout: 300000, // 300秒超时 (5分钟)
  1058. quality: 'medium',
  1059. compressed: true,
  1060. success: () => {
  1061. console.log('Android录制开始成功');
  1062. },
  1063. fail: (err) => {
  1064. console.error('Android录制失败:', err);
  1065. uni.showToast({
  1066. title: '录制失败,请检查相机权限',
  1067. icon: 'none'
  1068. });
  1069. this.proceedToNextQuestion();
  1070. }
  1071. };
  1072. this.recorder = this.cameraContext.startRecord(options);
  1073. }
  1074. }
  1075. });
  1076. },
  1077. // 添加新方法:使用替代录制方法
  1078. useAlternativeRecordingMethod() {
  1079. console.log('使用替代录制方法');
  1080. // 在iOS上,可以尝试使用chooseVideo API作为备选
  1081. uni.showActionSheet({
  1082. itemList: ['使用相册中的视频', '跳过此问题'],
  1083. success: (res) => {
  1084. if (res.tapIndex === 0) {
  1085. // 选择相册中的视频
  1086. uni.chooseVideo({
  1087. sourceType: ['album'],
  1088. maxDuration: 300, // 从60秒改为300秒
  1089. camera: 'front',
  1090. success: (res) => {
  1091. console.log('选择视频成功:', res.tempFilePath);
  1092. // 停止录制状态
  1093. this.isRecording = false;
  1094. this.showStopRecordingButton = false;
  1095. // 上传选择的视频
  1096. this.uploadRecordedVideo(res.tempFilePath);
  1097. },
  1098. fail: () => {
  1099. console.log('用户取消选择视频');
  1100. this.proceedToNextQuestion();
  1101. }
  1102. });
  1103. } else {
  1104. // 跳过此问题
  1105. console.log('用户选择跳过问题');
  1106. this.proceedToNextQuestion();
  1107. }
  1108. },
  1109. fail: () => {
  1110. console.log('操作取消');
  1111. this.proceedToNextQuestion();
  1112. }
  1113. });
  1114. },
  1115. // 添加新方法:请求小程序权限
  1116. requestMiniProgramPermissions() {
  1117. // 请求录音权限
  1118. uni.authorize({
  1119. scope: 'scope.record',
  1120. success: () => {
  1121. console.log('录音权限已获取');
  1122. // 请求相机权限
  1123. uni.authorize({
  1124. scope: 'scope.camera',
  1125. success: () => {
  1126. console.log('相机权限已获取');
  1127. // 权限都获取到后,开始录制
  1128. this.startMiniProgramRecording();
  1129. },
  1130. fail: (err) => {
  1131. console.error('相机权限获取失败:', err);
  1132. this.showPermissionDialog('相机');
  1133. }
  1134. });
  1135. },
  1136. fail: (err) => {
  1137. console.error('录音权限获取失败:', err);
  1138. this.showPermissionDialog('录音');
  1139. }
  1140. });
  1141. },
  1142. // 修改浏览器环境下的录制方法
  1143. startBrowserRecording() {
  1144. if (!this.cameraStream) {
  1145. console.error('没有可用的摄像头流');
  1146. uni.showToast({
  1147. title: '录制失败,摄像头未就绪',
  1148. icon: 'none'
  1149. });
  1150. this.proceedToNextQuestion();
  1151. return;
  1152. }
  1153. try {
  1154. // 检查流中是否包含音频轨道
  1155. const hasAudio = this.cameraStream.getAudioTracks().length > 0;
  1156. if (!hasAudio) {
  1157. console.warn('警告:媒体流中没有音频轨道,尝试重新获取带音频的媒体流');
  1158. // 尝试重新获取带音频的媒体流
  1159. navigator.mediaDevices.getUserMedia({
  1160. audio: {
  1161. echoCancellation: true,
  1162. noiseSuppression: true,
  1163. autoGainControl: true
  1164. },
  1165. video: true
  1166. })
  1167. .then(newStream => {
  1168. // 检查新流是否包含音频轨道
  1169. const audioTracks = newStream.getAudioTracks();
  1170. if (audioTracks.length > 0) {
  1171. console.log('成功获取音频轨道:', audioTracks[0].label);
  1172. // 合并视频和音频轨道
  1173. const videoTrack = this.cameraStream.getVideoTracks()[0];
  1174. const audioTrack = newStream.getAudioTracks()[0];
  1175. // 创建新的媒体流,包含视频和音频轨道
  1176. const combinedStream = new MediaStream();
  1177. if (videoTrack) combinedStream.addTrack(videoTrack);
  1178. if (audioTrack) combinedStream.addTrack(audioTrack);
  1179. this.cameraStream = combinedStream;
  1180. // 更新视频元素的源
  1181. const videoElement = this.$refs.userCameraVideo;
  1182. if (videoElement) {
  1183. videoElement.srcObject = combinedStream;
  1184. videoElement.muted = true; // 避免回声,但仍然录制声音
  1185. }
  1186. // 继续录制过程
  1187. this.setupMediaRecorder(combinedStream);
  1188. } else {
  1189. console.warn('仍然无法获取音频轨道');
  1190. // 使用现有流继续
  1191. this.setupMediaRecorder(this.cameraStream);
  1192. }
  1193. })
  1194. .catch(err => {
  1195. console.error('获取音频失败:', err);
  1196. // 使用现有流继续
  1197. this.setupMediaRecorder(this.cameraStream);
  1198. });
  1199. } else {
  1200. console.log('检测到音频轨道,直接使用');
  1201. // 已有音频轨道,直接使用
  1202. this.setupMediaRecorder(this.cameraStream);
  1203. }
  1204. } catch (error) {
  1205. console.error('浏览器录制失败:', error);
  1206. uni.showToast({
  1207. title: '录制失败,浏览器可能不支持此功能',
  1208. icon: 'none'
  1209. });
  1210. this.proceedToNextQuestion();
  1211. }
  1212. },
  1213. // 修改 setupMediaRecorder 方法
  1214. setupMediaRecorder(stream) {
  1215. // 检查流中的轨道
  1216. const videoTracks = stream.getVideoTracks();
  1217. const audioTracks = stream.getAudioTracks();
  1218. console.log('设置MediaRecorder - 视频轨道:', videoTracks.length, '音频轨道:', audioTracks.length);
  1219. // 尝试使用支持度更广的编码格式
  1220. let mimeType = '';
  1221. const supportedTypes = [
  1222. 'video/webm;codecs=vp9,opus',
  1223. 'video/webm;codecs=vp8,opus',
  1224. 'video/webm;codecs=h264,opus',
  1225. 'video/mp4;codecs=h264,aac',
  1226. 'video/webm',
  1227. 'video/mp4'
  1228. ];
  1229. for (const type of supportedTypes) {
  1230. if (MediaRecorder.isTypeSupported(type)) {
  1231. mimeType = type;
  1232. console.log('使用支持的MIME类型:', mimeType);
  1233. break;
  1234. }
  1235. }
  1236. // 创建MediaRecorder实例,设置较低的比特率以减小文件大小
  1237. const options = {
  1238. mimeType: mimeType || '',
  1239. audioBitsPerSecond: 64000, // 降低音频比特率
  1240. videoBitsPerSecond: 1000000, // 降低视频比特率到1Mbps
  1241. };
  1242. try {
  1243. this.mediaRecorder = new MediaRecorder(stream, options);
  1244. console.log('MediaRecorder创建成功,使用选项:', options);
  1245. } catch (e) {
  1246. console.warn('使用指定选项创建MediaRecorder失败,尝试使用默认选项');
  1247. this.mediaRecorder = new MediaRecorder(stream);
  1248. }
  1249. // 存储录制的数据块
  1250. this.recordedChunks = [];
  1251. // 监听数据可用事件
  1252. this.mediaRecorder.ondataavailable = (event) => {
  1253. if (event.data && event.data.size > 0) {
  1254. this.recordedChunks.push(event.data);
  1255. console.log(`收到数据块: ${event.data.size} 字节`);
  1256. }
  1257. };
  1258. // 监听录制停止事件
  1259. this.mediaRecorder.onstop = async () => {
  1260. console.log('MediaRecorder停止,数据块数量:', this.recordedChunks.length);
  1261. if (this.recordedChunks.length === 0) {
  1262. console.error('没有录制到数据');
  1263. uni.showToast({
  1264. title: '录制失败,未捕获到数据',
  1265. icon: 'none'
  1266. });
  1267. this.proceedToNextQuestion();
  1268. return;
  1269. }
  1270. // 创建Blob对象
  1271. const mimeType = this.mediaRecorder.mimeType || 'video/webm';
  1272. const blob = new Blob(this.recordedChunks, { type: mimeType });
  1273. console.log('创建Blob,原始大小:', blob.size, '类型:', mimeType);
  1274. // 显示压缩中提示
  1275. uni.showLoading({
  1276. title: '正在处理视频...',
  1277. mask: true
  1278. });
  1279. try {
  1280. // 压缩视频
  1281. const compressedBlob = await this.compressVideo(blob);
  1282. // 将Blob转换为File对象
  1283. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1284. const file = new File([compressedBlob], fileName, { type: mimeType });
  1285. // 隐藏压缩提示
  1286. uni.hideLoading();
  1287. // 上传文件
  1288. this.uploadRecordedVideo(file);
  1289. } catch (error) {
  1290. console.error('视频处理失败:', error);
  1291. uni.hideLoading();
  1292. // 如果压缩失败,使用原始视频
  1293. const fileName = `answer_${this.currentVideoIndex}_${Date.now()}.webm`;
  1294. const file = new File([blob], fileName, { type: mimeType });
  1295. this.uploadRecordedVideo(file);
  1296. }
  1297. };
  1298. // 监听错误
  1299. this.mediaRecorder.onerror = (event) => {
  1300. console.error('MediaRecorder错误:', event.error);
  1301. };
  1302. // 开始录制
  1303. try {
  1304. this.mediaRecorder.start(1000); // 每秒触发一次dataavailable事件
  1305. console.log('MediaRecorder开始录制');
  1306. // 设置最大录制时间为60秒
  1307. // 注意:我们已经在startRecordingAnswer方法中设置了全局计时器
  1308. // 这里作为备份机制,确保即使全局计时器失效,也能停止录制
  1309. this.mediaRecorderTimeout = setTimeout(() => {
  1310. if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
  1311. console.log('MediaRecorder备份超时机制触发,停止录制');
  1312. this.mediaRecorder.stop();
  1313. }
  1314. }, 300000); // 300秒 (5分钟)
  1315. } catch (e) {
  1316. console.error('开始录制失败:', e);
  1317. }
  1318. },
  1319. // 添加新方法:停止录制用户回答
  1320. stopRecordingAnswer() {
  1321. console.log('停止录制用户回答');
  1322. if (this.countdownTimer) {
  1323. clearInterval(this.countdownTimer);
  1324. this.countdownTimer = null;
  1325. this.showCountdown = false;
  1326. }
  1327. // 检查录制时长
  1328. const recordingDuration = this.getRecordingDuration();
  1329. const minimumDuration = 5; // 最小录制时长(秒),从3秒改为5秒
  1330. if (recordingDuration < minimumDuration) {
  1331. // 录制时间过短,显示提示
  1332. uni.showToast({
  1333. title: '录制时间过短,请至少录制5秒',
  1334. icon: 'none',
  1335. duration: 2000
  1336. });
  1337. // 不执行停止录制逻辑,继续录制
  1338. return;
  1339. }
  1340. // 隐藏停止录制按钮
  1341. this.showStopRecordingButton = false;
  1342. // 停止录制计时器
  1343. if (this.recordingTimer) {
  1344. clearInterval(this.recordingTimer);
  1345. this.recordingTimer = null;
  1346. }
  1347. // 根据平台选择不同的停止录制方法
  1348. const systemInfo = uni.getSystemInfoSync();
  1349. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1350. if (isMiniProgram) {
  1351. // 小程序环境使用小程序的停止录制API
  1352. this.stopMiniProgramRecording();
  1353. } else {
  1354. // H5/App环境使用MediaRecorder API
  1355. this.stopBrowserRecording();
  1356. }
  1357. // 标记录制已停止
  1358. this.isRecording = false;
  1359. },
  1360. // 添加新方法:获取录制时长
  1361. getRecordingDuration() {
  1362. // 如果有明确的录制开始时间,计算实际录制时长
  1363. if (this.recordingStartTime) {
  1364. return (Date.now() - this.recordingStartTime) / 1000;
  1365. }
  1366. // 如果没有明确记录开始时间,尝试从视频元素获取
  1367. if (this.mediaRecorder && this.$refs.userCameraVideo) {
  1368. return this.$refs.userCameraVideo.currentTime || 0;
  1369. }
  1370. // 如果无法获取准确时长,返回估计值(如果有计时器)
  1371. if (this.recordingTimerCount) {
  1372. return this.recordingTimerCount;
  1373. }
  1374. // 默认返回0
  1375. return 0;
  1376. },
  1377. // 添加新方法:重置录制
  1378. resetRecording() {
  1379. // 保持录制状态,但重置计时器
  1380. if (this.recordingTimer) {
  1381. clearTimeout(this.recordingTimer);
  1382. }
  1383. // 重置录制开始时间
  1384. this.recordingStartTime = Date.now();
  1385. this.recordingTimerCount = 0;
  1386. // 如果是浏览器环境,需要重置MediaRecorder
  1387. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  1388. // 停止当前录制
  1389. this.mediaRecorder.stop();
  1390. // 清空已录制的数据块
  1391. this.recordedChunks = [];
  1392. // 短暂延迟后重新开始录制
  1393. setTimeout(() => {
  1394. this.startBrowserRecording();
  1395. }, 500);
  1396. }
  1397. // 如果是小程序环境,需要重置相机录制
  1398. else if (this.cameraContext) {
  1399. // 停止当前录制
  1400. this.cameraContext.stopRecord({
  1401. success: () => {
  1402. console.log('重置录制:停止当前录制成功');
  1403. // 短暂延迟后重新开始录制
  1404. setTimeout(() => {
  1405. this.startMiniProgramRecording();
  1406. }, 500);
  1407. },
  1408. fail: (err) => {
  1409. console.error('重置录制:停止当前录制失败', err);
  1410. // 尝试直接重新开始录制
  1411. this.startMiniProgramRecording();
  1412. }
  1413. });
  1414. }
  1415. // 显示提示
  1416. uni.showToast({
  1417. title: '请重新开始回答',
  1418. icon: 'none',
  1419. duration: 2000
  1420. });
  1421. },
  1422. // 修复 completeRecordingStop 方法,确保视频正确上传和处理
  1423. completeRecordingStop() {
  1424. console.log('完成录制停止');
  1425. // 隐藏停止录制按钮
  1426. this.showStopRecordingButton = false;
  1427. // 停止录制计时器
  1428. if (this.recordingTimer) {
  1429. clearInterval(this.recordingTimer);
  1430. this.recordingTimer = null;
  1431. }
  1432. // 标记录制已停止
  1433. this.isRecording = false;
  1434. // 显示上传中状态
  1435. this.showUploadStatus = true;
  1436. this.uploadStatusText = '正在处理视频...';
  1437. this.isUploading = true;
  1438. // 根据平台选择不同的停止录制方法
  1439. const systemInfo = uni.getSystemInfoSync();
  1440. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  1441. if (isMiniProgram) {
  1442. // 小程序环境使用小程序的停止录制API
  1443. this.stopMiniProgramRecording();
  1444. } else {
  1445. // H5/App环境使用MediaRecorder API
  1446. this.stopBrowserRecording();
  1447. }
  1448. },
  1449. // 修复 stopMiniProgramRecording 方法,确保小程序环境下正确停止录制
  1450. stopMiniProgramRecording() {
  1451. console.log('停止小程序录制');
  1452. if (!this.cameraContext) {
  1453. console.error('相机上下文不存在');
  1454. this.handleRecordingError(new Error('相机上下文不存在'));
  1455. return;
  1456. }
  1457. try {
  1458. // 显示加载提示
  1459. uni.showLoading({
  1460. title: '正在处理视频...',
  1461. mask: true
  1462. });
  1463. // 停止录制
  1464. this.cameraContext.stopRecord({
  1465. success: (res) => {
  1466. console.log('停止录制成功:', res);
  1467. // 隐藏加载提示
  1468. uni.hideLoading();
  1469. // 获取视频临时路径
  1470. const tempVideoPath = res.tempVideoPath || res.tempFilePath;
  1471. if (tempVideoPath) {
  1472. console.log('获取到视频临时路径:', tempVideoPath);
  1473. // 上传视频
  1474. this.uploadRecordedVideo(tempVideoPath);
  1475. } else {
  1476. console.error('未获取到视频临时路径');
  1477. this.handleRecordingError(new Error('未获取到视频临时路径'));
  1478. }
  1479. },
  1480. fail: (err) => {
  1481. console.error('停止录制失败:', err);
  1482. uni.hideLoading();
  1483. this.handleRecordingError(err);
  1484. }
  1485. });
  1486. } catch (e) {
  1487. console.error('停止录制异常:', e);
  1488. uni.hideLoading();
  1489. this.handleRecordingError(e);
  1490. }
  1491. },
  1492. // 添加新方法:停止浏览器录制
  1493. stopBrowserRecording() {
  1494. if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
  1495. this.mediaRecorder.stop();
  1496. console.log('浏览器录制停止成功');
  1497. } else {
  1498. console.error('MediaRecorder不存在或已经停止');
  1499. this.proceedToNextQuestion();
  1500. }
  1501. },
  1502. // 修复 uploadRecordedVideo 方法,确保视频正确上传到服务器
  1503. uploadRecordedVideo(videoPath) {
  1504. console.log('开始上传录制的视频:', videoPath);
  1505. // 保存最后上传的视频路径,用于可能的重试
  1506. this.lastVideoToRetry = videoPath;
  1507. // 显示上传状态
  1508. this.showUploadStatus = true;
  1509. this.uploadStatusText = '正在上传视频...';
  1510. this.isUploading = true;
  1511. // 保存上传状态到本地存储
  1512. this.saveUploadStatus();
  1513. // 获取用户信息和租户ID
  1514. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  1515. const openid = uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')).openid : '';
  1516. const application_id = uni.getStorageSync('appId') || '';
  1517. // 使用uni.uploadFile上传视频
  1518. const uploadTask = uni.uploadFile({
  1519. url: `${apiBaseUrl}/api/upload/`,
  1520. filePath: videoPath,
  1521. name: 'file',
  1522. formData: {
  1523. tenant_id: tenant_id,
  1524. openid: openid,
  1525. application_id: application_id,
  1526. biz_type: 'interview_video',
  1527. video_duration: this.recordingTimerCount || 0,
  1528. has_audio: 'true' // 明确标记包含音频
  1529. },
  1530. success: (uploadRes) => {
  1531. console.log('视频上传成功:', uploadRes);
  1532. try {
  1533. // 解析响应数据
  1534. const result = typeof uploadRes.data === 'string' ? JSON.parse(uploadRes.data) : uploadRes.data;
  1535. if ((result.success || result.code === 2000) && (result.data?.url || result.data?.permanent_link)) {
  1536. // 上传成功,获取视频URL
  1537. const videoUrl = result.data.url || result.data.permanent_link || '';
  1538. console.log('视频URL:', videoUrl);
  1539. // 更新上传状态
  1540. this.uploadStatusText = '视频处理完成,正在分析...';
  1541. // 保存上传状态
  1542. this.saveUploadStatus();
  1543. // 提交视频到AI语音交互接口
  1544. this.submitToAiVoiceInteraction(videoUrl);
  1545. } else {
  1546. console.error('上传响应格式错误:', result);
  1547. const errorMsg = result.msg || result.message || '上传响应格式错误';
  1548. this.handleUploadError(new Error(errorMsg));
  1549. }
  1550. } catch (e) {
  1551. console.error('解析上传响应失败:', e);
  1552. this.handleUploadError(e);
  1553. }
  1554. },
  1555. fail: (err) => {
  1556. console.error('视频上传失败:', err);
  1557. // 提供更友好的错误信息
  1558. let errorMessage = '网络错误,请检查网络连接';
  1559. if (err.errMsg) {
  1560. if (err.errMsg.includes('timeout')) {
  1561. errorMessage = '上传超时,请检查网络连接';
  1562. } else if (err.errMsg.includes('fail')) {
  1563. errorMessage = '上传失败: ' + err.errMsg;
  1564. }
  1565. }
  1566. this.handleUploadError(new Error(errorMessage));
  1567. },
  1568. complete: () => {
  1569. // 隐藏上传状态
  1570. this.showUploadStatus = false;
  1571. this.isUploading = false;
  1572. // 更新上传状态
  1573. this.saveUploadStatus();
  1574. }
  1575. });
  1576. // 监听上传进度
  1577. uploadTask.onProgressUpdate((res) => {
  1578. console.log('上传进度:', res.progress);
  1579. this.uploadStatusText = `正在上传视频... ${res.progress}%`;
  1580. // 如果上传速度过慢,提供提示
  1581. if (res.progress < 10 && res.totalTimeMs > 10000) {
  1582. this.uploadStatusText = `上传速度较慢,请耐心等待... ${res.progress}%`;
  1583. }
  1584. });
  1585. },
  1586. // 修改 processUploadQueue 方法,添加后台上传支持
  1587. processUploadQueue() {
  1588. // 如果队列为空,结束处理
  1589. if (this.uploadQueue.length === 0) {
  1590. this.isUploading = false;
  1591. this.showUploadStatus = false;
  1592. // 清除上传状态
  1593. try {
  1594. uni.removeStorageSync('videoUploadStatus');
  1595. } catch (e) {
  1596. console.error('清除上传状态失败:', e);
  1597. }
  1598. // 发送上传完成通知
  1599. this.notifyUploadComplete();
  1600. return;
  1601. }
  1602. // 标记为正在上传
  1603. this.isUploading = true;
  1604. this.showUploadStatus = true;
  1605. // 获取队列中的第一个任务
  1606. const task = this.uploadQueue[0];
  1607. // 更新任务状态
  1608. this.uploadStatus[task.id] = 'uploading';
  1609. this.updateUploadStatusText();
  1610. // 增加尝试次数
  1611. task.attempts++;
  1612. // 根据文件类型选择上传方法
  1613. if (typeof task.file !== 'string') {
  1614. // 浏览器环境,使用XMLHttpRequest上传
  1615. this.uploadFileWithXHR(task);
  1616. } else {
  1617. // 小程序环境,使用uni.uploadFile上传
  1618. this.uploadFileWithUni(task);
  1619. }
  1620. // 更新上传状态到本地存储
  1621. this.saveUploadStatus();
  1622. },
  1623. // 添加新方法:发送上传完成通知
  1624. notifyUploadComplete() {
  1625. // 使用uni.postMessage在页面间通信(小程序环境)
  1626. try {
  1627. uni.$emit('videoUploadComplete', {
  1628. timestamp: Date.now()
  1629. });
  1630. console.log('发送上传完成通知');
  1631. } catch (e) {
  1632. console.error('发送上传完成通知失败:', e);
  1633. }
  1634. // 如果在App环境,可以使用本地通知
  1635. const systemInfo = uni.getSystemInfoSync();
  1636. if (systemInfo.platform === 'android' || systemInfo.platform === 'ios') {
  1637. // 检查是否支持本地通知
  1638. if (plus && plus.push) {
  1639. plus.push.createMessage('视频上传完成', '您的面试视频已成功上传', {});
  1640. }
  1641. }
  1642. },
  1643. // 添加新方法:使用XMLHttpRequest上传文件
  1644. uploadFileWithXHR(task) {
  1645. // 获取用户信息
  1646. const userInfo = uni.getStorageSync('userInfo');
  1647. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  1648. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  1649. // 创建FormData
  1650. const formData = new FormData();
  1651. formData.append('file', task.file);
  1652. formData.append('openid', openid);
  1653. formData.append('tenant_id', tenant_id);
  1654. formData.append('application_id', uni.getStorageSync('appId'));
  1655. formData.append('question_id', task.questionId);
  1656. formData.append('video_duration', this.recordingTimerCount || 0);
  1657. formData.append('has_audio', 'true'); // 明确标记包含音频
  1658. // 创建XMLHttpRequest
  1659. const xhr = new XMLHttpRequest();
  1660. xhr.open('POST', `${apiBaseUrl}/api/upload/`, true);
  1661. xhr.timeout = 120000; // 2分钟超时
  1662. // 监听上传进度
  1663. xhr.upload.onprogress = (event) => {
  1664. if (event.lengthComputable) {
  1665. const percentComplete = Math.round((event.loaded / event.total) * 100);
  1666. this.uploadProgress[task.id] = percentComplete;
  1667. this.updateUploadStatusText();
  1668. }
  1669. };
  1670. // 监听请求完成
  1671. xhr.onload = () => {
  1672. if (xhr.status === 200) {
  1673. try {
  1674. const res = JSON.parse(xhr.responseText);
  1675. console.log('上传响应:', res);
  1676. if (res.code === 2000) {
  1677. // 获取上传后的视频URL
  1678. const videoUrl = res.data.url || res.data.photoUrl || '';
  1679. if (videoUrl) {
  1680. // 上传成功,更新状态
  1681. this.uploadStatus[task.id] = 'success';
  1682. this.updateUploadStatusText();
  1683. // 提交到面试接口
  1684. this.submitVideoToInterview(videoUrl, task);
  1685. } else {
  1686. this.handleUploadFailure(task, '视频URL获取失败');
  1687. }
  1688. } else {
  1689. this.handleUploadFailure(task, res.msg || '上传失败');
  1690. }
  1691. } catch (e) {
  1692. this.handleUploadFailure(task, '解析响应失败');
  1693. }
  1694. } else {
  1695. this.handleUploadFailure(task, 'HTTP状态: ' + xhr.status);
  1696. }
  1697. };
  1698. // 监听错误
  1699. xhr.onerror = () => {
  1700. this.handleUploadFailure(task, '网络错误');
  1701. };
  1702. // 监听超时
  1703. xhr.ontimeout = () => {
  1704. this.handleUploadFailure(task, '上传超时');
  1705. };
  1706. // 发送请求
  1707. xhr.send(formData);
  1708. },
  1709. // 添加新方法:使用uni.uploadFile上传文件
  1710. uploadFileWithUni(task) {
  1711. // 获取用户信息
  1712. const userInfo = uni.getStorageSync('userInfo');
  1713. const openid = userInfo ? (JSON.parse(userInfo).openid || '') : '';
  1714. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  1715. // 创建上传任务
  1716. const uploadTask = uni.uploadFile({
  1717. url: `${apiBaseUrl}/api/upload/`,
  1718. filePath: task.file,
  1719. name: 'file',
  1720. formData: {
  1721. openid: openid,
  1722. tenant_id: tenant_id,
  1723. application_id: uni.getStorageSync('appId'),
  1724. question_id: task.questionId,
  1725. video_duration: this.recordingTimerCount || 0,
  1726. has_audio: 'true' // 明确标记包含音频
  1727. },
  1728. success: (uploadRes) => {
  1729. try {
  1730. const res = JSON.parse(uploadRes.data);
  1731. console.log('上传响应:', res);
  1732. if (res.code === 2000) {
  1733. // 获取上传后的视频URL
  1734. const videoUrl = res.data.permanent_link || res.data.url || '';
  1735. if (videoUrl) {
  1736. // 上传成功,更新状态
  1737. this.uploadStatus[task.id] = 'success';
  1738. this.updateUploadStatusText();
  1739. // 提交到面试接口
  1740. this.submitVideoToInterview(videoUrl, task);
  1741. } else {
  1742. this.handleUploadFailure(task, '视频URL获取失败');
  1743. }
  1744. } else {
  1745. this.handleUploadFailure(task, res.msg || '上传失败');
  1746. }
  1747. } catch (e) {
  1748. this.handleUploadFailure(task, '解析响应失败');
  1749. }
  1750. },
  1751. fail: (err) => {
  1752. this.handleUploadFailure(task, err.errMsg || '上传失败');
  1753. }
  1754. });
  1755. // 监听上传进度
  1756. uploadTask.onProgressUpdate((res) => {
  1757. this.uploadProgress[task.id] = res.progress;
  1758. this.updateUploadStatusText();
  1759. });
  1760. },
  1761. // 添加新方法:处理上传失败
  1762. handleUploadFailure(task, errorMsg) {
  1763. console.error('上传失败:', errorMsg);
  1764. // 更新任务状态
  1765. this.uploadStatus[task.id] = 'failed';
  1766. this.updateUploadStatusText();
  1767. // 检查是否还有重试机会
  1768. if (task.attempts < task.maxAttempts) {
  1769. console.log(`将在5秒后重试上传,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1770. // 5秒后重试
  1771. setTimeout(() => {
  1772. // 重置进度
  1773. this.uploadProgress[task.id] = 0;
  1774. // 重新开始上传
  1775. if (typeof task.file !== 'string') {
  1776. this.uploadFileWithXHR(task);
  1777. } else {
  1778. this.uploadFileWithUni(task);
  1779. }
  1780. }, 5000);
  1781. } else {
  1782. // 超过最大重试次数,移除任务并继续处理队列
  1783. console.log('超过最大重试次数,放弃上传');
  1784. // 显示错误提示
  1785. uni.showToast({
  1786. title: '视频上传失败,请稍后重试',
  1787. icon: 'none',
  1788. duration: 2000
  1789. });
  1790. // 从队列中移除当前任务
  1791. this.uploadQueue.shift();
  1792. // 继续处理队列中的下一个任务
  1793. this.processUploadQueue();
  1794. }
  1795. },
  1796. // 修改 submitVideoToInterview 方法
  1797. submitVideoToInterview(videoUrl, task = null) {
  1798. console.log('提交视频URL到面试接口:', videoUrl);
  1799. // 确定问题ID
  1800. let questionId;
  1801. if (task) {
  1802. questionId = task.questionId;
  1803. } else {
  1804. // 根据当前视频索引映射到正确的问题ID(兼容旧代码)
  1805. switch(this.currentVideoIndex) {
  1806. case 1:
  1807. questionId = 36;
  1808. break;
  1809. case 2:
  1810. questionId = 11;
  1811. break;
  1812. case 3:
  1813. questionId = 12;
  1814. break;
  1815. case 4:
  1816. questionId = 13;
  1817. break;
  1818. default:
  1819. questionId = 10;
  1820. }
  1821. }
  1822. // 准备请求参数
  1823. const requestData = {
  1824. application_id: uni.getStorageSync('appId'),
  1825. question_id: questionId,
  1826. video_url: videoUrl,
  1827. tenant_id: uni.getStorageSync('tenant_id') || '1',
  1828. has_audio: true // 明确标记包含音频
  1829. };
  1830. // 首先提交到面试接口
  1831. /* uni.request({
  1832. url: `${apiBaseUrl}/api/job/upload_video`,
  1833. method: 'POST',
  1834. data: requestData,
  1835. header: {
  1836. 'content-type': 'application/x-www-form-urlencoded'
  1837. },
  1838. success: (res) => {
  1839. console.log('面试接口提交成功:', res);
  1840. if (res.data.code === 0 || res.data.code === 2000) {
  1841. // 提交成功
  1842. if (task) {
  1843. // 从队列中移除当前任务
  1844. this.uploadQueue.shift();
  1845. // 继续处理队列中的下一个任务
  1846. this.processUploadQueue();
  1847. } else {
  1848. // 兼容旧代码的处理逻辑
  1849. uni.showToast({
  1850. title: '回答已提交',
  1851. icon: 'success'
  1852. });
  1853. // 保存最后上传的视频URL
  1854. this.lastUploadedVideoUrl = videoUrl;
  1855. // 隐藏重试按钮(如果之前显示了)
  1856. this.showRetryButton = false;
  1857. this.lastVideoToRetry = null;
  1858. }
  1859. // 提交到AI语音交互接口
  1860. } else {
  1861. // 提交失败
  1862. if (task) {
  1863. this.handleSubmitFailure(task, res.data.msg || '提交失败');
  1864. } else {
  1865. // 兼容旧代码的处理逻辑
  1866. uni.showToast({
  1867. title: res.data.msg || '提交失败,请重试',
  1868. icon: 'none'
  1869. });
  1870. // 保存失败的视频URL,用于重试
  1871. this.lastVideoToRetry = videoUrl;
  1872. // 显示重试按钮
  1873. this.showRetryButton = true;
  1874. }
  1875. }
  1876. },
  1877. fail: (err) => {
  1878. console.error('面试接口提交失败:', err);
  1879. if (task) {
  1880. this.handleSubmitFailure(task, err.errMsg || '网络错误');
  1881. } else {
  1882. // 兼容旧代码的处理逻辑
  1883. uni.hideLoading();
  1884. uni.showToast({
  1885. title: '网络错误,请重试',
  1886. icon: 'none'
  1887. });
  1888. // 保存失败的视频URL,用于重试
  1889. this.lastVideoToRetry = videoUrl;
  1890. // 显示重试按钮
  1891. this.showRetryButton = true;
  1892. }
  1893. }
  1894. }); */
  1895. this.submitToAiVoiceInteraction(videoUrl);
  1896. },
  1897. // 添加新方法:处理提交失败
  1898. handleSubmitFailure(task, errorMsg) {
  1899. console.error('提交失败:', errorMsg);
  1900. // 更新任务状态
  1901. this.uploadStatus[task.id] = 'failed';
  1902. this.updateUploadStatusText();
  1903. // 检查是否还有重试机会
  1904. if (task.attempts < task.maxAttempts) {
  1905. console.log(`将在5秒后重试提交,当前尝试次数: ${task.attempts}/${task.maxAttempts}`);
  1906. // 5秒后重试
  1907. setTimeout(() => {
  1908. // 重新提交
  1909. this.submitVideoToInterview(task.videoUrl, task);
  1910. }, 5000);
  1911. } else {
  1912. // 超过最大重试次数,移除任务并继续处理队列
  1913. console.log('超过最大重试次数,放弃提交');
  1914. // 显示错误提示
  1915. uni.showToast({
  1916. title: '视频提交失败,请稍后重试',
  1917. icon: 'none',
  1918. duration: 2000
  1919. });
  1920. // 从队列中移除当前任务
  1921. this.uploadQueue.shift();
  1922. // 继续处理队列中的下一个任务
  1923. this.processUploadQueue();
  1924. }
  1925. },
  1926. // 添加新方法:更新上传状态文本
  1927. updateUploadStatusText() {
  1928. if (this.uploadQueue.length === 0) {
  1929. this.uploadStatusText = '';
  1930. return;
  1931. }
  1932. const currentTask = this.uploadQueue[0];
  1933. const progress = this.uploadProgress[currentTask.id] || 0;
  1934. const status = this.uploadStatus[currentTask.id] || 'pending';
  1935. let statusText = '';
  1936. switch (status) {
  1937. case 'pending':
  1938. statusText = '等待上传';
  1939. break;
  1940. case 'uploading':
  1941. statusText = `上传中 ${progress}%`;
  1942. break;
  1943. case 'success':
  1944. statusText = '上传成功,提交中...';
  1945. break;
  1946. case 'failed':
  1947. statusText = `上传失败,${currentTask.attempts < currentTask.maxAttempts ? '即将重试' : '已放弃'}`;
  1948. break;
  1949. }
  1950. this.uploadStatusText = `问题${currentTask.questionId - 9}:${statusText}`;
  1951. // 如果队列中有多个任务,显示总数
  1952. if (this.uploadQueue.length > 1) {
  1953. this.uploadStatusText += ` (${this.uploadQueue.length}个视频待处理)`;
  1954. }
  1955. },
  1956. // 修改 proceedToNextQuestion 方法
  1957. proceedToNextQuestion() {
  1958. console.log('准备跳转到下一页面');
  1959. // 如果正在播放AI语音,不立即跳转
  1960. if (this.isPlayingAiVoice) {
  1961. console.log('AI语音正在播放,等待播放完成后再跳转');
  1962. return;
  1963. }
  1964. // 不再等待上传队列完成,直接跳转
  1965. // this.navigateToNextPage();
  1966. // 如果有未完成的上传任务,在后台继续处理
  1967. if (this.uploadQueue.length > 0) {
  1968. console.log('上传队列将在后台继续处理...');
  1969. // 保存上传状态到本地存储,以便在其他页面可以查看
  1970. this.saveUploadStatus();
  1971. }
  1972. },
  1973. // 修改 navigateToNextPage 方法
  1974. navigateToNextPage() {
  1975. console.log('准备导航到下一个页面');
  1976. // 如果正在播放AI语音,不立即跳转
  1977. if (this.isPlayingAiVoice) {
  1978. console.log('AI语音正在播放,等待播放完成后再跳转');
  1979. return;
  1980. }
  1981. console.log('导航到下一个页面');
  1982. // 检查 selectedJob 的 id
  1983. try {
  1984. const currentJobDetail = uni.getStorageSync('selectedJob');
  1985. const jobId = currentJobDetail ? JSON.parse(currentJobDetail).id : null;
  1986. // 如果 id 为 9 或 10,直接跳转到 success 页面
  1987. if (jobId === 9 || jobId === 10) {
  1988. uni.navigateTo({
  1989. url: '/pages/success/success',
  1990. success: () => {
  1991. console.log('成功跳转到成功页面');
  1992. },
  1993. fail: (err) => {
  1994. console.error('跳转失败:', err);
  1995. // 如果跳转失败,尝试其他页面
  1996. this.handleNavigationFailure();
  1997. }
  1998. });
  1999. return; // 提前返回,不执行后续代码
  2000. }
  2001. } catch (e) {
  2002. console.error('解析 selectedJob 失败:', e);
  2003. // 解析失败时继续默认跳转逻辑
  2004. }
  2005. // 原有的跳转逻辑
  2006. uni.navigateTo({
  2007. url: '/pages/posture-guide/posture-guide?uploading=' + (this.uploadQueue.length > 0 ? 'true' : 'false'),
  2008. success: () => {
  2009. console.log('成功跳转到结果页面');
  2010. },
  2011. fail: (err) => {
  2012. console.error('跳转失败:', err);
  2013. this.handleNavigationFailure();
  2014. }
  2015. });
  2016. },
  2017. // 添加处理导航失败的方法
  2018. handleNavigationFailure() {
  2019. // 尝试跳转到其他页面
  2020. uni.navigateTo({
  2021. url: '/pages/posture-guide/posture-guide',
  2022. fail: (err2) => {
  2023. console.error('备用跳转也失败:', err2);
  2024. // 最后尝试返回上一页
  2025. uni.navigateBack({
  2026. delta: 1
  2027. });
  2028. }
  2029. });
  2030. },
  2031. // 修改 handleAnswerButtonClick 方法
  2032. handleAnswerButtonClick() {
  2033. // 隐藏答题按钮
  2034. this.showAnswerButton = false;
  2035. // 显示简短提示
  2036. uni.showToast({
  2037. title: '面试完成',
  2038. icon: 'success',
  2039. duration: 500 // 只显示0.5秒
  2040. });
  2041. // 如果有最后上传的视频URL,提交到AI语音交互接口
  2042. if (this.lastUploadedVideoUrl) {
  2043. this.submitToAiVoiceInteraction(this.lastUploadedVideoUrl);
  2044. } else {
  2045. // 没有视频URL,直接跳转
  2046. this.navigateToNextPage();
  2047. }
  2048. },
  2049. // 处理相机错误
  2050. handleCameraError(e) {
  2051. console.error('相机错误:', e);
  2052. // 获取平台信息
  2053. const systemInfo = uni.getSystemInfoSync();
  2054. const isIOS = systemInfo.platform === 'ios';
  2055. if (isIOS) {
  2056. console.log('iOS相机错误,尝试重新初始化');
  2057. // 显示提示
  2058. uni.showToast({
  2059. title: '相机初始化中...',
  2060. icon: 'loading',
  2061. duration: 2000
  2062. });
  2063. // 重置相机
  2064. this.resetCamera();
  2065. // 如果正在录制,停止录制
  2066. if (this.isRecording) {
  2067. this.isRecording = false;
  2068. this.showStopRecordingButton = false;
  2069. // 提供替代选项
  2070. setTimeout(() => {
  2071. this.useAlternativeRecordingMethod();
  2072. }, 1000);
  2073. }
  2074. } else {
  2075. // 显示错误提示
  2076. uni.showToast({
  2077. title: '相机初始化失败,请检查权限设置',
  2078. icon: 'none'
  2079. });
  2080. // 尝试备用选项
  2081. this.tryFallbackOptions();
  2082. }
  2083. },
  2084. // 添加新方法:尝试备用选项
  2085. tryFallbackOptions() {
  2086. // 检查环境
  2087. const systemInfo = uni.getSystemInfoSync();
  2088. // 在小程序环境中使用小程序API
  2089. if (systemInfo.uniPlatform === 'mp-weixin' || systemInfo.uniPlatform === 'mp-alipay') {
  2090. this.useMiniProgramCamera();
  2091. }
  2092. // 在H5环境中显示静态图像
  2093. else {
  2094. this.showStaticCameraPlaceholder();
  2095. }
  2096. },
  2097. // 添加新方法:使用小程序相机API
  2098. useMiniProgramCamera() {
  2099. console.log('尝试使用小程序相机组件');
  2100. // 这里需要在模板中添加小程序相机组件
  2101. // 并设置一个标志来控制显示
  2102. this.useMiniProgramCameraComponent = true;
  2103. },
  2104. // 添加新方法:显示静态图像
  2105. showStaticCameraPlaceholder() {
  2106. console.log('显示静态摄像头占位图');
  2107. // 创建一个图像元素
  2108. const img = document.createElement('img');
  2109. img.src = '/static/images/camera-placeholder.png'; // 确保有这个图片资源
  2110. img.className = 'static-camera-image';
  2111. img.style.width = '100%';
  2112. img.style.height = '100%';
  2113. img.style.objectFit = 'cover';
  2114. // 获取容器并添加图像
  2115. const container = this.$refs.userCameraVideo.parentNode;
  2116. container.appendChild(img);
  2117. },
  2118. // 处理视频时间更新事件
  2119. handleTimeUpdate(e) {
  2120. // 获取当前视频播放时间
  2121. const currentTime = e.target.currentTime;
  2122. // 如果正在录制,更新录制时间显示
  2123. if (this.isRecording && this.recordingTimerCount) {
  2124. this.recordingTimeDisplay = this.formatTime(this.recordingTimerCount);
  2125. }
  2126. // 根据当前播放的视频索引选择对应的字幕数组
  2127. let currentSubtitles;
  2128. if (this.currentVideoIndex === 0) {
  2129. currentSubtitles = this.subtitles;
  2130. } else if (this.currentVideoIndex === 1) {
  2131. currentSubtitles = this.secondVideoSubtitles;
  2132. } else if (this.currentVideoIndex === 2) {
  2133. currentSubtitles = this.thirdVideoSubtitles;
  2134. } else if (this.currentVideoIndex === 3) {
  2135. currentSubtitles = this.fourthVideoSubtitles;
  2136. } else if (this.currentVideoIndex === 4) {
  2137. currentSubtitles = this.fifthVideoSubtitles;
  2138. } else if (this.currentVideoIndex === 5) {
  2139. currentSubtitles = this.sixthVideoSubtitles;
  2140. } else {
  2141. // 如果有更多视频,可以继续添加条件
  2142. currentSubtitles = [];
  2143. }
  2144. // 查找当前时间应该显示的字幕
  2145. const subtitle = currentSubtitles.find(
  2146. sub => currentTime >= sub.startTime && currentTime < sub.endTime
  2147. );
  2148. // 更新当前字幕
  2149. this.currentSubtitle = subtitle ? subtitle.text : '';
  2150. },
  2151. // Add a new method to handle the "Start Recording" button click
  2152. handleStartRecordingClick() {
  2153. // 隐藏"开始回答"按钮
  2154. this.showStartRecordingButton = false;
  2155. // 如果倒计时正在进行,清除倒计时
  2156. this.clearCountdown();
  2157. // 开始录制
  2158. this.startRecordingAnswer();
  2159. },
  2160. // 修改 checkAudioPermission 方法,确保在录制前获取音频权限
  2161. checkAudioPermission() {
  2162. // 在小程序环境中
  2163. const systemInfo = uni.getSystemInfoSync();
  2164. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  2165. if (isMiniProgram) {
  2166. // 小程序环境下的权限请求
  2167. uni.getSetting({
  2168. success: (res) => {
  2169. if (!res.authSetting['scope.record']) {
  2170. uni.authorize({
  2171. scope: 'scope.record',
  2172. success: () => {
  2173. console.log('录音权限已获取');
  2174. },
  2175. fail: (err) => {
  2176. console.error('录音权限获取失败:', err);
  2177. this.showPermissionDialog('录音');
  2178. }
  2179. });
  2180. }
  2181. if (!res.authSetting['scope.camera']) {
  2182. uni.authorize({
  2183. scope: 'scope.camera',
  2184. success: () => {
  2185. console.log('相机权限已获取');
  2186. },
  2187. fail: (err) => {
  2188. console.error('相机权限获取失败:', err);
  2189. this.showPermissionDialog('相机');
  2190. }
  2191. });
  2192. }
  2193. }
  2194. });
  2195. } else {
  2196. // 浏览器环境下的权限请求
  2197. // ... 保持原有代码不变
  2198. }
  2199. },
  2200. // 添加新方法:测试音频输入
  2201. testAudioInput() {
  2202. if (!this.cameraStream) {
  2203. console.warn('没有可用的媒体流,无法测试音频');
  2204. return;
  2205. }
  2206. const audioTracks = this.cameraStream.getAudioTracks();
  2207. if (audioTracks.length === 0) {
  2208. console.warn('没有检测到音频轨道,尝试重新获取');
  2209. this.tryGetAudioOnly();
  2210. return;
  2211. }
  2212. console.log('音频轨道信息:', audioTracks[0].getSettings());
  2213. // 创建音频上下文和分析器
  2214. try {
  2215. const AudioContext = window.AudioContext || window.webkitAudioContext;
  2216. if (!AudioContext) {
  2217. console.warn('浏览器不支持AudioContext');
  2218. return;
  2219. }
  2220. const audioContext = new AudioContext();
  2221. const analyser = audioContext.createAnalyser();
  2222. const microphone = audioContext.createMediaStreamSource(this.cameraStream);
  2223. microphone.connect(analyser);
  2224. // 设置分析器
  2225. analyser.fftSize = 256;
  2226. const bufferLength = analyser.frequencyBinCount;
  2227. const dataArray = new Uint8Array(bufferLength);
  2228. // 检测音频输入
  2229. let silenceCounter = 0;
  2230. const checkAudio = () => {
  2231. if (this.isRecording) return; // 如果正在录制,停止检测
  2232. analyser.getByteFrequencyData(dataArray);
  2233. // 计算平均音量
  2234. let sum = 0;
  2235. for (let i = 0; i < bufferLength; i++) {
  2236. sum += dataArray[i];
  2237. }
  2238. const average = sum / bufferLength;
  2239. if (average > 10) {
  2240. console.log('检测到音频输入,音量:', average);
  2241. silenceCounter = 0;
  2242. } else {
  2243. silenceCounter++;
  2244. if (silenceCounter > 10) {
  2245. console.warn('持续检测不到音频输入,可能麦克风未正常工作');
  2246. silenceCounter = 0;
  2247. }
  2248. }
  2249. // 继续检测
  2250. requestAnimationFrame(checkAudio);
  2251. };
  2252. // 开始检测
  2253. checkAudio();
  2254. } catch (e) {
  2255. console.error('音频测试失败:', e);
  2256. }
  2257. },
  2258. // 添加新方法:尝试单独获取音频
  2259. tryGetAudioOnly() {
  2260. navigator.mediaDevices.getUserMedia({ audio: true })
  2261. .then(audioStream => {
  2262. // 如果已有视频流,合并音频和视频轨道
  2263. if (this.cameraStream) {
  2264. const videoTrack = this.cameraStream.getVideoTracks()[0];
  2265. const audioTrack = audioStream.getAudioTracks()[0];
  2266. // 创建新的媒体流,包含视频和音频轨道
  2267. const combinedStream = new MediaStream();
  2268. if (videoTrack) combinedStream.addTrack(videoTrack);
  2269. if (audioTrack) combinedStream.addTrack(audioTrack);
  2270. // 更新摄像头流
  2271. this.cameraStream = combinedStream;
  2272. // 更新视频元素的源
  2273. const videoElement = this.$refs.userCameraVideo;
  2274. if (videoElement) {
  2275. videoElement.srcObject = combinedStream;
  2276. videoElement.muted = true; // 避免回声,但仍然录制声音
  2277. }
  2278. console.log('成功合并音频和视频轨道');
  2279. } else {
  2280. console.warn('没有视频流可合并');
  2281. }
  2282. })
  2283. .catch(err => {
  2284. console.error('单独获取音频失败:', err);
  2285. });
  2286. },
  2287. // 添加新方法:显示权限对话框
  2288. showPermissionDialog(permissionType) {
  2289. uni.showModal({
  2290. title: '需要权限',
  2291. content: `请允许使用${permissionType}权限,否则可能影响面试功能`,
  2292. confirmText: '去设置',
  2293. success: (res) => {
  2294. if (res.confirm) {
  2295. uni.openSetting({
  2296. success: (settingRes) => {
  2297. console.log('设置页面打开成功', settingRes);
  2298. }
  2299. });
  2300. }
  2301. }
  2302. });
  2303. },
  2304. // 添加重试上传方法
  2305. retryVideoUpload() {
  2306. if (this.lastVideoToRetry) {
  2307. // 隐藏重试按钮
  2308. this.showRetryButton = false;
  2309. // 显示加载提示
  2310. uni.showLoading({
  2311. title: '正在重新提交...',
  2312. mask: true
  2313. });
  2314. // 重新提交视频
  2315. this.submitVideoToInterview(this.lastVideoToRetry);
  2316. } else {
  2317. uni.showToast({
  2318. title: '没有可重试的视频',
  2319. icon: 'none'
  2320. });
  2321. }
  2322. },
  2323. // 添加一个新方法用于压缩视频
  2324. async compressVideo(videoBlob) {
  2325. // 如果视频大小小于某个阈值,直接返回原视频
  2326. if (videoBlob.size < 5 * 1024 * 1024) { // 小于5MB
  2327. return videoBlob;
  2328. }
  2329. console.log('开始压缩视频,原始大小:', videoBlob.size);
  2330. // 创建一个视频元素来加载视频
  2331. const videoElement = document.createElement('video');
  2332. videoElement.muted = true;
  2333. videoElement.autoplay = false;
  2334. // 创建一个canvas元素用于绘制视频帧
  2335. const canvas = document.createElement('canvas');
  2336. const ctx = canvas.getContext('2d');
  2337. // 设置视频源
  2338. videoElement.src = URL.createObjectURL(videoBlob);
  2339. return new Promise((resolve) => {
  2340. videoElement.onloadedmetadata = () => {
  2341. // 设置canvas尺寸为视频的一半
  2342. const width = Math.floor(videoElement.videoWidth / 2);
  2343. const height = Math.floor(videoElement.videoHeight / 2);
  2344. canvas.width = width;
  2345. canvas.height = height;
  2346. // 创建MediaRecorder来录制canvas
  2347. const stream = canvas.captureStream(15); // 15fps
  2348. // 如果原视频有音轨,添加到新流中
  2349. if (videoElement.captureStream) {
  2350. const originalStream = videoElement.captureStream();
  2351. const audioTracks = originalStream.getAudioTracks();
  2352. if (audioTracks.length > 0) {
  2353. stream.addTrack(audioTracks[0]);
  2354. }
  2355. }
  2356. // 设置较低的比特率
  2357. const options = {
  2358. mimeType: 'video/webm;codecs=vp8,opus',
  2359. audioBitsPerSecond: 64000,
  2360. videoBitsPerSecond: 800000 // 800kbps
  2361. };
  2362. const mediaRecorder = new MediaRecorder(stream, options);
  2363. const chunks = [];
  2364. mediaRecorder.ondataavailable = (e) => {
  2365. if (e.data.size > 0) {
  2366. chunks.push(e.data);
  2367. }
  2368. };
  2369. mediaRecorder.onstop = () => {
  2370. const compressedBlob = new Blob(chunks, { type: 'video/webm' });
  2371. console.log('视频压缩完成,压缩后大小:', compressedBlob.size);
  2372. resolve(compressedBlob);
  2373. };
  2374. // 开始播放视频并录制
  2375. videoElement.onplay = () => {
  2376. mediaRecorder.start(100);
  2377. // 绘制视频帧到canvas
  2378. const drawFrame = () => {
  2379. if (videoElement.paused || videoElement.ended) {
  2380. mediaRecorder.stop();
  2381. return;
  2382. }
  2383. ctx.drawImage(videoElement, 0, 0, width, height);
  2384. requestAnimationFrame(drawFrame);
  2385. };
  2386. drawFrame();
  2387. };
  2388. videoElement.play();
  2389. };
  2390. });
  2391. },
  2392. // 修改 checkIOSCameraRecordPermission 方法
  2393. checkIOSCameraRecordPermission() {
  2394. const systemInfo = uni.getSystemInfoSync();
  2395. if (systemInfo.platform !== 'ios') return;
  2396. // 在iOS上,需要同时请求相机和录音权限
  2397. uni.getSetting({
  2398. success: (res) => {
  2399. // 检查相机权限
  2400. if (!res.authSetting['scope.camera']) {
  2401. uni.authorize({
  2402. scope: 'scope.camera',
  2403. success: () => {
  2404. console.log('iOS相机权限已获取');
  2405. },
  2406. fail: (err) => {
  2407. console.error('iOS相机权限获取失败:', err);
  2408. this.showPermissionDialog('相机');
  2409. }
  2410. });
  2411. }
  2412. // 检查录音权限
  2413. if (!res.authSetting['scope.record']) {
  2414. uni.authorize({
  2415. scope: 'scope.record',
  2416. success: () => {
  2417. console.log('iOS录音权限已获取');
  2418. },
  2419. fail: (err) => {
  2420. console.error('iOS录音权限获取失败:', err);
  2421. this.showPermissionDialog('录音');
  2422. }
  2423. });
  2424. }
  2425. }
  2426. });
  2427. },
  2428. // 添加新方法:检查并修复渲染问题
  2429. checkAndFixRenderingIssues() {
  2430. // 检查全局对象,防止渲染错误
  2431. try {
  2432. // 检查是否有全局变量u
  2433. if (typeof u !== 'undefined' && u) {
  2434. // 检查currentQuestion属性
  2435. if (!u.currentQuestion) {
  2436. console.log('修复: 创建缺失的currentQuestion对象');
  2437. u.currentQuestion = {};
  2438. }
  2439. // 确保isImportant属性存在
  2440. if (u.currentQuestion && typeof u.currentQuestion.isImportant === 'undefined') {
  2441. console.log('修复: 设置缺失的isImportant属性');
  2442. u.currentQuestion.isImportant = false;
  2443. }
  2444. }
  2445. } catch (e) {
  2446. console.log('防御性检查异常:', e);
  2447. }
  2448. },
  2449. // 添加格式化时间的辅助方法
  2450. formatTime(seconds) {
  2451. if (!seconds && seconds !== 0) return '03:30'; // 默认显示03:30
  2452. const minutes = Math.floor(seconds / 60);
  2453. const remainingSeconds = seconds % 60;
  2454. return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  2455. },
  2456. // 添加新方法:保存上传状态到本地存储
  2457. saveUploadStatus() {
  2458. const uploadStatus = {
  2459. isUploading: this.isUploading,
  2460. uploadQueue: this.uploadQueue.length,
  2461. timestamp: Date.now()
  2462. };
  2463. try {
  2464. uni.setStorageSync('videoUploadStatus', JSON.stringify(uploadStatus));
  2465. } catch (e) {
  2466. console.error('保存上传状态失败:', e);
  2467. }
  2468. },
  2469. // 修改 submitToAiVoiceInteraction 方法
  2470. submitToAiVoiceInteraction(videoUrl) {
  2471. console.log('提交视频到AI语音交互接口:', videoUrl);
  2472. // 显示加载提示
  2473. uni.showLoading({
  2474. title: '面试官正在思考...',
  2475. mask: true
  2476. });
  2477. // 获取用户信息和租户ID
  2478. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  2479. const application_id = uni.getStorageSync('appId') || '98'; // 默认为98(报告)
  2480. // 准备请求数据
  2481. const requestData = {
  2482. voice_url: videoUrl,
  2483. tenant_id: tenant_id,
  2484. application_id: application_id,
  2485. scene_type: 'interview', // 场景类型:面试
  2486. voice_type: 'longxiaoxia', // 角色:龙小侠
  2487. conversation_history: this.conversationHistory // 添加对话历史
  2488. };
  2489. // 检查环境
  2490. const systemInfo = uni.getSystemInfoSync();
  2491. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  2492. if (isMiniProgram) {
  2493. // 小程序环境使用 uni.request
  2494. uni.request({
  2495. url: `${apiBaseUrl}/api/system/voice/mcp/interaction`, // 使用新的接口地址
  2496. method: 'POST',
  2497. data: requestData,
  2498. header: {
  2499. 'content-type': 'application/json' // 修改为JSON格式
  2500. },
  2501. success: (res) => {
  2502. if (res.statusCode === 200) {
  2503. this.handleAiInteractionResponse(res.data);
  2504. } else {
  2505. this.handleAiInteractionError(res.data);
  2506. }
  2507. },
  2508. fail: (err) => {
  2509. this.handleAiInteractionError(err);
  2510. }
  2511. });
  2512. } else {
  2513. // H5/App环境使用 XMLHttpRequest
  2514. const xhr = new XMLHttpRequest();
  2515. xhr.open('POST', `${apiBaseUrl}/api/system/voice/mcp/interaction`, true);
  2516. xhr.timeout = 60000; // 60秒超时
  2517. xhr.setRequestHeader('Content-Type', 'application/json');
  2518. xhr.onload = () => {
  2519. if (xhr.status === 200) {
  2520. try {
  2521. const response = JSON.parse(xhr.responseText);
  2522. this.handleAiInteractionResponse(response);
  2523. } catch (e) {
  2524. console.error('解析AI响应失败:', e);
  2525. this.handleAiInteractionError({ errMsg: '解析响应失败' });
  2526. }
  2527. } else {
  2528. this.handleAiInteractionError({ errMsg: 'HTTP状态: ' + xhr.status });
  2529. }
  2530. };
  2531. xhr.onerror = () => {
  2532. this.handleAiInteractionError({ errMsg: '网络错误' });
  2533. };
  2534. xhr.ontimeout = () => {
  2535. this.handleAiInteractionError({ errMsg: '请求超时' });
  2536. };
  2537. // 发送JSON格式的数据
  2538. xhr.send(JSON.stringify(requestData));
  2539. }
  2540. },
  2541. // 添加处理AI交互响应的方法
  2542. handleAiInteractionResponse(data) {
  2543. console.log('AI语音交互接口响应:', data);
  2544. // 获取系统信息
  2545. const systemInfo = uni.getSystemInfoSync();
  2546. const isIOS = systemInfo.platform === 'ios';
  2547. // 标记正在播放AI语音
  2548. this.isPlayingAiVoice = true;
  2549. // 隐藏加载提示
  2550. uni.hideLoading();
  2551. // 重置备选方法尝试标志
  2552. this.hasTriedFallbackMethod = false;
  2553. if (data && data.success) {
  2554. // 保存AI回复数据
  2555. this.aiText = data.ai_text || '';
  2556. this.transcribedText = data.transcribed_text ? data.transcribed_text.text : '';
  2557. // 记录对话历史
  2558. this.conversationHistory.push({
  2559. role: 'user',
  2560. content: this.transcribedText
  2561. });
  2562. this.conversationHistory.push({
  2563. role: 'assistant',
  2564. content: this.aiText
  2565. });
  2566. // 获取AI语音URL
  2567. const aiVoiceUrl = data.ai_voice_url || '';
  2568. if (aiVoiceUrl) {
  2569. // 构建完整的URL
  2570. this.aiVoiceUrl = aiVoiceUrl.startsWith('http')
  2571. ? aiVoiceUrl
  2572. : `${apiBaseUrl}${aiVoiceUrl}`;
  2573. console.log('AI语音URL:', this.aiVoiceUrl);
  2574. // 显示AI回复文本作为字幕
  2575. this.currentSubtitle = this.aiText;
  2576. // 检查平台
  2577. const systemInfo = uni.getSystemInfoSync();
  2578. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  2579. // 检查音频URL是否包含不支持的格式
  2580. const isUnsupportedFormat = this.checkUnsupportedAudioFormat(this.aiVoiceUrl);
  2581. if (isUnsupportedFormat && isMiniProgram) {
  2582. console.warn('检测到小程序不支持的音频格式,直接使用文本显示');
  2583. this.displayTextResponse();
  2584. return;
  2585. }
  2586. // 根据平台选择不同的播放方法
  2587. if (isMiniProgram) {
  2588. // 小程序环境使用原生API
  2589. if (isIOS) {
  2590. this.playIOSMiniProgramAudio();
  2591. } else {
  2592. this.playMiniProgramAudio();
  2593. }
  2594. /* this.playMiniProgramAudio(); */
  2595. } else {
  2596. // H5/App环境使用通用方法
  2597. this.playAiVoice();
  2598. }
  2599. // 重要:确保在AI语音播放期间不显示继续提问按钮
  2600. this.showContinueQuestionButton = false;
  2601. this.showEndInterviewButton = false;
  2602. } else {
  2603. console.warn('未获取到AI语音URL,使用文本显示');
  2604. // 如果没有语音URL,显示文本回复
  2605. this.displayTextResponse();
  2606. }
  2607. } else {
  2608. console.error('AI语音交互失败:', data);
  2609. // AI交互失败,显示继续提问按钮
  2610. this.showContinueQuestionOptions();
  2611. // 检查是否需要自动跳转
  2612. this.checkPendingNavigation();
  2613. }
  2614. },
  2615. // 修改 handleAiInteractionError 方法,不要直接跳转,而是显示继续提问选项
  2616. handleAiInteractionError(err) {
  2617. console.error('AI语音交互请求失败:', err);
  2618. uni.hideLoading();
  2619. // 显示错误提示
  2620. uni.showToast({
  2621. title: 'AI处理失败,请重试',
  2622. icon: 'none',
  2623. duration: 2000
  2624. });
  2625. // 请求失败,显示继续提问选项,而不是直接跳转
  2626. this.showContinueQuestionOptions();
  2627. },
  2628. // 添加新方法:播放AI语音
  2629. playAiVoice() {
  2630. console.log('开始播放AI语音:', this.aiVoiceUrl);
  2631. // 重置错误提示标记
  2632. this.hasShownTextFallbackToast = false;
  2633. // 标记正在播放AI语音
  2634. this.isPlayingAiVoice = true;
  2635. // 隐藏所有按钮
  2636. this.showContinueQuestionButton = false;
  2637. this.showEndInterviewButton = false;
  2638. // 显示AI回复文本作为字幕
  2639. this.currentSubtitle = this.aiText;
  2640. // 检查音频URL是否有效
  2641. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === '') {
  2642. console.error('无效的音频URL,直接显示文本');
  2643. this.useTextFallbackAndNavigate();
  2644. return;
  2645. }
  2646. // 获取系统信息
  2647. const systemInfo = uni.getSystemInfoSync();
  2648. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  2649. const isIOS = systemInfo.platform === 'ios';
  2650. // 根据平台选择播放方式
  2651. if (isMiniProgram) {
  2652. if (isIOS) {
  2653. this.playIOSMiniProgramAudio();
  2654. } else {
  2655. this.playMiniProgramAudio();
  2656. }
  2657. } else if (isIOS) {
  2658. this.playAiVoiceForIOS();
  2659. } else {
  2660. this.playAiVoiceForAndroid();
  2661. }
  2662. },
  2663. // 新增方法:文本回退并导航
  2664. useTextFallbackAndNavigate() {
  2665. console.log('使用文本回退并准备导航');
  2666. // 标记播放结束
  2667. this.isPlayingAiVoice = false;
  2668. // 显示友好提示
  2669. if (!this.hasShownTextFallbackToast) {
  2670. this.hasShownTextFallbackToast = true;
  2671. uni.showToast({
  2672. title: '为您显示文字回复',
  2673. icon: 'none',
  2674. duration: 2000
  2675. });
  2676. }
  2677. // 显示完整文本
  2678. this.currentSubtitle = this.aiText || '抱歉,AI回复内容加载失败';
  2679. // 给用户一定时间阅读文本后自动跳转
  2680. setTimeout(() => {
  2681. this.navigateToNextPage();
  2682. }, 5000); // 5秒后跳转
  2683. },
  2684. // 修改 handleIOSAudioPlaybackComplete 方法
  2685. handleIOSAudioPlaybackComplete() {
  2686. // 标记播放结束
  2687. this.isPlayingAiVoice = false;
  2688. // 延迟清空字幕
  2689. setTimeout(() => {
  2690. this.currentSubtitle = '';
  2691. }, 1000);
  2692. // 销毁音频播放器
  2693. if (this.aiAudioPlayer) {
  2694. try {
  2695. this.aiAudioPlayer.destroy();
  2696. } catch (e) {
  2697. console.error('销毁iOS音频播放器失败:', e);
  2698. }
  2699. this.aiAudioPlayer = null;
  2700. }
  2701. // 直接导航到下一页
  2702. this.navigateToNextPage();
  2703. },
  2704. // 修改 playMiniProgramAudio 方法
  2705. playMiniProgramAudio() {
  2706. console.log('使用小程序原生音频API播放:', this.aiVoiceUrl);
  2707. try {
  2708. const audioContext = wx.createInnerAudioContext();
  2709. audioContext.autoplay = true;
  2710. audioContext.src = this.aiVoiceUrl;
  2711. audioContext.onPlay(() => {
  2712. console.log('AI语音开始播放');
  2713. this.currentSubtitle = this.aiText;
  2714. });
  2715. audioContext.onEnded(() => {
  2716. console.log('AI语音播放结束');
  2717. this.isPlayingAiVoice = false;
  2718. this.currentSubtitle = '';
  2719. audioContext.destroy();
  2720. // 播放结束后直接导航
  2721. this.navigateToNextPage();
  2722. });
  2723. audioContext.onError((err) => {
  2724. console.error('AI语音播放错误:', err);
  2725. audioContext.destroy();
  2726. this.useTextFallbackAndNavigate();
  2727. });
  2728. // 设置超时处理
  2729. setTimeout(() => {
  2730. if (this.isPlayingAiVoice) {
  2731. console.warn('音频播放超时,使用文本回退');
  2732. this.useTextFallbackAndNavigate();
  2733. }
  2734. }, 5000);
  2735. } catch (e) {
  2736. console.error('小程序播放AI语音失败:', e);
  2737. this.useTextFallbackAndNavigate();
  2738. }
  2739. },
  2740. // 新增方法:导航到下一页
  2741. navigateToNextPage() {
  2742. // 这里添加导航逻辑
  2743. console.log('准备导航到下一页');
  2744. // 根据实际需求设置导航目标
  2745. uni.navigateTo({
  2746. url: '/pages/posture-guide/posture-guide',
  2747. fail: (err) => {
  2748. console.error('导航失败:', err);
  2749. // 导航失败时的处理
  2750. uni.showToast({
  2751. title: '页面跳转失败',
  2752. icon: 'none',
  2753. duration: 2000
  2754. });
  2755. }
  2756. });
  2757. },
  2758. // 新增方法:专门处理iOS小程序音频播放
  2759. playIOSMiniProgramAudio() {
  2760. console.log('iOS小程序环境:使用增强的音频处理方法');
  2761. // 重置错误标记
  2762. this.hasShownTextFallbackToast = false;
  2763. try {
  2764. // 创建音频上下文,使用wx API确保兼容性
  2765. const audioContext = wx.createInnerAudioContext();
  2766. // iOS小程序优化配置
  2767. audioContext.autoplay = false;
  2768. audioContext.obeyMuteSwitch = true; // iOS遵循静音开关
  2769. audioContext.volume = 1.0;
  2770. // 设置播放成功事件
  2771. audioContext.onPlay(() => {
  2772. console.log('iOS小程序音频开始播放');
  2773. this.currentSubtitle = this.aiText;
  2774. uni.showToast({
  2775. title: '正在播放AI回复',
  2776. icon: 'none',
  2777. duration: 1500
  2778. });
  2779. });
  2780. // 设置播放结束事件
  2781. audioContext.onEnded(() => {
  2782. console.log('iOS小程序音频播放结束');
  2783. this.handleIOSAudioComplete(audioContext);
  2784. });
  2785. // 设置播放停止事件
  2786. audioContext.onStop(() => {
  2787. console.log('iOS小程序音频播放停止');
  2788. this.handleIOSAudioComplete(audioContext);
  2789. });
  2790. // 重点:增强的iOS错误处理
  2791. audioContext.onError((err) => {
  2792. console.error('iOS小程序音频播放错误:', err);
  2793. this.handleIOSMiniProgramError(audioContext, err);
  2794. });
  2795. // 先尝试预下载音频文件(iOS小程序推荐做法)
  2796. this.preloadIOSMiniProgramAudio(this.aiVoiceUrl)
  2797. .then((localPath) => {
  2798. console.log('iOS小程序音频预下载成功:', localPath);
  2799. audioContext.src = localPath;
  2800. /* // 设置播放超时
  2801. const playTimeout = setTimeout(() => {
  2802. if (this.isPlayingAiVoice) {
  2803. console.log('iOS小程序音频播放超时,使用文本回退');
  2804. clearTimeout(playTimeout);
  2805. this.handleIOSPlaybackTimeout(audioContext);
  2806. }
  2807. }, 5000);
  2808. */
  2809. // 尝试播放
  2810. audioContext.play();
  2811. })
  2812. .catch((error) => {
  2813. console.error('iOS小程序音频预下载失败:', error);
  2814. // 预下载失败,尝试直接播放
  2815. this.fallbackIOSMiniProgramPlay(audioContext);
  2816. });
  2817. } catch (e) {
  2818. console.error('iOS小程序音频初始化失败:', e);
  2819. this.useIOSTextFallback();
  2820. }
  2821. },
  2822. // 新增方法:iOS小程序音频预下载
  2823. preloadIOSMiniProgramAudio(audioUrl) {
  2824. return new Promise((resolve, reject) => {
  2825. console.log('开始预下载iOS小程序音频:', audioUrl);
  2826. uni.downloadFile({
  2827. url: audioUrl,
  2828. timeout: 15000, // 15秒超时
  2829. success: (res) => {
  2830. if (res.statusCode === 200) {
  2831. console.log('iOS小程序音频下载成功:', res.tempFilePath);
  2832. resolve(res.tempFilePath);
  2833. } else {
  2834. console.error('iOS小程序音频下载失败,状态码:', res.statusCode);
  2835. reject(new Error(`下载失败,状态码: ${res.statusCode}`));
  2836. }
  2837. },
  2838. fail: (err) => {
  2839. console.error('iOS小程序音频下载请求失败:', err);
  2840. reject(err);
  2841. }
  2842. });
  2843. });
  2844. },
  2845. // 新增方法:iOS小程序错误处理
  2846. handleIOSMiniProgramError(audioContext, error) {
  2847. console.error('处理iOS小程序音频错误:', error);
  2848. // 销毁音频播放器
  2849. if (audioContext) {
  2850. try {
  2851. audioContext.destroy();
  2852. } catch (e) {
  2853. console.error('销毁音频播放器失败:', e);
  2854. }
  2855. }
  2856. // 根据错误类型显示不同的提示信息
  2857. let errorMessage = '音频播放异常,显示文字';
  2858. if (error.errMsg && error.errMsg.includes('-11850')) {
  2859. console.log('iOS小程序检测到系统音频错误(-11850)');
  2860. errorMessage = '系统音频服务暂停,显示文字';
  2861. } else if (error.errCode === 10002) {
  2862. console.log('iOS小程序检测到errCode:10002,网络或资源问题');
  2863. errorMessage = '音频加载中断,显示文字';
  2864. } else if (error.errCode === 10001) {
  2865. console.log('iOS小程序检测到errCode:10001,系统错误');
  2866. errorMessage = '系统音频错误,显示文字';
  2867. } else if (error.errCode === 10003) {
  2868. console.log('iOS小程序检测到errCode:10003,文件错误');
  2869. errorMessage = '音频文件错误,显示文字';
  2870. } else if (error.errCode === 10004) {
  2871. console.log('iOS小程序检测到errCode:10004,格式不支持');
  2872. errorMessage = '音频格式不支持,显示文字';
  2873. }
  2874. // 显示用户友好的错误提示
  2875. uni.showToast({
  2876. title: errorMessage,
  2877. icon: 'none',
  2878. duration: 2000
  2879. });
  2880. // 使用文本回退
  2881. this.useIOSTextFallback();
  2882. },
  2883. // 新增方法:iOS小程序播放超时处理
  2884. handleIOSPlaybackTimeout(audioContext) {
  2885. console.log('iOS小程序音频播放超时处理');
  2886. // 销毁音频播放器
  2887. if (audioContext) {
  2888. try {
  2889. audioContext.destroy();
  2890. } catch (e) {
  2891. console.error('销毁超时音频播放器失败:', e);
  2892. }
  2893. }
  2894. // 显示超时提示
  2895. uni.showToast({
  2896. title: '音频加载超时,显示文字',
  2897. icon: 'none',
  2898. duration: 2000
  2899. });
  2900. // 使用文本回退
  2901. this.useIOSTextFallback();
  2902. },
  2903. // 新增方法:iOS小程序备选播放方法
  2904. fallbackIOSMiniProgramPlay(audioContext) {
  2905. console.log('iOS小程序使用备选播放方法');
  2906. try {
  2907. // 直接设置原始URL
  2908. audioContext.src = this.aiVoiceUrl;
  2909. // 设置较短的超时时间
  2910. const fallbackTimeout = setTimeout(() => {
  2911. if (this.isPlayingAiVoice) {
  2912. console.log('iOS小程序备选方法也超时');
  2913. clearTimeout(fallbackTimeout);
  2914. this.handleIOSPlaybackTimeout(audioContext);
  2915. }
  2916. }, 3000);
  2917. // 尝试播放
  2918. audioContext.play();
  2919. } catch (e) {
  2920. console.error('iOS小程序备选播放方法失败:', e);
  2921. this.handleIOSPlaybackTimeout(audioContext);
  2922. }
  2923. },
  2924. // 新增方法:统一的iOS音频完成处理
  2925. handleIOSAudioComplete(audioContext) {
  2926. console.log('iOS音频播放完成');
  2927. // 标记播放结束
  2928. this.isPlayingAiVoice = false;
  2929. // 延迟清空字幕,让用户有时间阅读
  2930. setTimeout(() => {
  2931. this.currentSubtitle = '';
  2932. }, 2000);
  2933. // 销毁音频播放器
  2934. if (audioContext) {
  2935. try {
  2936. audioContext.destroy();
  2937. } catch (e) {
  2938. console.error('销毁iOS音频播放器失败:', e);
  2939. }
  2940. }
  2941. // 显示继续提问选项
  2942. this.showContinueQuestionOptions();
  2943. // 检查是否需要自动跳转
  2944. this.checkPendingNavigation();
  2945. },
  2946. // 移除原来的 playMiniProgramAudioForIOS 方法,因为已经被新的 playIOSMiniProgramAudio 替代
  2947. // playMiniProgramAudioForIOS() 方法可以删除或注释掉
  2948. // 保留原有的 useIOSTextFallback 方法,但确保它能正确处理所有iOS场景
  2949. useIOSTextFallback() {
  2950. console.log('iOS使用文本回退方案');
  2951. // 确保播放状态正确
  2952. this.isPlayingAiVoice = false;
  2953. // 显示友好提示(如果还没有显示过)
  2954. if (!this.hasShownTextFallbackToast) {
  2955. this.hasShownTextFallbackToast = true;
  2956. uni.showToast({
  2957. title: '为您显示文字回复',
  2958. icon: 'none',
  2959. duration: 2000
  2960. });
  2961. }
  2962. // 显示完整文本
  2963. this.currentSubtitle = this.aiText || '抱歉,AI回复内容加载失败';
  2964. // 根据文本长度计算合适的阅读时间
  2965. const textLength = this.aiText ? this.aiText.length : 20;
  2966. const readingTime = Math.max(5000, Math.min(textLength * 100, 15000)); // 最少5秒,最多15秒
  2967. console.log(`文本长度: ${textLength}, 预计阅读时间: ${readingTime}ms`);
  2968. setTimeout(() => {
  2969. this.handleIOSAudioComplete(null);
  2970. }, readingTime);
  2971. },
  2972. // 添加新方法:iOS音频播放完成处理
  2973. handleIOSAudioPlaybackComplete() {
  2974. // 标记播放结束
  2975. this.isPlayingAiVoice = false;
  2976. // 延迟清空字幕,让用户有时间阅读
  2977. setTimeout(() => {
  2978. this.currentSubtitle = '';
  2979. }, 2000);
  2980. // 销毁音频播放器
  2981. if (this.aiAudioPlayer) {
  2982. try {
  2983. this.aiAudioPlayer.destroy();
  2984. } catch (e) {
  2985. console.error('销毁iOS音频播放器失败:', e);
  2986. }
  2987. this.aiAudioPlayer = null;
  2988. }
  2989. // 显示继续提问选项
  2990. this.showContinueQuestionOptions();
  2991. // 检查是否需要自动跳转
  2992. this.checkPendingNavigation();
  2993. },
  2994. // 添加新方法:iOS音频错误处理
  2995. handleIOSAudioError(error) {
  2996. console.error('iOS音频播放错误:', error);
  2997. // 根据错误码进行不同处理
  2998. switch (error.errCode) {
  2999. case 10001:
  3000. console.log('iOS音频系统错误,尝试重新播放');
  3001. this.retryIOSAudioPlay();
  3002. break;
  3003. case 10002:
  3004. console.log('iOS音频网络错误,使用备选方案');
  3005. this.handleIOS10002Error();
  3006. break;
  3007. case 10003:
  3008. console.log('iOS音频文件错误,使用文本回退');
  3009. this.useIOSTextFallback();
  3010. break;
  3011. case 10004:
  3012. console.log('iOS音频格式不支持,使用文本回退');
  3013. this.useIOSTextFallback();
  3014. break;
  3015. default:
  3016. console.log('iOS音频未知错误,使用文本回退');
  3017. this.useIOSTextFallback();
  3018. break;
  3019. }
  3020. },
  3021. // 添加新方法:重试iOS音频播放
  3022. retryIOSAudioPlay() {
  3023. console.log('重试iOS音频播放');
  3024. // 销毁当前播放器
  3025. if (this.aiAudioPlayer) {
  3026. try {
  3027. this.aiAudioPlayer.destroy();
  3028. } catch (e) {
  3029. console.error('销毁播放器失败:', e);
  3030. }
  3031. this.aiAudioPlayer = null;
  3032. }
  3033. // 延迟后重试
  3034. setTimeout(() => {
  3035. this.fallbackIOSAudioPlay();
  3036. }, 1000);
  3037. },
  3038. // 添加新方法:iOS备选音频播放
  3039. fallbackIOSAudioPlay() {
  3040. console.log('iOS备选音频播放方案');
  3041. try {
  3042. // 创建新的音频播放器
  3043. this.aiAudioPlayer = uni.createInnerAudioContext();
  3044. // 使用更保守的设置
  3045. this.aiAudioPlayer.autoplay = true;
  3046. this.aiAudioPlayer.obeyMuteSwitch = true; // 遵循静音开关
  3047. this.aiAudioPlayer.volume = 0.8;
  3048. // 直接使用原URL
  3049. this.aiAudioPlayer.src = this.aiVoiceUrl;
  3050. // 设置简化的事件监听
  3051. this.aiAudioPlayer.onPlay(() => {
  3052. console.log('iOS备选方案:音频开始播放');
  3053. this.currentSubtitle = this.aiText;
  3054. });
  3055. this.aiAudioPlayer.onEnded(() => {
  3056. console.log('iOS备选方案:音频播放结束');
  3057. this.handleIOSAudioPlaybackComplete();
  3058. });
  3059. this.aiAudioPlayer.onError((err) => {
  3060. console.error('iOS备选方案:音频播放错误:', err);
  3061. this.useIOSTextFallback();
  3062. });
  3063. // 设置超时,如果5秒内没有开始播放,使用文本回退
  3064. setTimeout(() => {
  3065. if (this.isPlayingAiVoice && this.aiAudioPlayer) {
  3066. console.log('iOS音频播放超时,使用文本回退');
  3067. this.useIOSTextFallback();
  3068. }
  3069. }, 5000);
  3070. } catch (e) {
  3071. console.error('iOS备选音频播放失败:', e);
  3072. this.useIOSTextFallback();
  3073. }
  3074. },
  3075. // 修改 playMiniProgramAudio 方法,添加iOS特殊处理
  3076. playMiniProgramAudio() {
  3077. console.log('使用小程序原生音频API播放:', this.aiVoiceUrl);
  3078. // 标记正在播放AI语音
  3079. this.isPlayingAiVoice = true;
  3080. // 确保在播放期间不显示继续提问按钮
  3081. this.showContinueQuestionButton = false;
  3082. this.showEndInterviewButton = false;
  3083. // 检查音频URL是否有效
  3084. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === '') {
  3085. console.error('无效的音频URL');
  3086. this.handleAudioPlaybackFailure();
  3087. return;
  3088. }
  3089. // 检查平台
  3090. const systemInfo = uni.getSystemInfoSync();
  3091. const isIOS = systemInfo.platform === 'ios';
  3092. // iOS小程序特殊处理
  3093. if (isIOS) {
  3094. this.playMiniProgramAudioForIOS();
  3095. return;
  3096. }
  3097. // 非iOS平台的原有逻辑
  3098. try {
  3099. // 使用wx.createInnerAudioContext API (微信小程序)
  3100. const audioContext = wx.createInnerAudioContext();
  3101. // 设置音频属性
  3102. audioContext.autoplay = false;
  3103. audioContext.obeyMuteSwitch = false; // 忽略静音开关
  3104. // 监听播放开始事件
  3105. audioContext.onPlay(() => {
  3106. console.log('AI语音开始播放');
  3107. // 显示正在播放提示
  3108. uni.showToast({
  3109. title: '正在播放AI回复',
  3110. icon: 'none',
  3111. duration: 2000
  3112. });
  3113. });
  3114. // 监听播放结束事件
  3115. audioContext.onEnded(() => {
  3116. console.log('AI语音播放结束');
  3117. // 标记播放结束
  3118. this.isPlayingAiVoice = false;
  3119. // 清空字幕
  3120. this.currentSubtitle = '';
  3121. // 销毁音频播放器
  3122. audioContext.destroy();
  3123. // 播放结束后显示继续提问选项,而不是直接跳转
  3124. this.showContinueQuestionOptions();
  3125. });
  3126. // 监听播放错误事件
  3127. audioContext.onError((err) => {
  3128. console.error('AI语音播放错误:', err);
  3129. // 标记播放结束
  3130. this.isPlayingAiVoice = false;
  3131. // 清空字幕
  3132. this.currentSubtitle = '';
  3133. // 销毁音频播放器
  3134. audioContext.destroy();
  3135. // 播放错误时也显示继续提问选项
  3136. this.showContinueQuestionOptions();
  3137. });
  3138. // 设置音频源
  3139. audioContext.src = this.aiVoiceUrl;
  3140. // 添加超时处理,如果5秒内没有开始播放,则认为出错
  3141. const playTimeout = setTimeout(() => {
  3142. if (this.isPlayingAiVoice) {
  3143. console.warn('小程序音频播放超时,尝试备选方法');
  3144. this.tryMiniProgramFallbackAudioPlay();
  3145. }
  3146. }, 5000);
  3147. // 延迟一小段时间再播放,确保src已经设置完成
  3148. setTimeout(() => {
  3149. audioContext.play();
  3150. // 播放开始后清除超时
  3151. clearTimeout(playTimeout);
  3152. }, 300);
  3153. } catch (e) {
  3154. console.error('小程序播放AI语音时发生错误:', e);
  3155. // 标记播放结束
  3156. this.isPlayingAiVoice = false;
  3157. // 显示错误提示
  3158. uni.showToast({
  3159. title: '音频播放失败,请查看文字回复',
  3160. icon: 'none',
  3161. duration: 2000
  3162. });
  3163. // 使用文本显示方式作为备选方案
  3164. this.handleAudioPlaybackFailure();
  3165. }
  3166. },
  3167. // 添加新方法:iOS小程序音频播放
  3168. playMiniProgramAudioForIOS() {
  3169. console.log('iOS小程序环境:开始播放AI语音');
  3170. // 重置错误标记
  3171. this.hasShownTextFallbackToast = false;
  3172. try {
  3173. // 创建音频上下文
  3174. const audioContext = wx.createInnerAudioContext();
  3175. // iOS小程序特殊配置
  3176. audioContext.autoplay = false;
  3177. audioContext.obeyMuteSwitch = true; // iOS上遵循静音开关
  3178. audioContext.volume = 1.0;
  3179. // 设置错误处理
  3180. audioContext.onError((err) => {
  3181. console.error('iOS小程序音频播放错误:', err);
  3182. // 检查具体错误类型
  3183. if (err.errMsg && err.errMsg.includes('-11850')) {
  3184. console.log('iOS小程序检测到系统音频错误(-11850)');
  3185. uni.showToast({
  3186. title: '系统音频暂停,显示文字',
  3187. icon: 'none',
  3188. duration: 2000
  3189. });
  3190. } else if (err.errCode === 10002) {
  3191. console.log('iOS小程序检测到errCode:10002,网络或资源问题');
  3192. uni.showToast({
  3193. title: '音频加载中断,显示文字',
  3194. icon: 'none',
  3195. duration: 2000
  3196. });
  3197. } else {
  3198. console.log('iOS小程序其他音频错误:', err.errCode);
  3199. uni.showToast({
  3200. title: '音频播放异常,显示文字',
  3201. icon: 'none',
  3202. duration: 2000
  3203. });
  3204. }
  3205. this.useIOSMiniProgramTextFallback(audioContext);
  3206. });
  3207. // 先尝试直接播放,如果失败再下载
  3208. audioContext.src = this.aiVoiceUrl;
  3209. // 设置成功事件监听
  3210. audioContext.onPlay(() => {
  3211. console.log('iOS小程序音频开始播放');
  3212. this.currentSubtitle = this.aiText;
  3213. uni.showToast({
  3214. title: '正在播放AI回复',
  3215. icon: 'none',
  3216. duration: 1500
  3217. });
  3218. });
  3219. audioContext.onEnded(() => {
  3220. console.log('iOS小程序音频播放结束');
  3221. this.handleIOSMiniProgramAudioComplete(audioContext);
  3222. });
  3223. // 设置较短的播放超时时间
  3224. const playTimeout = setTimeout(() => {
  3225. if (this.isPlayingAiVoice) {
  3226. console.log('iOS小程序音频播放超时,切换到文本显示');
  3227. clearTimeout(playTimeout);
  3228. this.useIOSMiniProgramTextFallback(audioContext);
  3229. }
  3230. }, 3000); // 缩短到3秒
  3231. // 尝试播放
  3232. audioContext.play();
  3233. } catch (e) {
  3234. console.error('iOS小程序音频播放初始化失败:', e);
  3235. this.handleAudioPlaybackFailure();
  3236. }
  3237. },
  3238. // 添加新方法:iOS小程序音频播放完成
  3239. handleIOSMiniProgramAudioComplete(audioContext) {
  3240. this.isPlayingAiVoice = false;
  3241. setTimeout(() => {
  3242. this.currentSubtitle = '';
  3243. }, 2000);
  3244. if (audioContext) {
  3245. try {
  3246. audioContext.destroy();
  3247. } catch (e) {
  3248. console.error('销毁iOS小程序音频播放器失败:', e);
  3249. }
  3250. }
  3251. this.showContinueQuestionOptions();
  3252. this.checkPendingNavigation();
  3253. },
  3254. // 添加新方法:iOS小程序音频错误处理
  3255. handleIOSMiniProgramAudioError(audioContext, error) {
  3256. console.error('iOS小程序音频错误:', error);
  3257. // 根据错误码处理
  3258. if (error.errCode === 10002) {
  3259. this.useIOSMiniProgramTextFallback(audioContext);
  3260. } else {
  3261. // 其他错误也使用文本回退
  3262. this.useIOSMiniProgramTextFallback(audioContext);
  3263. }
  3264. },
  3265. // 添加新方法:iOS小程序文本回退
  3266. useIOSMiniProgramTextFallback(audioContext) {
  3267. console.log('iOS小程序使用文本回退方案');
  3268. // 销毁音频播放器
  3269. if (audioContext) {
  3270. try {
  3271. audioContext.destroy();
  3272. } catch (e) {
  3273. console.error('销毁音频播放器失败:', e);
  3274. }
  3275. }
  3276. // 确保播放状态正确
  3277. this.isPlayingAiVoice = false;
  3278. // 显示完整文本
  3279. this.currentSubtitle = this.aiText || '抱歉,AI回复内容加载失败';
  3280. // 根据文本长度计算阅读时间
  3281. const textLength = this.aiText ? this.aiText.length : 20;
  3282. const readingTime = Math.max(6000, Math.min(textLength * 120, 18000)); // 最少6秒,最多18秒
  3283. setTimeout(() => {
  3284. this.handleIOSMiniProgramAudioComplete(null);
  3285. }, readingTime);
  3286. },
  3287. // 添加新方法:处理音频播放错误
  3288. handleAudioPlaybackError(error) {
  3289. console.error('音频播放错误:', error);
  3290. // 尝试使用备选方法
  3291. if (!this.hasTriedFallbackMethod) {
  3292. this.hasTriedFallbackMethod = true;
  3293. this.tryFallbackAudioPlay();
  3294. return;
  3295. }
  3296. // 如果备选方法也失败,回退到文本显示
  3297. this.handleAudioPlaybackFailure();
  3298. },
  3299. // 添加新方法:尝试备选音频播放方法
  3300. tryFallbackAudioPlay() {
  3301. console.log('使用备选方法播放音频:', this.aiVoiceUrl);
  3302. // 如果当前播放器存在,先销毁
  3303. if (this.aiAudioPlayer) {
  3304. try {
  3305. this.aiAudioPlayer.destroy();
  3306. } catch (e) {
  3307. console.error('销毁音频播放器失败:', e);
  3308. }
  3309. this.aiAudioPlayer = null;
  3310. }
  3311. // 使用uni.downloadFile先下载音频文件
  3312. uni.downloadFile({
  3313. url: this.aiVoiceUrl,
  3314. success: (res) => {
  3315. if (res.statusCode === 200) {
  3316. console.log('音频文件下载成功:', res.tempFilePath);
  3317. // 使用新的音频上下文播放本地文件
  3318. const newAudioPlayer = uni.createInnerAudioContext();
  3319. this.aiAudioPlayer = newAudioPlayer;
  3320. newAudioPlayer.autoplay = true;
  3321. newAudioPlayer.src = res.tempFilePath;
  3322. // 设置事件监听
  3323. newAudioPlayer.onPlay(() => {
  3324. console.log('备选方法:音频开始播放');
  3325. });
  3326. newAudioPlayer.onEnded(() => {
  3327. console.log('备选方法:音频播放结束');
  3328. this.isPlayingAiVoice = false;
  3329. this.currentSubtitle = '';
  3330. newAudioPlayer.destroy();
  3331. this.aiAudioPlayer = null;
  3332. this.showContinueQuestionOptions();
  3333. this.checkPendingNavigation();
  3334. });
  3335. newAudioPlayer.onError((err) => {
  3336. console.error('备选方法:音频播放错误:', err);
  3337. this.isPlayingAiVoice = false;
  3338. this.currentSubtitle = '';
  3339. newAudioPlayer.destroy();
  3340. this.aiAudioPlayer = null;
  3341. // 显示错误提示
  3342. uni.showToast({
  3343. title: '无法播放音频,请查看文字回复',
  3344. icon: 'none',
  3345. duration: 2000
  3346. });
  3347. this.showContinueQuestionOptions();
  3348. this.checkPendingNavigation();
  3349. });
  3350. } else {
  3351. console.error('音频文件下载失败:', res);
  3352. this.handleAudioPlaybackFailure();
  3353. }
  3354. },
  3355. fail: (err) => {
  3356. console.error('音频文件下载请求失败:', err);
  3357. this.handleAudioPlaybackFailure();
  3358. }
  3359. });
  3360. },
  3361. // 添加新方法:处理音频播放失败的情况
  3362. handleAudioPlaybackFailure() {
  3363. // 标记播放结束
  3364. this.isPlayingAiVoice = false;
  3365. // 显示错误提示
  3366. uni.showToast({
  3367. title: '无法播放音频,将直接显示文字回复',
  3368. icon: 'none',
  3369. duration: 2000
  3370. });
  3371. // 确保字幕显示足够长的时间(10秒)让用户阅读
  3372. this.currentSubtitle = this.aiText;
  3373. // 设置定时器,10秒后清除字幕并显示继续提问选项
  3374. setTimeout(() => {
  3375. this.currentSubtitle = '';
  3376. this.showContinueQuestionOptions();
  3377. this.checkPendingNavigation();
  3378. }, 10000);
  3379. },
  3380. // 添加新方法:从视频中提取音频
  3381. async extractAudioFromVideo(videoBlob) {
  3382. console.log('开始从视频中提取音频');
  3383. return new Promise((resolve, reject) => {
  3384. try {
  3385. // 创建一个视频元素来加载视频
  3386. const videoElement = document.createElement('video');
  3387. videoElement.muted = false;
  3388. videoElement.autoplay = false;
  3389. // 创建音频上下文
  3390. const audioContext = new (window.AudioContext || window.webkitAudioContext)();
  3391. let audioDestination = null;
  3392. let mediaRecorder = null;
  3393. const audioChunks = [];
  3394. // 设置视频源
  3395. videoElement.src = URL.createObjectURL(videoBlob);
  3396. videoElement.onloadedmetadata = () => {
  3397. console.log('视频元数据已加载,视频时长:', videoElement.duration);
  3398. // 创建媒体源和目标节点
  3399. const source = audioContext.createMediaElementSource(videoElement);
  3400. audioDestination = audioContext.createMediaStreamDestination();
  3401. source.connect(audioDestination);
  3402. // 创建MediaRecorder来录制音频
  3403. mediaRecorder = new MediaRecorder(audioDestination.stream);
  3404. mediaRecorder.ondataavailable = (event) => {
  3405. if (event.data.size > 0) {
  3406. audioChunks.push(event.data);
  3407. }
  3408. };
  3409. mediaRecorder.onstop = () => {
  3410. const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
  3411. console.log('音频提取完成,大小:', audioBlob.size);
  3412. resolve(audioBlob);
  3413. };
  3414. // 开始播放视频并录制音频
  3415. videoElement.onplay = () => {
  3416. mediaRecorder.start(100);
  3417. };
  3418. videoElement.onended = () => {
  3419. mediaRecorder.stop();
  3420. audioContext.close();
  3421. };
  3422. // 开始播放
  3423. videoElement.play();
  3424. };
  3425. videoElement.onerror = (err) => {
  3426. console.error('视频加载失败:', err);
  3427. reject(new Error('视频加载失败'));
  3428. };
  3429. } catch (err) {
  3430. console.error('提取音频失败:', err);
  3431. reject(err);
  3432. }
  3433. });
  3434. },
  3435. // 添加新方法:上传音频文件
  3436. async uploadAudioFile(audioBlob, videoUrl) {
  3437. console.log('准备上传音频文件');
  3438. // 创建FormData
  3439. const formData = new FormData();
  3440. // 将Blob转换为File对象
  3441. const fileName = `audio_${Date.now()}.wav`;
  3442. const audioFile = new File([audioBlob], fileName, { type: 'audio/wav' });
  3443. // 添加文件和其他参数
  3444. formData.append('audio_file', audioFile);
  3445. formData.append('video_url', videoUrl);
  3446. formData.append('tenant_id', uni.getStorageSync('tenant_id') || '1');
  3447. formData.append('openid', uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')).openid : '');
  3448. formData.append('application_id', uni.getStorageSync('appId') || '');
  3449. return new Promise((resolve, reject) => {
  3450. // 创建XMLHttpRequest
  3451. const xhr = new XMLHttpRequest();
  3452. xhr.open('POST', `${apiBaseUrl}/api/voice_ai_interaction`, true);
  3453. xhr.timeout = 60000; // 60秒超时
  3454. xhr.onload = () => {
  3455. if (xhr.status === 200) {
  3456. try {
  3457. const response = JSON.parse(xhr.responseText);
  3458. resolve(response);
  3459. } catch (e) {
  3460. reject(new Error('解析响应失败'));
  3461. }
  3462. } else {
  3463. reject(new Error('HTTP状态: ' + xhr.status));
  3464. }
  3465. };
  3466. xhr.onerror = () => {
  3467. reject(new Error('网络错误'));
  3468. };
  3469. xhr.ontimeout = () => {
  3470. reject(new Error('请求超时'));
  3471. };
  3472. xhr.send(formData);
  3473. });
  3474. },
  3475. // 添加一个新方法:使用小程序原生音频API
  3476. playMiniProgramAudio() {
  3477. console.log('使用小程序原生音频API播放:', this.aiVoiceUrl);
  3478. // 标记正在播放AI语音
  3479. this.isPlayingAiVoice = true;
  3480. // 确保在播放期间不显示继续提问按钮
  3481. this.showContinueQuestionButton = false;
  3482. this.showEndInterviewButton = false;
  3483. // 检查音频URL是否有效
  3484. if (!this.aiVoiceUrl || this.aiVoiceUrl.trim() === '') {
  3485. console.error('无效的音频URL');
  3486. // 显示错误提示
  3487. uni.showToast({
  3488. title: '音频加载失败',
  3489. icon: 'none',
  3490. duration: 2000
  3491. });
  3492. // 标记播放结束
  3493. this.isPlayingAiVoice = false;
  3494. // 显示继续提问选项
  3495. this.showContinueQuestionOptions();
  3496. return;
  3497. }
  3498. try {
  3499. // 使用wx.createInnerAudioContext API (微信小程序)
  3500. const audioContext = wx.createInnerAudioContext();
  3501. // 设置音频属性
  3502. audioContext.autoplay = false;
  3503. audioContext.obeyMuteSwitch = false; // 忽略静音开关
  3504. // 监听播放开始事件
  3505. audioContext.onPlay(() => {
  3506. console.log('AI语音开始播放');
  3507. // 显示正在播放提示
  3508. uni.showToast({
  3509. title: '正在播放AI回复',
  3510. icon: 'none',
  3511. duration: 2000
  3512. });
  3513. });
  3514. // 监听播放结束事件
  3515. audioContext.onEnded(() => {
  3516. console.log('AI语音播放结束');
  3517. // 标记播放结束
  3518. this.isPlayingAiVoice = false;
  3519. // 清空字幕
  3520. this.currentSubtitle = '';
  3521. // 销毁音频播放器
  3522. audioContext.destroy();
  3523. // 播放结束后显示继续提问选项,而不是直接跳转
  3524. this.showContinueQuestionOptions();
  3525. });
  3526. // 监听播放错误事件
  3527. audioContext.onError((err) => {
  3528. console.error('AI语音播放错误:', err);
  3529. // 标记播放结束
  3530. this.isPlayingAiVoice = false;
  3531. // 清空字幕
  3532. this.currentSubtitle = '';
  3533. // 销毁音频播放器
  3534. audioContext.destroy();
  3535. // 播放错误时也显示继续提问选项
  3536. this.showContinueQuestionOptions();
  3537. });
  3538. // 设置音频源
  3539. audioContext.src = this.aiVoiceUrl;
  3540. // 添加超时处理,如果5秒内没有开始播放,则认为出错
  3541. const playTimeout = setTimeout(() => {
  3542. if (this.isPlayingAiVoice) {
  3543. console.warn('小程序音频播放超时,尝试备选方法');
  3544. this.tryMiniProgramFallbackAudioPlay();
  3545. }
  3546. }, 5000);
  3547. // 延迟一小段时间再播放,确保src已经设置完成
  3548. setTimeout(() => {
  3549. audioContext.play();
  3550. // 播放开始后清除超时
  3551. clearTimeout(playTimeout);
  3552. }, 300);
  3553. } catch (e) {
  3554. console.error('小程序播放AI语音时发生错误:', e);
  3555. // 标记播放结束
  3556. this.isPlayingAiVoice = false;
  3557. // 显示错误提示
  3558. uni.showToast({
  3559. title: '音频播放失败,请查看文字回复',
  3560. icon: 'none',
  3561. duration: 2000
  3562. });
  3563. // 使用文本显示方式作为备选方案
  3564. this.handleAudioPlaybackFailure();
  3565. }
  3566. },
  3567. // 添加新方法:小程序环境的备选音频播放方法
  3568. tryMiniProgramFallbackAudioPlay() {
  3569. console.log('使用小程序备选方法播放音频:', this.aiVoiceUrl);
  3570. // 先下载音频文件到本地
  3571. uni.downloadFile({
  3572. url: this.aiVoiceUrl,
  3573. success: (res) => {
  3574. if (res.statusCode === 200) {
  3575. console.log('小程序音频文件下载成功:', res.tempFilePath);
  3576. // 使用新的音频上下文播放本地文件
  3577. const newAudioContext = wx.createInnerAudioContext();
  3578. newAudioContext.autoplay = true;
  3579. newAudioContext.src = res.tempFilePath;
  3580. // 设置事件监听
  3581. newAudioContext.onPlay(() => {
  3582. console.log('小程序备选方法:音频开始播放');
  3583. });
  3584. newAudioContext.onEnded(() => {
  3585. console.log('小程序备选方法:音频播放结束');
  3586. this.isPlayingAiVoice = false;
  3587. this.currentSubtitle = '';
  3588. newAudioContext.destroy();
  3589. this.showContinueQuestionOptions();
  3590. this.checkPendingNavigation();
  3591. });
  3592. newAudioContext.onError((err) => {
  3593. console.error('小程序备选方法:音频播放错误:', err);
  3594. this.isPlayingAiVoice = false;
  3595. this.currentSubtitle = '';
  3596. newAudioContext.destroy();
  3597. // 尝试使用系统播放器作为最后的备选方案
  3598. this.trySystemAudioPlayer();
  3599. });
  3600. } else {
  3601. console.error('小程序音频文件下载失败:', res);
  3602. this.handleAudioPlaybackFailure();
  3603. }
  3604. },
  3605. fail: (err) => {
  3606. console.error('小程序音频文件下载请求失败:', err);
  3607. this.handleAudioPlaybackFailure();
  3608. }
  3609. });
  3610. },
  3611. // 添加新方法:尝试使用系统播放器
  3612. trySystemAudioPlayer() {
  3613. console.log('尝试使用系统播放器播放音频');
  3614. // 检查平台
  3615. const systemInfo = uni.getSystemInfoSync();
  3616. if (systemInfo.platform === 'android' || systemInfo.platform === 'ios') {
  3617. // 在移动端尝试使用系统播放器
  3618. uni.showToast({
  3619. title: '正在使用系统播放器',
  3620. icon: 'none',
  3621. duration: 2000
  3622. });
  3623. // 使用uni.playVoice API (如果可用)
  3624. if (typeof uni.playVoice === 'function') {
  3625. uni.playVoice({
  3626. filePath: this.aiVoiceUrl,
  3627. success: () => {
  3628. console.log('系统播放器播放成功');
  3629. setTimeout(() => {
  3630. this.isPlayingAiVoice = false;
  3631. this.currentSubtitle = '';
  3632. this.showContinueQuestionOptions();
  3633. }, 5000); // 假设音频长度约为5秒
  3634. },
  3635. fail: (err) => {
  3636. console.error('系统播放器播放失败:', err);
  3637. this.handleAudioPlaybackFailure();
  3638. }
  3639. });
  3640. } else {
  3641. // 如果uni.playVoice不可用,尝试使用uni.openDocument
  3642. uni.openDocument({
  3643. filePath: this.aiVoiceUrl,
  3644. fileType: 'audio',
  3645. success: () => {
  3646. console.log('使用系统应用打开音频文件');
  3647. // 由于控制权交给了系统,我们需要手动处理后续流程
  3648. setTimeout(() => {
  3649. this.isPlayingAiVoice = false;
  3650. this.currentSubtitle = '';
  3651. this.showContinueQuestionOptions();
  3652. }, 8000); // 给用户足够的时间听完音频
  3653. },
  3654. fail: (err) => {
  3655. console.error('无法使用系统应用打开音频:', err);
  3656. this.handleAudioPlaybackFailure();
  3657. }
  3658. });
  3659. }
  3660. } else {
  3661. // 在其他平台上直接使用文本显示
  3662. this.handleAudioPlaybackFailure();
  3663. }
  3664. },
  3665. // 修改 showContinueQuestionOptions 方法,确保状态正确设置
  3666. showContinueQuestionOptions() {
  3667. // 如果正在播放AI语音,不执行此方法
  3668. if (this.isPlayingAiVoice) {
  3669. console.log('AI语音正在播放,暂不显示继续提问选项');
  3670. return;
  3671. }
  3672. // 增加提问轮次计数
  3673. this.questionRound++;
  3674. // 清空字幕
  3675. this.currentSubtitle = '';
  3676. // 直接跳转到下一页,不再显示继续提问按钮
  3677. console.log('准备跳转到下一页面');
  3678. // 延迟3秒后跳转,给用户一些时间查看状态
  3679. setTimeout(() => {
  3680. this.navigateToNextPage();
  3681. }, 3000);
  3682. // 确保所有录制状态被重置
  3683. this.isRecording = false;
  3684. if (this.recordingTimer) {
  3685. clearInterval(this.recordingTimer);
  3686. this.recordingTimer = null;
  3687. }
  3688. // 重置录制相关数据
  3689. this.recordedChunks = [];
  3690. this.recordingTimerCount = 0;
  3691. this.remainingTime = this.maxRecordingTime;
  3692. },
  3693. // 修复 handleContinueQuestionClick 方法,确保状态正确重置
  3694. handleContinueQuestionClick() {
  3695. // 如果AI语音仍在播放,不允许继续提问
  3696. if (this.isPlayingAiVoice) {
  3697. uni.showToast({
  3698. title: '请等待面试官回复完成',
  3699. icon: 'none',
  3700. duration: 2000
  3701. });
  3702. return;
  3703. }
  3704. // 隐藏继续提问和结束面试按钮
  3705. this.showContinueQuestionButton = false;
  3706. this.showEndInterviewButton = false;
  3707. // 重置录制相关状态
  3708. this.isRecording = false;
  3709. this.recordingTimerCount = 0;
  3710. this.remainingTime = this.maxRecordingTime;
  3711. this.recordingTimeDisplay = '00:00 / ' + this.formatTime(this.maxRecordingTime);
  3712. // 清空已录制的数据
  3713. this.recordedChunks = [];
  3714. // 如果有正在进行的录制,先停止
  3715. if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
  3716. this.mediaRecorder.stop();
  3717. }
  3718. if (this.recorder) {
  3719. try {
  3720. this.cameraContext.stopRecord();
  3721. } catch (e) {
  3722. console.error('停止录制失败:', e);
  3723. }
  3724. this.recorder = null;
  3725. }
  3726. // 延迟一小段时间再开始新的录制,确保之前的录制已完全停止
  3727. setTimeout(() => {
  3728. // 开始新一轮录制
  3729. this.startRecordingAnswer();
  3730. }, 500);
  3731. },
  3732. // 添加新方法:处理结束面试按钮点击
  3733. handleEndInterviewClick() {
  3734. // 隐藏继续提问和结束面试按钮
  3735. this.showContinueQuestionButton = false;
  3736. this.showEndInterviewButton = false;
  3737. // 显示结束提示
  3738. /* uni.showToast({
  3739. title: '面试已结束',
  3740. icon: 'success',
  3741. duration: 1500
  3742. }); */
  3743. // 执行现有的跳转逻辑
  3744. this.navigateToNextPage();
  3745. },
  3746. // 添加新方法:处理录制错误
  3747. handleRecordingError(error) {
  3748. console.error('录制错误:', error);
  3749. // 显示错误提示
  3750. uni.showToast({
  3751. title: '录制失败,请重试',
  3752. icon: 'none',
  3753. duration: 2000
  3754. });
  3755. // 重置录制状态
  3756. this.isRecording = false;
  3757. this.showStopRecordingButton = false;
  3758. // 显示重试按钮
  3759. this.showStartRecordingButton = true;
  3760. },
  3761. // 添加新方法:处理上传错误
  3762. handleUploadError(error) {
  3763. console.error('上传错误:', error);
  3764. // 显示错误提示
  3765. uni.showToast({
  3766. title: '上传失败,请重试',
  3767. icon: 'none',
  3768. duration: 2000
  3769. });
  3770. // 显示重试按钮
  3771. this.showRetryButton = true;
  3772. // 隐藏上传状态
  3773. this.showUploadStatus = false;
  3774. this.isUploading = false;
  3775. },
  3776. // 添加新方法:检查是否有待处理的自动跳转
  3777. checkPendingNavigation() {
  3778. // 确保所有处理都已完成
  3779. if (this.pendingAutoNavigation &&
  3780. !this.isPlayingAiVoice &&
  3781. !this.isUploading &&
  3782. !this.currentSubtitle) { // 确保字幕也已经清空
  3783. console.log('所有处理已完成,准备跳转');
  3784. // 重置标志
  3785. this.pendingAutoNavigation = false;
  3786. // 延迟跳转,给用户一点时间看到最终状态
  3787. setTimeout(() => {
  3788. this.navigateToNextPage();
  3789. }, 1000);
  3790. }
  3791. },
  3792. // 添加新方法:显示文本回复
  3793. displayTextResponse() {
  3794. // 标记为正在"播放"
  3795. this.isPlayingAiVoice = true;
  3796. // 显示AI回复文本
  3797. this.currentSubtitle = '';
  3798. // 计算每个字的显示时间(假设每个字需要200毫秒)
  3799. const charDisplayTime = 200;
  3800. let currentIndex = 0;
  3801. // 使用定时器逐字显示文本
  3802. const textInterval = setInterval(() => {
  3803. if (currentIndex <= this.aiText.length) {
  3804. this.currentSubtitle = this.aiText.substring(0, currentIndex);
  3805. currentIndex++;
  3806. } else {
  3807. // 文本显示完成
  3808. clearInterval(textInterval);
  3809. // 等待用户阅读完整文本
  3810. setTimeout(() => {
  3811. // 标记播放结束
  3812. this.isPlayingAiVoice = false;
  3813. // 清空字幕
  3814. this.currentSubtitle = '';
  3815. // 显示继续提问选项
  3816. this.showContinueQuestionOptions();
  3817. // 检查是否需要自动跳转
  3818. this.checkPendingNavigation();
  3819. }, 3000); // 给用户3秒时间阅读完整文本
  3820. }
  3821. }, charDisplayTime);
  3822. },
  3823. // 添加新方法:检查不支持的音频格式
  3824. checkUnsupportedAudioFormat(url) {
  3825. if (!url) return false;
  3826. // 获取URL的文件扩展名
  3827. const extension = url.split('.').pop().toLowerCase();
  3828. // 检查平台
  3829. const systemInfo = uni.getSystemInfoSync();
  3830. const isMiniProgram = systemInfo.uniPlatform && systemInfo.uniPlatform.startsWith('mp-');
  3831. if (isMiniProgram) {
  3832. // 微信小程序支持的音频格式有限,主要是mp3和wav
  3833. // 检查是否是不支持的格式
  3834. const unsupportedFormats = ['ogg', 'flac', 'aac', 'm4a', 'opus'];
  3835. return unsupportedFormats.includes(extension);
  3836. }
  3837. return false;
  3838. },
  3839. // 修改文本回退方法
  3840. useIOSTextFallback() {
  3841. console.log('切换到文本显示模式');
  3842. this.isPlayingAiVoice = false;
  3843. if (!this.hasShownTextFallbackToast) {
  3844. this.hasShownTextFallbackToast = true;
  3845. uni.showToast({
  3846. title: '已切换为文字显示',
  3847. icon: 'none',
  3848. duration: 2000
  3849. });
  3850. }
  3851. // 显示AI回复文本
  3852. this.currentSubtitle = this.aiText || '抱歉,无法获取AI回复内容';
  3853. // 根据文本长度计算阅读时间
  3854. const textLength = this.aiText ? this.aiText.length : 20;
  3855. const readingTime = Math.max(8000, Math.min(textLength * 150, 20000)); // 最少8秒,最多20秒
  3856. console.log(`文本长度: ${textLength}字,预计阅读时间: ${readingTime/1000}秒`);
  3857. // 等待用户阅读完成后再跳转
  3858. setTimeout(() => {
  3859. this.navigateToNextPage();
  3860. }, readingTime);
  3861. }
  3862. }
  3863. }
  3864. </script>
  3865. <style scoped>
  3866. .identity-verify-container {
  3867. padding: 0;
  3868. max-width: 100%;
  3869. margin: 0 auto;
  3870. height: 100vh;
  3871. display: flex;
  3872. flex-direction: column;
  3873. background-color: #f5f5f5;
  3874. }
  3875. .digital-human-container {
  3876. position: relative;
  3877. width: 100%;
  3878. height: 100vh;
  3879. overflow: hidden;
  3880. background-color: #f0f0f0;
  3881. }
  3882. .digital-human-video {
  3883. width: 100%;
  3884. height: 100%;
  3885. display: flex;
  3886. justify-content: center;
  3887. align-items: center;
  3888. }
  3889. /* 用户摄像头容器样式 */
  3890. .user-camera-container {
  3891. position: absolute;
  3892. top: 50px;
  3893. right: 5px;
  3894. width: 110px; /* 稍微增加宽度 */
  3895. height: 160px; /* 稍微增加高度 */
  3896. border-radius: 4px; /* 减小圆角 */
  3897. overflow: hidden;
  3898. box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  3899. z-index: 20;
  3900. }
  3901. /* 用户摄像头视频样式 */
  3902. .user-camera-video {
  3903. width: 100%;
  3904. height: 100%;
  3905. object-fit: cover;
  3906. background-color: #333;
  3907. }
  3908. .video-player {
  3909. width: 100%;
  3910. height: 100%;
  3911. object-fit: cover;
  3912. outline: none; /* 移除视频获得焦点时的轮廓 */
  3913. -webkit-tap-highlight-color: transparent; /* 移除移动设备上的点击高亮 */
  3914. }
  3915. /* 隐藏视频控制条 */
  3916. video::-webkit-media-controls {
  3917. display: none !important;
  3918. }
  3919. video::-webkit-media-controls-enclosure {
  3920. display: none !important;
  3921. }
  3922. video::-webkit-media-controls-panel {
  3923. display: none !important;
  3924. }
  3925. video::-webkit-media-controls-play-button {
  3926. display: none !important;
  3927. }
  3928. video::-webkit-media-controls-timeline {
  3929. display: none !important;
  3930. }
  3931. video::-webkit-media-controls-current-time-display {
  3932. display: none !important;
  3933. }
  3934. video::-webkit-media-controls-time-remaining-display {
  3935. display: none !important;
  3936. }
  3937. video::-webkit-media-controls-mute-button {
  3938. display: none !important;
  3939. }
  3940. video::-webkit-media-controls-volume-slider {
  3941. display: none !important;
  3942. }
  3943. video::-webkit-media-controls-fullscreen-button {
  3944. display: none !important;
  3945. }
  3946. /* 修改字幕覆盖层样式,使其与图片中的样式一致 */
  3947. .subtitle-overlay {
  3948. position: absolute;
  3949. bottom: 180px; /* 调整位置,使其更靠近底部 */
  3950. left: 5%; /* 从左侧留出5%的空间 */
  3951. width: 80%; /* 宽度设为90%,两侧各留5%实现居中 */
  3952. padding: 15px 20px; /* 增加左右内边距 */
  3953. border-radius: 15px;
  3954. background-color: rgba(255, 255, 255, 0.9); /* 白色半透明背景 */
  3955. color: #333; /* 文字颜色为深色 */
  3956. text-align: left; /* 文字左对齐 */
  3957. font-size: 16px;
  3958. line-height: 1.5;
  3959. z-index: 10;
  3960. margin: 0 auto; /* 添加自动边距实现居中 */
  3961. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); /* 添加轻微阴影 */
  3962. }
  3963. /* 添加计时器样式,右侧显示橙色圆点和时间 */
  3964. .subtitle-overlay::after {
  3965. content: attr(data-time);
  3966. position: absolute;
  3967. right: 20px;
  3968. bottom: 15px;
  3969. color: #333;
  3970. font-size: 14px;
  3971. }
  3972. .control-panel {
  3973. padding: 15px;
  3974. display: flex;
  3975. justify-content: center;
  3976. }
  3977. .control-button {
  3978. padding: 10px 20px;
  3979. background-color: #4CAF50;
  3980. color: white;
  3981. border: none;
  3982. border-radius: 4px;
  3983. cursor: pointer;
  3984. }
  3985. .loading {
  3986. text-align: center;
  3987. margin: 20px 0;
  3988. font-size: 16px;
  3989. }
  3990. .response-container {
  3991. margin-top: 20px;
  3992. padding: 0 20px;
  3993. display: none; /* 默认隐藏调试信息 */
  3994. }
  3995. /* 当showDebugInfo为true时显示 */
  3996. .showDebugInfo .response-container {
  3997. display: block;
  3998. }
  3999. .response-item {
  4000. padding: 10px;
  4001. border: 1px solid #eee;
  4002. border-radius: 4px;
  4003. margin-bottom: 10px;
  4004. background-color: #f9f9f9;
  4005. }
  4006. .response-content {
  4007. display: flex;
  4008. flex-direction: column;
  4009. }
  4010. .answer-button-container {
  4011. position: absolute;
  4012. bottom: 50px; /* 将按钮放在底部而不是75%的位置 */
  4013. left: 50%;
  4014. transform: translateX(-50%); /* 只在X轴上平移,保持水平居中 */
  4015. z-index: 20;
  4016. }
  4017. /* 修改回答按钮样式 */
  4018. .answer-button {
  4019. width: 120px;
  4020. height: 40px; /* 改为矩形按钮 */
  4021. border-radius: 20px; /* 圆角矩形 */
  4022. background-color: #ffffff; /* 白色背景 */
  4023. color: #333; /* 深色文字 */
  4024. font-size: 16px;
  4025. border: none;
  4026. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4027. display: flex;
  4028. justify-content: center;
  4029. align-items: center;
  4030. cursor: pointer;
  4031. animation: none; /* 移除脉动动画 */
  4032. }
  4033. .stop-recording-button-container {
  4034. position: absolute;
  4035. bottom: 50px; /* 统一与其他按钮的位置 */
  4036. left: 50%;
  4037. transform: translateX(-50%);
  4038. z-index: 20;
  4039. }
  4040. .stop-recording-button {
  4041. width: 120px;
  4042. height: 40px; /* 改为矩形按钮 */
  4043. border-radius: 20px; /* 圆角矩形 */
  4044. background-color: #0039b3; /* 白色背景 */
  4045. color: #fff; /* 深色文字 */
  4046. font-size: 16px;
  4047. border: none;
  4048. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4049. display: flex;
  4050. justify-content: center;
  4051. align-items: center;
  4052. cursor: pointer;
  4053. animation: none; /* 移除脉动动画 */
  4054. }
  4055. .recording-indicator {
  4056. position: absolute;
  4057. top: 20px;
  4058. left: 20px;
  4059. display: flex;
  4060. align-items: center;
  4061. background-color: rgba(255, 255, 255, 0.9); /* 改为白色半透明背景 */
  4062. padding: 5px 10px;
  4063. border-radius: 15px;
  4064. z-index: 20;
  4065. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  4066. }
  4067. .recording-dot {
  4068. width: 12px;
  4069. height: 12px;
  4070. background-color: #ff6b00; /* 改为橙色,与图片中的颜色一致 */
  4071. border-radius: 50%;
  4072. margin-right: 8px;
  4073. animation: blink 1s infinite;
  4074. }
  4075. .recording-text {
  4076. color: #333; /* 改为深色文字 */
  4077. font-size: 14px;
  4078. }
  4079. .timer-text {
  4080. color: #333; /* 改为深色文字 */
  4081. font-size: 14px;
  4082. margin-left: 8px;
  4083. }
  4084. .start-recording-button-container {
  4085. position: absolute;
  4086. bottom: 50px; /* 统一与其他按钮的位置 */
  4087. left: 50%;
  4088. transform: translateX(-50%);
  4089. z-index: 101;
  4090. }
  4091. .start-recording-button {
  4092. width: 120px;
  4093. height: 40px; /* 改为矩形按钮 */
  4094. border-radius: 20px; /* 圆角矩形 */
  4095. background-color: #ffffff; /* 白色背景 */
  4096. color: #333; /* 深色文字 */
  4097. font-size: 16px;
  4098. border: none;
  4099. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4100. display: flex;
  4101. justify-content: center;
  4102. align-items: center;
  4103. cursor: pointer;
  4104. animation: none; /* 移除脉动动画 */
  4105. }
  4106. .retry-button-container {
  4107. position: absolute;
  4108. bottom: 30px; /* 统一与其他按钮的位置 */
  4109. left: 50%;
  4110. transform: translateX(-50%);
  4111. z-index: 20;
  4112. }
  4113. .retry-button {
  4114. padding: 10px 20px;
  4115. background-color: #ffffff; /* 白色背景 */
  4116. color: #333; /* 深色文字 */
  4117. font-size: 16px;
  4118. border: none;
  4119. border-radius: 20px; /* 圆角矩形 */
  4120. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4121. cursor: pointer;
  4122. }
  4123. .retry-button:hover {
  4124. background-color: #2980b9;
  4125. }
  4126. /* 录制时长显示 */
  4127. .recording-timer {
  4128. position: absolute;
  4129. top: 20px;
  4130. left: 130px; /* 放在录制指示器旁边 */
  4131. background-color: rgba(0, 0, 0, 0.6);
  4132. padding: 5px 10px;
  4133. border-radius: 15px;
  4134. z-index: 20;
  4135. display: flex;
  4136. flex-direction: column;
  4137. }
  4138. .timer-text {
  4139. color: #000;
  4140. font-size: 14px;
  4141. font-family: monospace; /* 使用等宽字体,使时间显示更稳定 */
  4142. }
  4143. .remaining-time {
  4144. color: white;
  4145. font-size: 12px;
  4146. margin-top: 2px;
  4147. }
  4148. .remaining-time.warning {
  4149. color: #ff6b6b; /* 剩余时间少时显示红色 */
  4150. animation: blink 1s infinite; /* 使用闪烁动画提醒用户 */
  4151. }
  4152. /* 添加上传状态指示器 */
  4153. .upload-status-indicator {
  4154. position: absolute;
  4155. top: 20px;
  4156. left: 130px; /* 放在录制指示器旁边 */
  4157. background-color: rgba(0, 0, 0, 0.6);
  4158. padding: 5px 10px;
  4159. border-radius: 15px;
  4160. z-index: 20;
  4161. }
  4162. .upload-status-content {
  4163. display: flex;
  4164. align-items: center;
  4165. }
  4166. .upload-status-icon {
  4167. width: 12px;
  4168. height: 12px;
  4169. border-radius: 50%;
  4170. margin-right: 8px;
  4171. }
  4172. .upload-status-text {
  4173. color: white;
  4174. font-size: 14px;
  4175. }
  4176. .uploading {
  4177. background-color: #e74c3c;
  4178. }
  4179. /* 添加倒计时蒙层样式 */
  4180. .countdown-overlay {
  4181. position: fixed;
  4182. top: 0;
  4183. left: 0;
  4184. width: 100%;
  4185. height: 100%;
  4186. background-color: rgba(0, 0, 0, 0.7);
  4187. z-index: 100;
  4188. display: flex;
  4189. justify-content: center;
  4190. align-items: center;
  4191. }
  4192. .countdown-content {
  4193. display: flex;
  4194. flex-direction: column;
  4195. align-items: center;
  4196. }
  4197. .countdown-number {
  4198. font-size: 80px;
  4199. color: #ffffff;
  4200. font-weight: bold;
  4201. margin-bottom: 20px;
  4202. }
  4203. .countdown-text {
  4204. font-size: 24px;
  4205. color: #ffffff;
  4206. }
  4207. /* 添加GIF播放器样式 */
  4208. .gif-player {
  4209. width: 100%;
  4210. height: 100%;
  4211. object-fit: cover;
  4212. position: absolute;
  4213. top: 0;
  4214. left: 0;
  4215. z-index: 5; /* 确保GIF在视频上方但在字幕和按钮下方 */
  4216. }
  4217. /* AI语音播放蒙层样式 */
  4218. .ai-voice-overlay {
  4219. position: fixed;
  4220. top: 0;
  4221. left: 0;
  4222. width: 100%;
  4223. height: 100%;
  4224. background-color: rgba(0, 0, 0, 0.8); /* 0.8的透明度 */
  4225. z-index: 99; /* 确保在其他元素之上,但在倒计时蒙层之下 */
  4226. display: flex;
  4227. justify-content: center;
  4228. align-items: center;
  4229. }
  4230. .ai-voice-content {
  4231. display: flex;
  4232. flex-direction: column;
  4233. align-items: center;
  4234. color: #ffffff;
  4235. }
  4236. .ai-voice-icon {
  4237. width: 40px;
  4238. height: 40px;
  4239. border-radius: 50%;
  4240. background-color: #ffffff;
  4241. margin-bottom: 15px;
  4242. position: relative;
  4243. animation: pulse 1.5s infinite;
  4244. }
  4245. .ai-voice-icon::before {
  4246. content: '';
  4247. position: absolute;
  4248. top: 50%;
  4249. left: 50%;
  4250. transform: translate(-50%, -50%);
  4251. width: 20px;
  4252. height: 20px;
  4253. background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23333"><path d="M12 15c1.66 0 3-1.34 3-3V6c0-1.66-1.34-3-3-3S9 4.34 9 6v6c0 1.66 1.34 3 3 3zm-1-9c0-.55.45-1 1-1s1 .45 1 1v6c0 .55-.45 1-1 1s-1-.45-1-1V6z"/><path d="M17 11c0 2.76-2.24 5-5 5s-5-2.24-5-5H5c0 3.53 2.61 6.43 6 6.92V21h2v-3.08c3.39-.49 6-3.39 6-6.92h-2z"/></svg>');
  4254. background-repeat: no-repeat;
  4255. background-position: center;
  4256. }
  4257. .ai-voice-text {
  4258. font-size: 18px;
  4259. color: #ffffff;
  4260. }
  4261. @keyframes pulse {
  4262. 0% {
  4263. transform: scale(1);
  4264. opacity: 1;
  4265. }
  4266. 50% {
  4267. transform: scale(1.1);
  4268. opacity: 0.8;
  4269. }
  4270. 100% {
  4271. transform: scale(1);
  4272. opacity: 1;
  4273. }
  4274. }
  4275. /* 继续提问按钮样式 */
  4276. .continue-question-container {
  4277. position: absolute;
  4278. bottom: 100px; /* 位置高于结束面试按钮 */
  4279. left: 50%;
  4280. transform: translateX(-50%);
  4281. z-index: 20;
  4282. }
  4283. .continue-question-button {
  4284. width: 120px;
  4285. height: 40px;
  4286. border-radius: 20px;
  4287. background-color: #ffffff;
  4288. color: #333;
  4289. font-size: 16px;
  4290. border: none;
  4291. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4292. display: flex;
  4293. justify-content: center;
  4294. align-items: center;
  4295. cursor: pointer;
  4296. }
  4297. /* 结束面试按钮样式 */
  4298. .end-interview-container {
  4299. position: absolute;
  4300. bottom: 50px;
  4301. left: 50%;
  4302. transform: translateX(-50%);
  4303. z-index: 20;
  4304. }
  4305. .end-interview-button {
  4306. width: 120px;
  4307. height: 40px;
  4308. border-radius: 20px;
  4309. background-color: #e74c3c; /* 红色背景 */
  4310. color: #ffffff; /* 白色文字 */
  4311. font-size: 16px;
  4312. border: none;
  4313. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  4314. display: flex;
  4315. justify-content: center;
  4316. align-items: center;
  4317. cursor: pointer;
  4318. }
  4319. /* 提问轮次指示器样式 */
  4320. .question-round-indicator {
  4321. position: absolute;
  4322. top: 20px;
  4323. right: 120px; /* 位置在用户摄像头左侧 */
  4324. background-color: rgba(255, 255, 255, 0.9);
  4325. padding: 5px 10px;
  4326. border-radius: 15px;
  4327. z-index: 20;
  4328. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  4329. }
  4330. .round-text {
  4331. color: #333;
  4332. font-size: 14px;
  4333. }
  4334. </style>