interview-question.vue 173 KB

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