Skip to content

Commit

Permalink
fix: 修复 usePlayAudio 调用位置错误
Browse files Browse the repository at this point in the history
  • Loading branch information
lengyibai committed Jun 27, 2024
1 parent 9c3ce26 commit 28e5982
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { HeroDetailStore } from "@/store";
import { _AudioPlayer } from "@/utils/tool";
import { usePlayAudio } from "@/hooks";

const { playAudio } = usePlayAudio();

/** 音频播放器 */
const audioPlayer = new _AudioPlayer({
volume: 0.25,
Expand All @@ -28,6 +26,8 @@ const { show, show_progress, show_close, hero_data } = ExposeData;
const useHeroDetail = () => {
const $heroDetailStore = HeroDetailStore();

const { playAudio } = usePlayAudio();

const ExposeMethods = {
/** @description 显示弹窗 */
async openHeroDetail(id?: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
import { reactive } from "vue";

import { usePlayAudio } from "@/hooks";
import { dayjs } from "@/utils/tool";

const { playAudio } = usePlayAudio();

const ExposeData = {
/** 消息队列 */
messages: reactive<Global.Message.Info[]>([]),
};
const { messages } = ExposeData;

export const useMessage = () => {
const ExposeMethods = {
openMsg(text = "未设置提示", type: Global.Message.Status = "info") {
/** 获取文字长度 */
const text_length = text.split("").length / 3;
/** 通过文字长度,设置显示时长 */
const time = text_length > 3 ? text_length : text_length + 1;

//延迟提醒,避免与点击操作同时播放
const msgName: Record<string, Partial<keyof Global.Audio.Key>> = {
info: "n74s",
warning: "p6q3",
error: "vw31",
};

//播放指定名称的音效
playAudio(msgName[type]);

//创建消息内容
const msgText: Global.Message.Info = {
id: dayjs().valueOf(),
text,
type,
duration: time,
};

//将消息内容添加进消息队列
messages.unshift(msgText);
setTimeout(() => {
messages.pop();
}, time * 1000);
},
};

return {
...ExposeData,
...ExposeMethods,
};
};
import { reactive } from "vue";

import { usePlayAudio } from "@/hooks";
import { dayjs } from "@/utils/tool";

const ExposeData = {
/** 消息队列 */
messages: reactive<Global.Message.Info[]>([]),
};
const { messages } = ExposeData;

export const useMessage = () => {
const { playAudio } = usePlayAudio();

const ExposeMethods = {
openMsg(text = "未设置提示", type: Global.Message.Status = "info") {
/** 获取文字长度 */
const text_length = text.split("").length / 3;
/** 通过文字长度,设置显示时长 */
const time = text_length > 3 ? text_length : text_length + 1;

//延迟提醒,避免与点击操作同时播放
const msgName: Record<string, Partial<keyof Global.Audio.Key>> = {
info: "n74s",
warning: "p6q3",
error: "vw31",
};

//播放指定名称的音效
playAudio(msgName[type]);

//创建消息内容
const msgText: Global.Message.Info = {
id: dayjs().valueOf(),
text,
type,
duration: time,
};

//将消息内容添加进消息队列
messages.unshift(msgText);
setTimeout(() => {
messages.pop();
}, time * 1000);
},
};

return {
...ExposeData,
...ExposeMethods,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { KnapsackStore } from "@/store/modules/knapsack";
import { _isArray } from "@/utils/tool";
import { usePlayAudio } from "@/hooks";

const { playAudio } = usePlayAudio();

/** 关闭弹窗函数 */
let onClose: (() => void) | undefined;

Expand All @@ -25,6 +23,8 @@ const { show, show_bg, prop_list } = ExposeData;
const useObtain = () => {
const $knapsackStore = KnapsackStore();

const { playAudio } = usePlayAudio();

const ExposeMethods = {
/** @description 显示弹窗
* @param article 物品信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { SCENE_TIP } from "@/config/modules/scene-tip";
import { SettingStore } from "@/store/modules/setting";
import { usePlayAudio } from "@/hooks/modules/usePlayAudio";

const { playAudio } = usePlayAudio();

/** 是否已开启队列状态 */
let queue = false;
/** 用于触发下一个tip */
Expand Down Expand Up @@ -45,6 +43,8 @@ const { is_once, btn_text, content, disabled, show_tip, show_mask, align, need_m
const useTip = () => {
const $settingStore = SettingStore();

const { playAudio } = usePlayAudio();

/** @description 设置是否显示tip */
const setShowTip = async (v: boolean) => {
//处理显示和隐藏蒙版和tip的入场顺序
Expand Down
230 changes: 115 additions & 115 deletions src/hooks/modules/usePlayAudio.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,115 @@
import { _getAudioLink } from "@/utils/concise";
import { _debounce } from "@/utils/tool";

/** 音量 */
let volume = 0.5;
/** 启用音效 */
let status = true;
/** 音效类型 */
const sound_type: Global.Audio.Key = {
gz76: "atlas",
bq69: "activity",
kj62: "btn",
p60v: "back",
cy87: "cancel",
ba09: "close",
e6b4: "confirm_dialog",
pj83: "confirm",
h2w0: "default",
kh79: "delete",
u4c5: "detail",
h7t9: "epigraph",
ph23: "equip",
vw31: "error",
d5e2: "fold",
iv51: "hero",
p53r: "into",
al41: "key",
e84n: "login",
n74s: "message",
le78: "mode",
za86: "num",
v6p0: "show",
gz43: "skin",
n4r4: "tab",
rt25: "tip",
p6q3: "warning",
o7r6: "raffle",
jo36: "raffle_obtain",
fz02: "knapsack",
h3w0: "store",
bg51: "receipt",
o3l2: "shop",
wm14: "activate",
jy55: "yibao",
hy43: "back_small",
pk92: "tv",
//au63: "",
//gk90: "",
//e90x: "",
//kj35: "",
//zm31: "",
//dh38: "",
};

/**
* @description: 播放音效
* @param name 音效随机标识
*/
const play = (name: keyof Global.Audio.Key) => {
const audio = new Audio();
audio.src = _getAudioLink(sound_type[name]);
audio.volume = volume;
audio.play().catch(() => {});
};

/** @description 部分场景可能会导致音效累积播放,需要进行防抖处理 */
const debouncePlayAudio = _debounce((name: keyof Global.Audio.Key) => {
play(name);
}, 50);

/** @description 播放音效 */
const usePlayAudio = () => {
const ExposeMethods = {
/**
* @description: 调用播放
* @param name 音效名
*/
playAudio(name: keyof Global.Audio.Key = "h2w0") {
if (!status) return;

/**
* fz02:背包页面在保存的时候会出现栈溢出,导致音量频繁触发
* n4r4:悬浮音效防抖处理
* n74s、p6q3、vw31 消息提醒防抖处理
* za86:滑动音效防抖处理
*/
if (["fz02", "n4r4", "n74s", "p6q3", "vw31", "za86"].includes(name)) {
debouncePlayAudio(name);
} else {
play(name);
}
},

/**
* @description: 音量调节
* @param v 音量:1-100
*/
setAudioVolume(v: number) {
volume = (v / 100) * 0.75;
},

/** @description 设置音效功能开关
* @param v 开关状态
*/
setAudioSwitch(v: boolean) {
status = v;
},
};

return {
...ExposeMethods,
};
};

export { usePlayAudio };
import { _getAudioLink } from "@/utils/concise";
import { _debounce } from "@/utils/tool";

/** 音量 */
let volume = 0.5;
/** 启用音效 */
let status = true;
/** 音效类型 */
const sound_type: Global.Audio.Key = {
gz76: "atlas",
bq69: "activity",
kj62: "btn",
p60v: "back",
cy87: "cancel",
ba09: "close",
e6b4: "confirm_dialog",
pj83: "confirm",
h2w0: "default",
kh79: "delete",
u4c5: "detail",
h7t9: "epigraph",
ph23: "equip",
vw31: "error",
d5e2: "fold",
iv51: "hero",
p53r: "into",
al41: "key",
e84n: "login",
n74s: "message",
le78: "mode",
za86: "num",
v6p0: "show",
gz43: "skin",
n4r4: "tab",
rt25: "tip",
p6q3: "warning",
o7r6: "raffle",
jo36: "raffle_obtain",
fz02: "knapsack",
h3w0: "store",
bg51: "receipt",
o3l2: "shop",
wm14: "activate",
jy55: "yibao",
hy43: "back_small",
pk92: "tv",
//au63: "",
//gk90: "",
//e90x: "",
//kj35: "",
//zm31: "",
//dh38: "",
};

/**
* @description: 播放音效
* @param name 音效随机标识
*/
const play = (name: keyof Global.Audio.Key) => {
const audio = new Audio();
audio.src = _getAudioLink(sound_type[name]);
audio.volume = volume;
audio.play().catch(() => {});
};

/** @description 部分场景可能会导致音效累积播放,需要进行防抖处理 */
const debouncePlayAudio = _debounce((name: keyof Global.Audio.Key) => {
play(name);
}, 50);

/** @description 播放音效 */
const usePlayAudio = () => {
const ExposeMethods = {
/**
* @description: 调用播放
* @param name 音效名
*/
playAudio(name: keyof Global.Audio.Key = "h2w0") {
if (!status) return;

/**
* fz02:背包页面在保存的时候会出现栈溢出,导致音量频繁触发
* n4r4:悬浮音效防抖处理
* n74s、p6q3、vw31 消息提醒防抖处理
* za86:滑动音效防抖处理
*/
if (["fz02", "n4r4", "n74s", "p6q3", "vw31", "za86"].includes(name)) {
debouncePlayAudio(name);
} else {
play(name);
}
},

/**
* @description: 音量调节
* @param v 音量:1-100
*/
setAudioVolume(v: number) {
volume = (v / 100) * 0.75;
},

/** @description 设置音效功能开关
* @param v 开关状态
*/
setAudioSwitch(v: boolean) {
status = v;
},
};

return {
...ExposeMethods,
};
};

export { usePlayAudio };

0 comments on commit 28e5982

Please sign in to comment.