Browse Source

bug fix music autoplay

zjxpcyc 6 years ago
parent
commit
13e38f608c
2 changed files with 15 additions and 1 deletions
  1. 14
    0
      src/components/music.vue
  2. 1
    1
      src/router.js

+ 14
- 0
src/components/music.vue View File

14
     return {
14
     return {
15
       playing: 'off',
15
       playing: 'off',
16
       audio: null,
16
       audio: null,
17
+      trigger: 'ontouchend' in window.document ? 'touchstart' : 'click',
17
 
18
 
18
       icons: {
19
       icons: {
19
         '1': {
20
         '1': {
30
   watch: {
31
   watch: {
31
     url: function(n) {
32
     url: function(n) {
32
       if (this.playing === 'off' && n) {
33
       if (this.playing === 'off' && n) {
34
+        // 自动播放
33
         window.setTimeout(this.play, 1000)
35
         window.setTimeout(this.play, 1000)
36
+
37
+        // 手动触发音乐播放
38
+        // IOS 不允许自动播放
39
+        window.document.addEventListener(this.trigger, this.autoplay, false)
34
       }
40
       }
35
     }
41
     }
36
   },
42
   },
37
   methods: {
43
   methods: {
44
+    autoplay() {
45
+      document.removeEventListener(this.trigger, this.autoplay, false)
46
+
47
+      if (this.playing === 'off') {
48
+        this.play()
49
+      }
50
+    },
51
+
38
     play() {
52
     play() {
39
       if (this.url) {
53
       if (this.url) {
40
         if (!this.audio) {
54
         if (!this.audio) {

+ 1
- 1
src/router.js View File

1
 import Vue from 'vue'
1
 import Vue from 'vue'
2
 import Router from 'vue-router'
2
 import Router from 'vue-router'
3
-import store from './store/index.js'
3
+// import store from './store/index.js'
4
 
4
 
5
 Vue.use(Router)
5
 Vue.use(Router)
6
 
6