Browse Source

fix:微信浏览器无法自动播放问题

aamin 1 year ago
parent
commit
133f1c2449

+ 0 - 9
components.d.ts

@@ -7,18 +7,9 @@ export {}
 
 declare module 'vue' {
   export interface GlobalComponents {
-    Audio3: typeof import('./src/components/audio3.vue')['default']
     AudioBox: typeof import('./src/components/AudioBox.vue')['default']
-    AudioPlayer: typeof import('./src/components/audioPlayer.vue')['default']
-    AudioPlayerNew: typeof import('./src/components/audioPlayerNew.vue')['default']
-    AudioPlayerNews: typeof import('./src/components/audioPlayerNews.vue')['default']
-    HelloWorld: typeof import('./src/components/HelloWorld.vue')['default']
-    MpHtml: typeof import('./src/components/mp-html/mp-html.vue')['default']
-    Node: typeof import('./src/components/mp-html/node/node.vue')['default']
     Progress: typeof import('./src/components/progress.vue')['default']
     RouterLink: typeof import('vue-router')['RouterLink']
     RouterView: typeof import('vue-router')['RouterView']
-    VanCollapse: typeof import('vant/es')['Collapse']
-    VanCollapseItem: typeof import('vant/es')['CollapseItem']
   }
 }

+ 12 - 1
package-lock.json

@@ -14,7 +14,8 @@
         "vant": "^4.7.1",
         "vite-plugin-style-import": "^2.0.0",
         "vue": "^3.3.4",
-        "vue-router": "^4.2.4"
+        "vue-router": "^4.2.4",
+        "weixin-js-sdk": "^1.6.0"
       },
       "devDependencies": {
         "@types/node": "^20.8.4",
@@ -1990,6 +1991,11 @@
       "integrity": "sha512-kyDivFZ7ZM0BVOUteVbDFhlRt7Ah/CSPwJdi8hBpkK7QLumUqdLtVfm/PX/hkcnrvr0i77fO5+TjZ94Pe+C9iw==",
       "dev": true
     },
+    "node_modules/weixin-js-sdk": {
+      "version": "1.6.0",
+      "resolved": "https://registry.npmmirror.com/weixin-js-sdk/-/weixin-js-sdk-1.6.0.tgz",
+      "integrity": "sha512-3IYQH7aalJGFJrwdT3epvTdR1MboMiH7vIZ5BRL2eYOJ12BNah7csoMkmSZzkq1+l92sSq29XdTCVjCJoK2sBQ=="
+    },
     "node_modules/yallist": {
       "version": "4.0.0",
       "resolved": "https://registry.npmmirror.com/yallist/-/yallist-4.0.0.tgz",
@@ -3395,6 +3401,11 @@
       "integrity": "sha512-kyDivFZ7ZM0BVOUteVbDFhlRt7Ah/CSPwJdi8hBpkK7QLumUqdLtVfm/PX/hkcnrvr0i77fO5+TjZ94Pe+C9iw==",
       "dev": true
     },
