123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- import { computed, watchEffect } from 'vue'
- import { useStore } from 'vuex'
- import { useApp } from '@/app'
- import browser from './browser'
- // const sounds$ = document.createElement('div')
- // sounds$.className = 'audios'
- // sounds$.style.display = 'none'
- // sounds$.style.position = 'absolute'
- // sounds$.style.zIndex = -1
- // document.body.appendChild(sounds$)
- const retryPlay = player => {
- function onclick() {
- if (player.pauseFromOther) {
- return
- }
- $player.removeEventListener('click', onclick)
- $player.removeEventListener('touchstart', onclick)
- player.sound.play()
- }
- const $player = document.querySelector('.player')
- $player.addEventListener('click', onclick)
- $player.addEventListener('touchstart', onclick)
- }
- class AudioPlayer extends KanKan.MITT.Emiter {
- constructor(name) {
- super()
- // const loadeddata = e => {
- // this.duration = this.sound.duration
- // }
- // const timeupdate = e => {}
- // this.sound = document.createElement('audio')
- // this.sound.className = `audio-${name}`
- // //this.sound.autoplay = 'autoplay'
- // this.sound.preload = true
- // this.sound.controls = true
- // this.sound.addEventListener('loadeddata', loadeddata)
- // this.sound.addEventListener('timeupdate', timeupdate)
- // this.duration = 0
- // this.isPlay = false
- // this.isPause = false
- // this.pauseFromOther = false
- // sounds$.appendChild(this.sound)
- this.isPlay = false
- this.isPause = false
- this.pauseFromOther = false
- this._create = (src, options = {}) => {
- return new Promise((resolve, reject) => {
- this._remove()
- setTimeout(() => {
- this.sound = new Howl({
- preload: true,
- src: [src],
- loop: options.loop || false,
- html5: options.html5 || false,
- onloaderror(id, err) {
- if (!options.html5) {
- reject(err)
- }
- },
- onplayerror: function (err) {
- if (!options.html5) {
- reject(err)
- }
- },
- onload() {
- if (!options.html5) {
- resolve()
- }
- },
- onplay: () => {
- this.isPlay = true
- this.isPause = false
- this.emit('play')
- },
- onpause: () => {
- this.isPause = true
- this.isPlay = false
- this.emit('pause')
- },
- onstop: () => {
- this.isPause = false
- this.isPlay = false
- this.emit('pause')
- },
- })
- if (options.html5) {
- resolve()
- }
- }, 50)
- })
- }
- this._remove = () => {
- if (this.sound) {
- if (this.isPlay) {
- this.sound.stop()
- }
- this.sound.unload()
- this.sound = null
- }
- }
- }
- play() {
- this.sound.play()
-
- }
- pause(fromOther) {
- if (!this.isPlay) {
- return
- }
- this.sound.pause()
-
- if (fromOther) {
- this.pauseFromOther = true
- }
- }
- stop() {
- this.sound.stop()
- }
- resume() {
- if (this.pauseFromOther) {
- this.pauseFromOther = false
- if (!this.isPlay) {
- this.play()
- }
- }
- }
- source(src, options = {}) {
- this._init(src, options)
- }
- }
- class MusicPlayer extends AudioPlayer {
- constructor() {
- super('music')
- const store = useStore()
- // const source = computed(() => {
- // let music = store.getters['scene/musicURL']
- // if (music) {
- // return {
- // src: music,
- // loop: true,
- // }
- // }
- // return null
- // })
- const source = ''
- const ready = () => {
- if (typeof parent.WeixinJSBridge !== 'undefined') {
- parent.WeixinJSBridge.invoke(
- 'getNetworkType',
- {},
- e => {
- this.play()
- },
- false
- )
- }
- }
- this.isLock = true
- this.watchPlay = () => {
- if (this.isLock) {
- return
- }
- if (source.value && source.value.src) {
- this._create(source.value.src, {
- loop: source.value.loop,
- html5: false,
- })
- .then(() => {
- if (browser.detectWeixin()) {
- if (typeof parent.WeixinJSBridge !== 'undefined') {
- ready()
- } else {
- parent.document.addEventListener('WeixinJSBridgeReady', ready)
- }
- } else {
- retryPlay(this)
- }
- })
- .catch(err => {
- console.error('err', err)
- })
- } else {
- setTimeout(() => {
- this._remove()
- }, 50)
- }
- }
- watchEffect(() => this.watchPlay())
- }
- }
- class SoundPlayer extends AudioPlayer {
- constructor() {
- super('sound')
- const store = useStore()
- const source = computed(() => {
- return store.getters.sound
- })
- watchEffect(() => {
- if (this.sound.src) {
- this.sound.pause()
- }
- if (source.value && source.value.src) {
- this.sound.src = source.value.src
- this.sound.loop = source.value.loop || false
- this.play()
- } else {
- this.sound.removeAttribute('src')
- this.sound.removeAttribute('loop')
- this.pause()
- }
- })
- }
- }
- export function useMusicPlayer() {
- if (useMusicPlayer.player === void 0) {
- useMusicPlayer.player = new MusicPlayer()
- useApp().then(app => {
- app.Scene.on('loaded', () => {
- useMusicPlayer.player.isLock = false
- useMusicPlayer.player.watchPlay()
- })
- })
- }
- return useMusicPlayer.player
- }
- export function useSoundPlayer() {
- if (useSoundPlayer.player === void 0) {
- useSoundPlayer.player = new SoundPlayer()
- }
- return useSoundPlayer.player
- }
- function wxConfig() {
- wx.config({
- // 配置信息, 即使不正确也能使用 wx.ready
- debug: false,
- appId: '',
- timestamp: 1,
- nonceStr: '',
- signature: '',
- jsApiList: [],
- })
- }
|