Polyv Help Center

Help Center

Connect Mic Feature

Updated: 2024-10-10 17:27:10

The connectMic module provides integration for the connect mic feature. See below for detailed usage.

1. Setting Up Connect Mic and Connect Mic Info

1.1 Setting Up Connect Mic

The viewing page SDK does not enable the connect mic feature by default. If the connect mic feature is needed, developers must manually set it up. After setup, the APIs of the connect mic module can be called.

API Method: setupConnectMic(pageInstance: ConnectMicPageInstance): Promise<ConnectMicResult>

Parameter Description:

  • pageInstance: Page or component instance, type ConnectMicPageInstance, required

Return Value Description: Setup result, type Promise<ConnectMicResult>

Example:

// 在小程序页面或组件 attached 中设置连麦并传入页面或组件实例
Component({
  lifetimes: {
    async attached() {
      // 设置连麦功能
      const result = await watchCore.connectMic.setupConnectMic(this);
      if (result.success) {
        console.log('设置成功');
      } else {
        console.log('设置失败', result.failReason);
      }
    },
  },
});

1.2 Whether Connect Mic is Supported

Used to determine if the current environment supports the connect mic feature.

API Method: supportConnectMic(): SupportResult

Return Value Description: Type SupportResult

Example:

const result = watchCore.connectMic.supportConnectMic();
console.log('是否支持连麦:', result.support ? '支持' : '不支持');

1.3 Getting Connect Mic Info

The status and data of the connect mic module are stored via connectMicInfo. Developers can obtain connect mic info using the getConnectMicInfo method.

PS: Listen for connect mic event changes via the ConnectMicEvents.ConnectMicInfoChange event.

API Method: getConnectMicInfo(): ConnectMicStoreInfo

Return Value Description: Connect mic info, type ConnectMicStoreInfo, detailed type description as follows

Property Description Type
supportConnectMic Whether the current environment supports connect mic boolean
openMicStatus Connect mic status, enabled or disabled boolean
connectMicType Connect mic type ConnectMicType
connectMicStatus User connect mic status ConnectMicStatus
showJoinQueueNumberEnabled Connect mic sort display toggle boolean
currentMicIndex Connect mic order index (-1 means not in queue) number
currentIsSpeaker Whether the current user is the main speaker boolean
localIsVideoMuted Whether the local stream has the camera disabled boolean
localIsAudioMuted Whether the local stream has the microphone disabled boolean

Example:

const info = watchCore.connectMic.getConnectMicInfo();
console.log('当前是否支持连麦功能', data.supportConnectMic);
console.log('当前是否开启连麦', data.openMicStatus);

1.4 Checking if Connect Mic Status is Currently Connected

Use isConnectMicing to check if the user or the passed status is currently connected (ConnectMicStatus.Publishing or ConnectMicStatus.Connected).

API Method: isConnectMicing(status?: ConnectMicStatus): boolean

Parameter Description:

  • status: Connect mic status, uses current status if not passed, type ConnectMicStatus, optional

Return Value Description: Whether currently connected

Example:

const res = watchCore.connectMic.isConnectMicing();
if (res) {
  console.log('用户连麦中');
} else {
  console.log('用户没有连麦');
}

2. Audience Member Going on Mic

2.1 Audience Member Applying to Connect Mic

When the lecturer/host enables the connect mic feature, users can apply to connect. Developers can call the applyConnectMic method to apply, and can call cancelApplyConnectMic to cancel the application.

PS: Listen for the connect mic application approval via the ConnectMicEvents.AllowConnectMicApply event.

API Method: applyConnectMic(): Promise<ConnectMicResult>

Return Value Description: Type Promise<ConnectMicResult>

Example:

import { ConnectMicError, ConnectMicEvents } from '@polyv/live-watch-miniprogram-sdk';

// 申请连麦
async function applyConnectMic() {
  const result = await watchCore.connectMic.applyConnectMic();
  if (result.success) {
    toast.success('连麦申请成功!请等待主播同意');
    const info = watchCore.connectMic.getConnectMicInfo();
    console.log('当前连麦状态:', info.connectMicStatus); // ConnectMicStatus.Applying
    return;
  }

  if (result.failReason === ConnectMicError.GetDevicePermissionFail) {
    toast.error('连麦申请失败!未获取设备权限');
  }
}

