Skip to content

Commit

Permalink
fix(add create inner): useWebAudioImplement option add
Browse files Browse the repository at this point in the history
  • Loading branch information
innocces committed Sep 24, 2021
1 parent b63567f commit 9e1254c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
23 changes: 14 additions & 9 deletions packages/hooks/src/useAudio/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ const [

##### 以下均为`initOptions`

| 参数 | 说明 | 类型 | 默认值 |
| -------------- | ----------------------------------------------------- | --------- | ------- |
| autoplay | 是否自动开始播放(若指定后面可与新的配置合并) | `boolean` | `false` |
| loop | 是否循环播放(若指定后面可与新的配置合并) | `boolean` | `false` |
| src | 音频资源的地址(若指定后面可与新的配置合并) | `string` | - |
| volume | 音量。范围 0~1(若指定后面可与新的配置合并) | `number` | `1` |
| startTime | 开始播放的位置(单位:s)(若指定后面可与新的配置合并) | `number` | `0` |
| mixWithOther | 是否与其他音频混播(若指定后面可与新的配置合并) | `boolean` | - |
| obeyMuteSwitch | 是否遵循系统静音开关(若指定后面可与新的配置合并) | `boolean` | `true` |
| 参数 | 说明 | 类型 | 默认值 |
| -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ------- |
| autoplay | 是否自动开始播放(若指定后面可与新的配置合并) | `boolean` | `false` |
| loop | 是否循环播放(若指定后面可与新的配置合并) | `boolean` | `false` |
| src | 音频资源的地址(若指定后面可与新的配置合并) | `string` | - |
| volume | 音量。范围 0~1(若指定后面可与新的配置合并) | `number` | `1` |
| startTime | 开始播放的位置(单位:s)(若指定后面可与新的配置合并) | `number` | `0` |
| mixWithOther | 是否与其他音频混播(若指定后面可与新的配置合并) | `boolean` | - |
| obeyMuteSwitch | 是否遵循系统静音开关(若指定后面可与新的配置合并) | `boolean` | `true` |
| useWebAudioImplement | 是否使用 WebAudio 作为底层音频驱动,默认关闭。对于短音频、播放频繁的音频建议开启此选项,开启后将获得更优的性能表现,对于长音频建议关闭此选项。(若指定后面可与新的配置合并) | `boolean` | `false` |

## 返回值说明

Expand Down Expand Up @@ -128,3 +129,7 @@ const [
- [audio](https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.setInnerAudioOption.html)

- [audio article 推荐](https://developers.weixin.qq.com/community/develop/article/doc/000a2c9894cb081577d8d67ef5b813)

- [useWebAudioImplement](https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.createInnerAudioContext.html#%E5%8F%82%E6%95%B0)

- [useWebAudioImplement warn](https://developers.weixin.qq.com/community/develop/doc/000806e46d80408196cc3a6085bc00?highLine=wx.setInnerAudioOption)
25 changes: 20 additions & 5 deletions packages/hooks/src/useAudio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ import {
import { useCallback, useEffect, useState } from 'react';
import useEnv from '../useEnv';

export interface ICreateInnerAudioContextOption {
useWebAudioImplement?: boolean;
}
export type ICreateInnerAudioContext = (
option?: ICreateInnerAudioContextOption,
) => InnerAudioContext;
export type IAudioContext = InnerAudioContext | undefined;

export type IAudioSource =
| getAvailableAudioSources.SuccessCallbackResult['audioSources']
| undefined;
export interface IOption extends Partial<setInnerAudioOption.Option> {
export interface IOption
extends Partial<setInnerAudioOption.Option & ICreateInnerAudioContextOption> {
autoplay: boolean;
loop: boolean;
src: string;
Expand Down Expand Up @@ -111,7 +118,13 @@ function useAudio(
if (audioContext) {
return audioContext;
}
const context = createInnerAudioContext();
const payload =
typeof createOption.useWebAudioImplement === 'undefined'
? {}
: { useWebAudioImplement: createOption.useWebAudioImplement };
const context = (
createInnerAudioContext as unknown as ICreateInnerAudioContext
)(payload);
setAudioOption({ ...(initOption || {}), ...createOption }, context);
setAudioContext(context);
return context;
Expand All @@ -132,8 +145,10 @@ function useAudio(
if (SPECIALOPTION.includes(key)) {
specialOptions[key] = value;
}
// some option need setting root
(<any>context)[key] = value;
// some option need setting root not in weapp
if (!SPECIALOPTION.includes(key) || env === ENV_TYPE.WEAPP) {
(<any>context)[key] = value;
}
});
setInnerAudioOption({
...specialOptions,
Expand All @@ -146,7 +161,7 @@ function useAudio(
}
});
},
[audioContext],
[audioContext, env],
);

const play = useCallback<IPlayAction>(
Expand Down

2 comments on commit 9e1254c

@vercel
Copy link

@vercel vercel bot commented on 9e1254c Sep 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

taro-hooks – ./

taro-hooks-innocces.vercel.app
taro-hooks-git-main-innocces.vercel.app
taro-hooks-theta.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 9e1254c Sep 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

taro-hooks-h5 – ./

taro-hooks-h5-innocces.vercel.app
taro-hooks-h5-git-main-innocces.vercel.app
taro-hooks-h5-green.vercel.app

Please # to comment.