Browse Source

新增呼叫适应

3860082966@qq.com 1 week ago
parent
commit
156a7836f2

BIN
projects/live-app/public/mp3/call.mp3


BIN
projects/live-app/public/mp3/message.mp3


+ 58 - 9
projects/live-app/src/services/message.service.ts

@@ -7,7 +7,7 @@ import { Subject } from 'rxjs';
 import { Router } from '@angular/router';
 import { AiChatService } from './aichart.service';
 declare const AgoraRTM: any;
-import {VapInit} from '../lib/vap-player/index'
+import { VapInit } from '../lib/vap-player/index'
 
 
 @Injectable({
@@ -36,8 +36,21 @@ export class MessageService {
   giftLogMap: any = [];
   giftList: Array<any> = []; //礼物列表
   timeg: any;
-  isPlayer:boolean = false;
+  isPlayer: boolean = false;
 
+  private audioPlayer: HTMLAudioElement | null = null;
+  private loopPlayAudio(audioUrl: string, rep?:boolean) {
+    if (this.audioPlayer) {
+      this.audioPlayer.pause();
+      this.audioPlayer.currentTime = 0;
+    }
+
+    this.audioPlayer = new Audio(audioUrl);
+    this.audioPlayer.loop = rep; // 设置循环播放
+    this.audioPlayer.play().catch(error => {
+      console.error('播放音频失败:', error);
+    });
+  }
   reset() {
     this.options = { connectState: false };
     this.rtmClient = null;
@@ -117,6 +130,15 @@ export class MessageService {
         if (!is_self) {
           this.callPresence(message.text, event.publisher, event.channelName);
         }
+        //呼出播放声音
+        if (message.text == 'USERCALLINVITATION') {
+          this.loopPlayAudio('mp3/call.mp3',true);
+        } else if (message.text == 'CLOASEINVITATION') {
+          if (this.audioPlayer) {
+            this.audioPlayer.pause();
+            this.audioPlayer.currentTime = 0;
+          }
+        }
         return;
       }
       this.showMessage(event);
@@ -178,6 +200,7 @@ export class MessageService {
       return;
     }
     if (message.indexOf('USERGREETING_') > -1) {
+      this.loopPlayAudio('mp3/message.mp3')
       let arr = message.split('_');
       this.alert = await this.alertController.create({
         cssClass: 'my-custom-class',
@@ -205,6 +228,7 @@ export class MessageService {
     }
     switch (message) {
       case 'USERCALLINVITATION':
+        this.loopPlayAudio('mp3/call.mp3',true);
         await this.setConnectState(this.userId, 'CONNECTING');
         // console.log(`收到${userData?.nickname?.value ?? '未知用户'}通话邀请`);
         this.alert = await this.alertController.create({
@@ -222,6 +246,11 @@ export class MessageService {
                   'REFUSEINVITATION_' + publisher,
                   channelName
                 );
+                // 停止播放音频
+                if (this.audioPlayer) {
+                  this.audioPlayer.pause();
+                  this.audioPlayer.currentTime = 0;
+                }
               },
             },
             {
@@ -234,6 +263,11 @@ export class MessageService {
                 );
                 let rid = await this.getRoom(this.userId);
                 this.router.navigate(['live/link-room/' + rid]);
+                // 停止播放音频
+                if (this.audioPlayer) {
+                  this.audioPlayer.pause();
+                  this.audioPlayer.currentTime = 0;
+                }
               },
             },
           ],
@@ -250,16 +284,31 @@ export class MessageService {
           duration: 1500,
         });
         toast.present();
+        // 停止播放音频
+        if (this.audioPlayer) {
+          this.audioPlayer.pause();
+          this.audioPlayer.currentTime = 0;
+        }
         break;
       case 'REFUSEINVITATION_' + this.userId:
         // console.log(`${userData?.nickname?.value ?? '未知用户'}拒绝通话`);
         this.alert?.dismiss();
         this.eventSource.next(false);
+        // 停止播放音频
+        if (this.audioPlayer) {
+          this.audioPlayer.pause();
+          this.audioPlayer.currentTime = 0;
+        }
         break;
       case 'RESPONSEINVITOIN_' + this.userId:
         // console.log(`${userData?.nickname?.value ?? '未知用户'}同意通话,进入视频通话`);
         this.alert?.dismiss();
         this.eventSource.next(true);
+        // 停止播放音频
+        if (this.audioPlayer) {
+          this.audioPlayer.pause();
+          this.audioPlayer.currentTime = 0;
+        }
         break;
     }
   }
@@ -276,8 +325,8 @@ export class MessageService {
   //     }
   //   }, 5000);
   // }
-  
-  playGift(giftModule:any){
+
+  playGift(giftModule: any) {
     console.log(giftModule);
     this.isPlayer = true
     let dom = document.getElementById('vap-gift');
@@ -303,13 +352,13 @@ export class MessageService {
       vapPlayer = null;
       console.log('播放结束');
       this.giftLogMap.pop();
-      if(this.giftLogMap.length > 0){
+      if (this.giftLogMap.length > 0) {
         this.playGift(this.giftLogMap.slice(-1)[0]);
-      }else{
+      } else {
         this.isPlayer = false
       }
     });
-    vapPlayer.on('playering', function() { console.log('playering'); })
+    vapPlayer.on('playering', function () { console.log('playering'); })
   }
 
   async getRoom(uid: string): Promise<string | undefined> {
@@ -382,8 +431,8 @@ export class MessageService {
     });
   }
   /* 订阅消息 */
-  subscribeMessage(channelName: string, param?: any, deadline?: number):any {
-    if (this.channelNameList[channelName]) {return};
+  subscribeMessage(channelName: string, param?: any, deadline?: number): any {
+    if (this.channelNameList[channelName]) { return };
 
     return new Promise((resolve, reject) => {
       const options = {