wangfumin 1 ماه پیش
والد
کامیت
1e26248008

+ 65 - 0
小程序入口_嵌套展示端/components/xr-ar-2dmarker/index.js

@@ -19,13 +19,74 @@ Component({
   },
   lifetimes: {
     attached() {
+      this.ensureInnerAudioOption();
     },
     detached() {
       this.stopSunRotation();
       this.stopTiger();
+      this.stopBgm();
     }
   },
   methods: {
+    ensureInnerAudioOption() {
+      if (this._innerAudioOptionInited) return;
+      this._innerAudioOptionInited = true;
+      if (!wx || typeof wx.setInnerAudioOption !== 'function') return;
+      try {
+        wx.setInnerAudioOption({ obeyMuteSwitch: false });
+      } catch (e) {
+      }
+    },
+    startBgm() {
+      this.bgmShouldPlay = true;
+      if (this.bgmCtx) {
+        try {
+          this.bgmCtx.play();
+        } catch (e) {
+        }
+        return;
+      }
+
+      this.ensureInnerAudioOption();
+      if (!wx || typeof wx.createInnerAudioContext !== 'function') return;
+
+      const bgmCtx = wx.createInnerAudioContext();
+      bgmCtx.src = 'https://ossxiaoan.4dage.com/hq-eduction-vr/bg.mp3';
+      bgmCtx.loop = true;
+      bgmCtx.volume = 0.25;
+
+      const keepAlive = () => {
+        if (!this.bgmShouldPlay) return;
+        if (!this.bgmCtx) return;
+        try {
+          bgmCtx.play();
+        } catch (e) {
+        }
+      };
+
+      bgmCtx.onStop(keepAlive);
+      bgmCtx.onPause(keepAlive);
+      bgmCtx.onEnded(keepAlive);
+
+      this.bgmCtx = bgmCtx;
+      try {
+        bgmCtx.play();
+      } catch (e) {
+      }
+    },
+    stopBgm() {
+      this.bgmShouldPlay = false;
+      if (!this.bgmCtx) return;
+      try {
+        this.bgmCtx.stop();
+      } catch (e) {
+      }
+      try {
+        this.bgmCtx.destroy();
+      } catch (e) {
+      }
+      this.bgmCtx = null;
+    },
     emitLoadState(extra) {
       const payload = Object.assign({
         arReady: !!this.data.arReady,
@@ -161,6 +222,7 @@ Component({
       if (!this.data.item1Loaded) {
         this.setData({ item1Loaded: true }, () => this.emitLoadState());
       }
+      this.startBgm();
       this.tryStartEntryAnimation();
     },
     handleItem2Loaded({ detail }) {
@@ -663,6 +725,7 @@ Component({
     },
     prepareTigerAudio() {
       if (this.tigerAudioCtx || this.tigerAudioPreparing) return;
+      this.ensureInnerAudioOption();
       const url = 'https://ossxiaoan.4dage.com/hq-eduction-vr/tiger/tiger.mp3';
 
       this.tigerAudioPrepared = false;
@@ -671,6 +734,7 @@ Component({
       this.tigerAudioPreparing = true;
 
       const createAudioCtx = (src) => {
+        this.ensureInnerAudioOption();
         const audioCtx = wx.createInnerAudioContext();
         audioCtx.src = src;
         audioCtx.loop = false;
@@ -849,6 +913,7 @@ Component({
             } else {
               const audioUrl = 'https://houseoss.4dkankan.com/project/hq-eduction-vr/public/%E5%AE%89%E5%BE%B7%E7%83%88.mp3';
               if (!this.audioCtx) {
+                this.ensureInnerAudioOption();
                 const audioCtx = wx.createInnerAudioContext();
                 audioCtx.src = audioUrl;
                 audioCtx.play();

+ 2 - 2
小程序入口_嵌套展示端/components/xr-ar-2dmarker/index.wxml

@@ -6,7 +6,7 @@
 
   <xr-env env-data="xr-frame-team-workspace-day" />
   
-  <xr-node wx:if="{{arReady}}">
+  <xr-node wx:if="{{arReady && loaded}}">
     <xr-ar-tracker mode="Marker" src="{{markerImg}}" bind:ar-tracker-state="handleARTrackerState1">
       <xr-gltf vis model="bg" position="0 0 0" scale="0.5 0.5 0.5" rotation="-90 0 0" bind:gltf-loaded="handleItem1Loaded" />
 
@@ -19,4 +19,4 @@
     <xr-light type="ambient" color="1 1 1" intensity="1" />
     <xr-light type="directional" rotation="180 0 0" color="1 1 1" intensity="2" />
   </xr-node>
-</xr-scene>
+</xr-scene>