+    "weixin-js-sdk": {
+      "version": "1.6.0",
+      "resolved": "https://registry.npmmirror.com/weixin-js-sdk/-/weixin-js-sdk-1.6.0.tgz",
+      "integrity": "sha512-3IYQH7aalJGFJrwdT3epvTdR1MboMiH7vIZ5BRL2eYOJ12BNah7csoMkmSZzkq1+l92sSq29XdTCVjCJoK2sBQ=="
+    },
     "yallist": {
       "version": "4.0.0",
       "resolved": "https://registry.npmmirror.com/yallist/-/yallist-4.0.0.tgz",

+ 2 - 1
package.json

@@ -15,7 +15,8 @@
     "vant": "^4.7.1",
     "vite-plugin-style-import": "^2.0.0",
     "vue": "^3.3.4",
-    "vue-router": "^4.2.4"
+    "vue-router": "^4.2.4",
+    "weixin-js-sdk": "^1.6.0"
   },
   "devDependencies": {
     "@types/node": "^20.8.4",

BIN
src/assets/img/contentBg.png


+ 66 - 4
src/components/AudioBox.vue

@@ -1,6 +1,8 @@
 <script setup lang="ts">
 import { stat } from "fs";
 import { kill } from "process";
+import { showToast } from "vant";
+import wx from "weixin-js-sdk";
 
 // 播放状态
 const state = ref(false);
@@ -73,7 +75,6 @@ const updateProgress2 = (moveX: number) => {
 
 // 更新进度条(播放中)
 const updateProgress = (e: any) => {
-  console.log(e)
   e.target.autoplay = true
   const progressBar = document.getElementById("progressBar");
   var value = e.target.currentTime / e.target.duration;
@@ -89,9 +90,48 @@ const updateProgress = (e: any) => {
 
 const durationRef = ref(null);
 
+const formatePlayerDuration = (node: any) => {
+  node.onloadedmetadata = (e: any) => {
+    const audio = e.target;
+    const audioDuration = audio.duration;
+    if (audioDuration === Infinity) {
+      audio.currentTime = 1e101;
+      audio.ontimeupdate = () => {
+        audio.ontimeupdate = () => {
+          return;
+        };
+        // 不重新设置currtTime,会直接触发audio的ended事件,因为之前将currentTime设置成了一个比音频时长还大的值。所以要将currentTime重置为初始状态。
+        // 注: 这里有一个问题,直接设置为0 是不起作用的。需要重新设置一下audio.currentTime = 1e101;然后再设置为
+        audio.currentTime = 1e101;
+        audio.currentTime = 0;
+      };
+    }
+  };
+};
+
+// 计算音频的时长
+const countAudioTime = async () => {
+  var audio = document.getElementById("music1");
+
+  while (isNaN(audio!.duration) || audio!.duration === Infinity) {
+    // 延迟一会 不然网页都卡死
+    await new Promise((resolve) => setTimeout(resolve, 200));
+    // 设置随机播放时间,模拟调进度条
+    // audio!.currentTime = 10000000 * Math.random();
+    durationRef.value = timeToMinute(audio!.duration);
+  }
+  // showToast(audio!.duration);
+  // console.log("音频的总时长:", audio.duration);
+};
+
 const getDuration = () => {
   var audio = document.getElementById("music1");
-  durationRef.value = timeToMinute(audio!.duration);
+  countAudioTime();
+  // formatePlayerDuration(audio)
+  setTimeout(() => {
+    durationRef.value = timeToMinute(audio!.duration);
+    // showToast(isNaN(audio!.duration), audio!.duration);
+  }, 100);
 };
 const timeToMinute = (times: any) => {
   var t = "";
@@ -112,7 +152,6 @@ const timeToMinute = (times: any) => {
 };
 
 const clear = () => {
-  console.log("切换");
   const progressBar = document.getElementById("progressBar");
   progressBar!.style.width = 0 + "px";
   var audio = document.getElementById("music1");
@@ -125,8 +164,30 @@ defineExpose({
   clear,
 });
 
+const autoPlayFn = () => {
+  const IS_IOS = !/(Android)/i.test(navigator.userAgent); //ios终端
+  if (IS_IOS) {
+    wx.config({
+      // 配置信息, 即使不正确也能使用 wx.ready
+      debug: false,
+      appId: "",
+      timestamp: 1,
+      nonceStr: "",
+      signature: "",
+      jsApiList: [],
+    });
+    wx.ready(function () {
+      document.getElementById("music1")!.load();
+      document.getElementById("music1")!.play();
+      state.value = true;
+    });
+  }
+};
+
 onMounted(() => {
+  getDuration();
   clear();
+  autoPlayFn()
 });
 </script>
 
@@ -137,8 +198,9 @@ onMounted(() => {
     @timeupdate="updateProgress"
     controls
     ref="audioRef"
-    style="display: none"
+    style="display:none"
     @canplay="getDuration"
+    preload="meta"
   ></audio>
   <div class="audio-box">
     <img

+ 1 - 0
src/view/audioDetail/index.vue

@@ -118,6 +118,7 @@ onMounted(() => {
         <!-- <audio :src="currentP.audioUrl"></audio> -->
         <div class="control-top">
           <div class="top-audio">
+            <!-- <audio id="audiom" :src="currentP.audioUrl" preload="meta"></audio> -->
             <AudioBox :audioUrl="currentP.audioUrl" ref="audiobox" />
             <!-- <audioPlayer :audioUrl="currentP.audioUrl" /> -->
           </div>

+ 33 - 5
src/view/home/index.vue

@@ -7,12 +7,40 @@ const getAssetURL = (image: string) => {
 
 const router = useRouter();
 const goDetail = (id: string) => {
-  router.push({
-    path: "/exhibitionList",
-    query: {
-      id: id,
+  // // 初始化Aduio
+  // var audio = new Audio(
+  //   "https://houseoss.4dkankan.com/project/yueYang/dongtingchangge/part1/001-1.mp3"
+  // );
+  // audio.muted = false;
+  // var playPromise = audio.play();
+  // // audio.muted = true;
+  // if (playPromise) {
+  //   playPromise
+  //     .then(() => {
+  //       // 音频加载成功
+  //       // 音频的播放需要耗时
+  //       setTimeout(() => {
+  //         // 后续操作
+  //         audio.pause();
+  //         console.log("成功");
+  //       }, 50); // audio.duration 为音频的时长单位为秒
+  //     })
+  //     .catch((e) => {
+  //       // 音频加载失败
+  //       audio.pause();
+  //       console.log("失败");
+  //     });
+  // }
+
+  router.push(
+    {
+      path: "/exhibitionList",
+      query: {
+        id: id,
+      },
     },
-  });
+    5000
+  );
 };
 onMounted(() => {
   //console.log(dataList);