npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hyext/link-mic

v1.0.11

Published

A link-mic lib for huya miniapp business development

Downloads

13

Readme

@hyext/link-mic

基于虎牙小程序SDK封装的连麦专用库。

安装

$ npm i @hyext/link-mic

引用

import { createLinkMicManager } from '@hyext/link-mic'

const linkMicManager = createLinkMicManager(hyExt, { extUuid: 'your_extUuid' }) // 创建一个连麦管理器

接口

createLinkMicManager(sdk, options)

创建一个基于SDK封装的连麦管理器。

参数

| Name | Type | Required | Default | Description | | ---- | ---- | ---- | ---- | ---- | | sdk | typeof hyExt | true | void | 小程序SDK - hyExt | | options | CreateLinkMicManagerOptions | true | void | 创建linkMicManager的options |

其中CreateLinkMicManagerOptions的数据结构如下:

| Name | Type | Required | Default | Description | | ---- | ---- | ---- | ---- | ---- | | debugSDK | boolean | false | void | 开启内部SDK接口debug | | debugManager | boolean | false | void | 开启 manager debug | | extUuid | string | true | void | 小程序uuid, 可在 开发者平台->概要->小程序ID 查看 |

返回值

返回一个LinkMicManager实例。

LinkMicManager

连麦管理器对象,封装了简洁易用的的API,帮助我们实现连麦功能。

接口类型

  • RoomSecret对象

| Name | Type | required | Description | | ---- | ---- | ---- | ---- | | roomKey | string | true | 房间秘钥 | | roomId | string | true | 房间id |

  • RoomInfoCore对象

| Name | Type | required | Description | | ---- | ---- | ---- | ---- | | roomStatus | number | true | 连麦房间状态,1 - 已创建, 2 - 已摧毁 | | roomMemberList | RoomMember[] | true | 成员列表 | | roomKey | string | true | 房间秘钥 | | roomId | string | true | 房间id |

其中RoomMember对象的数据结构为:

| Name | Type | required | Description | | ---- | ---- | ---- | ---- | | joinRoomLiveStreamRoomNumber | number | true | 成员人数 | | roomMemberInfo | string | true | 成员信息json字符串,数据结构为User, 需要转换一下 | | isRoomCreator | boolean | true | 是否是房间创建者 | | joinRoomRole | number | true | 成员身份,1 - 主播 ,2 - 观众| | roomMemberUnionId | string | true | unionId |

  • User对象

| Name | Type | required | Description | | ---- | ---- | ---- | ---- | | nick | string | true | 昵称 | | avatar | string | true | 头像 | | unionId | string | true | 唯一id |

  • CreateRoomEvent对象

| Name | Type | required | Description | | ---- | ---- | ---- | ---- | | roomStatus | number | true | 连麦房间状态,1 - 已创建, 2 - 已摧毁 | | creator | User | true | 用户信息 | | roomKey | string | true | 房间秘钥 | | roomId | string | true | 房间id |

  • AllRoomInfo对象

| Name | Type | required | Description | | ---- | ---- | ---- | ---- | | roomList | RoomInfoCore[] | true | 房间信息列表 |

  • RoomInfo对象

| Name | Type | required | Description | | ---- | ---- | ---- | ---- | | roomInfo | RoomInfoCore | true | 房间信息 |

实例接口

  • linkMicManager.createRoom(roomMemberMaxCount?: number): Promise<RoomSecret | null> - 创建连麦房间,返回一个promise。

其中roomMemberMaxCount为房间最大人数,默认不需要填 , 创建成功会返回RoomSecret, 失败返回null

  • linkMicManager.joinCurrAnchorRoom(): Promise<RoomInfoCore| null> - 加入当前主播连麦房间,适用于观众加入主播连麦房间的场景。

  • linkMicManager.leaveRoom(): Promise<void> - 正常离开连麦房间时调用。

  • linkMicManager.leaveRoomOnBailout(): void - 离开直播间的时候调用,一般在根节点触发unmount hook的时候去调用这个api,提前结束连麦。

  • linkMicManager.mute(): void - 麦静音。

  • linkMicManager.unmute(): void - 解除麦静音。

  • linkMicManager.mutePlayOnViewer(): Promise<void> - 停止观众端视频流的音频播放。

  • linkMicManager.unmutePlayOnViewer(): Promise<void> - 恢复观众端视频流的音频播放。

  • linkMicManager.on(eventName: string, handler: (data: any) => void, isCustomLinkMicEvent?: boolean) - 监听事件。

  • linkMicManager.emit(eventName: string, data: any, isCustomLinkMicEvent?: boolean) - 触发事件。

  • linkMicManager.queryAllJoinRoom():Promise<AllRoomInfo> - 查询用户加入的所有房间信息。

  • linkMicManager.queryRoomInfo(): Promise<RoomInfo> - 查询用户当前加入的房间信息,只能在用户加入房间后再调用。