watchCore.connectMic.eventEmitter.on(ConnectMicEvents.AllowConnectMicApply, () => {
  toast.success('讲师已通过你的连麦申请');
});

2.2 Canceling Connect Mic Application

After an audience member applies to connect, call cancelApplyConnectMic to cancel the application.

API Method: cancelApplyConnectMic(): void

Example:

watchCore.connectMic.cancelApplyConnectMic();

const info = watchCore.connectMic.getConnectMicInfo();
console.log('当前连麦状态:', info.connectMicStatus); // ConnectMicStatus.NotConnect

2.3 Starting Local Stream Push

After the lecturer approves the connect mic application, the connect mic module triggers the ConnectMicEvents.ConnectMicDataChange event callback with a new connect mic data list. In this list, there will be a connect mic object where isLocal is true. After obtaining the new list, create a connect mic player, and in the attached lifecycle of the connect mic player component, call the startStream method to push the stream. Refer to the <connect-mic-player> component in the demo for details.

API Method: startStream(): void

Example:

Component({
  lifetimes: {
    attached() {
      // 数据为本地流,调用方法推流
      if (this.data.isLocal) {
        watchCore.connectMic.startStream();
      }
    }
  },
});

2.4 Ending Connect Mic

After the lecturer approves the audience member's connect mic application and the connection is successful, use endConnectMic to manually end the audience member's connection.

PS: Listen for successful leave via the ConnectMicEvents.LeaveConnectMicSuccess event.

API Method: endConnectMic(): Promise<void>

Example:

// 结束连麦
watchCore.connectMic.endConnectMic();

watchCore.connectMic.eventEmitter(ConnectMicEvents.LeaveConnectMicSuccess, () => {
  toast.success('结束连麦成功');
  const info = watchCore.connectMic.getConnectMicInfo();
  console.log('当前连麦状态:', info.connectMicStatus); // ConnectMicStatus.NotConnect
});

3. Camera Settings

3.1 Enabling Local Camera

Enable the local camera using the enabledVideo method. Listen for local camera toggle events via the ConnectMicEvents.LocalVideoMuteChange event.

API Method: enabledVideo(): void

Example:

// 开启本地摄像头
watchCore.connectMic.enabledVideo();

3.2 Disabling Local Camera

Disable the local camera using the disabledVideo method. Listen for local camera toggle events via the ConnectMicEvents.LocalVideoMuteChange event.

API Method: disabledVideo(): void

Example:

// 关闭本地摄像头
watchCore.connectMic.disabledVideo();

3.3 Switching Front/Rear Camera

API Method: switchCamera(): void

4. Microphone Settings

4.1 Enabling Local Microphone

Enable the local microphone using the enabledAudio method. Listen for local microphone toggle events via the ConnectMicEvents.LocalAudioMuteChange event.

API Method: enabledAudio(): void

Example:

// 开启本地麦克风
watchCore.connectMic.enabledAudio();

4.2 Disabling Local Microphone

Disable the local microphone using the disabledAudio method. Listen for local microphone toggle events via the ConnectMicEvents.LocalAudioMuteChange event.

API Method: disabledAudio(): void

Example:

// 关闭本地麦克风
watchCore.connectMic.disabledAudio();

5. Other

5.1 User Connect Mic Status

Enum: ConnectMicStatus

Constant Enum Member Description
'notConnect' ConnectMicStatus.NotConnect Not connected
'applying' ConnectMicStatus.Applying Applying to connect
'publishing' ConnectMicStatus.Publishing Streaming
'connected' ConnectMicStatus.Connected Connected
'error' ConnectMicStatus.Error Connect mic error

5.2 Connect Mic Type

Used to distinguish whether the lecturer has enabled video or audio connect mic.

Enum: ConnectMicType

Constant Enum Member Description
'video' ConnectMicType.Video Video connect mic
'audio' ConnectMicType.Audio Audio connect mic
联系客服,在线咨询