lingxixue-web/src/hooks/useAudio.ts
2026-05-09 10:34:51 +08:00

57 lines
1.2 KiB
TypeScript

import Player, { Events } from 'xgplayer'
import type { IPlayerOptions } from 'xgplayer'
import { globalStore } from '@/stores'
interface Audio_Config extends IPlayerOptions {
autoplay?: boolean
recordType?: string // 日志上报
onPlay?: (...args: any[]) => void
onEnded?: (...args: any[]) => void
onError?: (...args: any[]) => void
}
export const useAudio = (config: Audio_Config) => {
const { system } = globalStore()
const player = new Player({
id: 'x-audio',
autoplay: true,
volume: system.volume.musicVolume / 100,
keyShortcut: false,
fullscreen: {
useCssFullscreen: true, // 全屏按钮将会调用页面内全屏
},
defaultPlaybackRate: system.volume.playbackRate,
...config,
})
// player.playNext({
// url: config.url,
// });
player.play()
player.on(Events.PLAY, result => {
config.onPlay &&
config.onPlay({
result,
config,
})
})
player.on(Events.ENDED, result => {
config.onEnded &&
config.onEnded({
result,
config,
})
})
player.on(Events.ERROR, result => {
config.onError &&
config.onError({
result,
config,
})
})
return player
}