实例事件

内置事件

LinkMicManager初始化的时候会主动监听连麦事件, LinkMicManager实例暴露了内置的连麦事件id, 可通过linkMicManager.eventIdPool获取。

内置事件如下:

  • eventIdPool.joinRoom - 成员加入事件
  • eventIdPool.leaveRoom - 成员离开事件
  • eventIdPool.closeRoom - 房间解散事件
  • eventIdPool.createRoom - 房间创建事件

joinRoomleaveRoomcloseRoom事件回调中都返回一个RoomInfoCore对象, 而createRoom返回一个CreateRoomEvent对象。

例子:

import { EventName, createLinkMicManager } from '@hyext/link-mic'

const linkMicManager = createLinkMicManager(hyExt) 
const eventIdPool = linkMicManager.eventIdPool
linkMicManager.on(eventIdPool.joinRoom, (data: RoomInfoCore) => {})
linkMicManager.on(eventIdPool.leaveRoom, (data: RoomInfoCore) => {})
linkMicManager.on(eventIdPool.closeRoom, (data: RoomInfoCore) => {})
linkMicManager.on(eventIdPool.createRoom, (data: CreateRoomEvent) => {})
自定义事件

我们在连麦的过程中自定义一些事件,做一些有趣的事情:例如播放炫酷的特效之类。

用法:

const isCustom = true // 自定义事件

// 观众端监听自定义事件
linkMicManager.on('your_custom_event', (yourCustomData) => {
  // do something
}, isCustom)

// 主播端触发自定义事件
linkMicManager.emit('your_custom_event', { foo: 'bar' }, isCustom)

注意:

  1. linkMicManager.emit的data必须是一个可序列化的数据,例如对象或者数组等。
  2. 必须在加入或创建成功房间后再调用,否则会报错,当然非定制事件不受这种约束。

createAudioManager(service)

创建一个音频流管理器。

  • audioManager.publishAudio():Promise<AudioMember> - 推上行流,返回个人的音频流信息。
  • audioManager.updateLocalAudio(audioMembers: AudioMember[]):Promise<void> - 更新本地下行音频流。
  • audioManager.stopAudio():Promise<void> - 停止音频流使用。

其中AudioMember的数据结构为:

type AudioMember = {
  nick: string,
  streamName: string,
  unionId: string, // 不需要UID
}

Demo

import { createLoggerForDebug, createSDKPolyfillWithLogger, createService, createAudioManager } from '@hyext/link-mic'

const logger = createLoggerForDebug("id", true);
const polyfilledSdk = createSDKPolyfillWithLogger(sdk, logger);
const service = createService(polyfilledSdk);

// 创建实例
const audioManager = createAudioManager(service)

实例接口

DEMO

接下来演示一下demo,嘻嘻。

创建房间demo

主播端小程序创建房间:

import { createLinkMicManager } from '@hyext/link-mic'

const linkMicManager = createLinkMicManager(hyExt) 
const eventIdPool = linkMicManager.eventIdPool

linkMicManager.on(eventIdPool.joinRoom, (data: RoomInfoCore) => { 
  // 更新成员信息
})
linkMicManager.on(eventIdPool.leaveRoom, (data: RoomInfoCore) => {
  // 更新成员信息
})
linkMicManager.on(eventIdPool.closeRoom, (data: RoomInfoCore) => {
  // 恢复原来的UI
})

await linkMicManager.createRoom()
// 改成连麦的UI

加入房间demo

观众端小程序加入房间:

import { EventName, createLinkMicManager } from '@hyext/link-mic'

const linkMicManager = createLinkMicManager(hyExt) 
const eventIdPool = linkMicManager.eventIdPool

linkMicManager.on(eventIdPool.createRoom, (data: CreateRoomEvent) => {
  // 创建房间信息,此时可以展示一个加入的按钮,让用户加入连麦,亦或者你自行调用接口加入连麦
})

linkMicManager.on(eventIdPool.joinRoom, (data: RoomInfoCore) => { 
  // 更新成员信息
})

linkMicManager.on(eventIdPool.leaveRoom, (data: RoomInfoCore) => {
  // 更新成员信息
})

linkMicManager.on(eventIdPool.closeRoom, (data: RoomInfoCore) => {
  // 恢复原来的UI
})

// 加入主播连麦
const roomInfoCore = await linkMicManager.joinCurrAnchorRoom()
// 利用roomInfoCore数据,变更为连麦的UI

// 用户点击按钮退出
await linkMicManager.leaveRoom()
// 恢复原来的UI

Demo源码展示

请戳这里