1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| <template> <div class="audio"> <audio controls id="audioPlay_z" src="/file/notification.mp3" v-if="!isMobile"></audio> <audio controls id="audioPlay_z" src="/file/notification_m.mp3" v-else></audio> </div> </template>
<script> import { getPlatformType } from '@/utils'
export default { name: 'PlayNotifyVoice', data () { return { audioFile: '/file/notification.mp3', palyNum: 0, isPlaying: false } }, computed: { isiOS () { return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) }, isSafari () { const userAgent = navigator.userAgent return userAgent.indexOf('Safari') > -1 && userAgent.indexOf('Chrome') === -1 && userAgent.indexOf('Edge') === -1 && userAgent.indexOf('OPR') === -1 }, isMobile () { return getPlatformType() !== 'pc' } }, beforeMount () { if (this.isiOS) { document.body.addEventListener('touchstart', this.init, false) } if (this.isSafari) { document.body.addEventListener('click', this.init, false) } }, mounted () { var audioEle = document.getElementById('audioPlay_z') audioEle.addEventListener('ended', (res) => { this.isPlaying = false if ('fastSeek' in audioEle) { audioEle.fastSeek(1) audioEle.load() } else { audioEle.currentTime = 1 } this.palyNum = this.palyNum - 1 console.log('ended', this.palyNum) if (this.palyNum > 0) { this.playVoice() } }) audioEle.addEventListener('playing', (res) => { this.isPlaying = true console.log('isPlaying') }) audioEle.addEventListener('pause', (res) => { console.log('pause') }) audioEle.addEventListener('canplay', (res) => { console.log('canplay') }) audioEle.addEventListener('canplaythrough', (res) => { console.log('canplaythrough') }) audioEle.addEventListener('timeupdate', (res) => { console.log('timeupdate') }) }, methods: { init () { var audioEle = document.getElementById('audioPlay_z') let playPromise = audioEle.play() if (playPromise !== undefined) { playPromise.then(() => { audioEle.play() audioEle.pause() if ('fastSeek' in audioEle) { audioEle.fastSeek(1) } else { audioEle.currentTime = 1 } }).catch(() => { console.log('play failed init') }) } this.palyNum = 0 this.isPlaying = false document.body.removeEventListener('touchstart', this.init, false) document.body.removeEventListener('click', this.init, false) }, playNotifyVoice () { if (!!window.ActiveXObject || 'ActiveXObject' in window) { var OSPlayer = new ActiveXObject('WMPLayer.OCX') OSPlayer.url = this.audioFile OSPlayer.controls.play() } else { this.palyNum++ this.playVoice() } }, playVoice () { var audioEle = document.getElementById('audioPlay_z') console.log(this.isPlaying, audioEle) if (!this.isPlaying) { let playPromise = audioEle.play() if (playPromise !== undefined) { playPromise.then(() => { console.log('isPlaying', this.isPlaying, this.palyNum) if ('fastSeek' in audioEle) { audioEle.fastSeek(0) audioEle.load() } else { audioEle.currentTime = 0 } console.log(111) audioEle.play() }).catch(() => { console.log('play failed') }) } } } } } </script>
<style lang="less" scoped> #audioPlay_z { display: none; } </style>
|