123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- import { computed, watchEffect } from 'vue'
- import { useStore } from 'vuex'
- import { useApp } from '@/app'
- import browser from './browser'
- import QJKanKan from '@/sdk/QJKanKan'
- let CLICKFIRST = false
- // 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() {
- $player.removeEventListener('click', onclick)
- $player.removeEventListener('touchstart', onclick)
- //判断是否第一次进入或者是否已点击过
- if (player.pauseFromOther || CLICKFIRST) {
- return
- }
- CLICKFIRST = true
- player.sound.play()
- }
- const $player = document.querySelector('.ui-view-layout')
- $player.addEventListener('click', onclick)
- $player.addEventListener('touchstart', onclick)
- }
- class AudioPlayer extends QJKanKan.MITT.Emiter {
- constructor(name) {
- super()
- 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')
- },
- onend: () => {
- 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
- }
- }
- paused() {
- console.log('hard-paused')
- // this.sound && this.sound.mute(true)
- this.sound && this.sound.pause()
- this.isPlay = false
- }
- 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 ready = () => {
- if (typeof parent.WeixinJSBridge !== 'undefined') {
- parent.WeixinJSBridge.invoke(
- 'getNetworkType',
- {},
- e => {
- try {
- if (!player.pauseFromOther) {
- this.play()
- }
- } catch (error) { }
- },
- false
- )
- }
- }
- this.isLock = true
- this.watchPlay = (autoplay = null) => {
- debugger
- if (this.isLock) {
- return
- }
- this._remove()
- const source = computed(() => {
- let music = store.getters['fdkk/fdkkBGM'] || store.getters['scene/musicURL']
- console.log(music, 'musicmusicmusic');
- if (music) {
- return {
- src: music,
- loop: true,
- }
- }
- return null
- })
- 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 {
- if (autoplay) {
- setTimeout(() => {
- this.play()
- }, 50);
- }
- else {
- retryPlay(this)
- }
- // ready()
- // }
- })
- .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['functions/commentary']
- })
- 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 (source.value.openByDefault) {
- this.play()
- }
- })
- .catch(err => {
- console.error('err', err)
- })
- } else {
- setTimeout(() => {
- this._remove()
- }, 50)
- }
- }
- // watchEffect(() => this.watchPlay())
- }
- }
- export function useMusicPlayer() {
- if (useMusicPlayer.player === void 0) {
- useMusicPlayer.player = new MusicPlayer()
- useApp().then(app => {
- 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: [],
- })
- }
|