From bd3a3057169eb727728f8a322f51fd4fb4b6cf30 Mon Sep 17 00:00:00 2001
From: 闫增涛 <1829501689@qq.com>
Date: 星期四, 11 四月 2024 22:08:32 +0800
Subject: [PATCH] 答题器听力题播放功能优化

---
 packageBookService/pages/bookServices/examination/examination.js |   42 +++++++++++++++++++++++++++++++++++-------
 1 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/packageBookService/pages/bookServices/examination/examination.js b/packageBookService/pages/bookServices/examination/examination.js
index 2c5d010..d5c4b7a 100644
--- a/packageBookService/pages/bookServices/examination/examination.js
+++ b/packageBookService/pages/bookServices/examination/examination.js
@@ -182,7 +182,6 @@
   // 鐩戝惉watch
   watch(context, variableName, callback) {
     let value = context.data[variableName]; // 鑾峰彇琚洃鍚睘鎬х殑褰撳墠鍊�
-
     // 浣跨敤 Object.defineProperty 鏂规硶鍦ㄦ暟鎹璞′笂瀹氫箟灞炴�х殑 getter 鍜� setter
     Object.defineProperty(context.data, variableName, {
       configurable: true, // 鍙厤缃�
@@ -197,6 +196,34 @@
       },
     });
   },
+
+  // 姝e垯鎵惧嚭鍚姏src
+  extractSourceSrc(htmlString) {
+    // 姝e垯琛ㄨ揪寮忓尮閰�<source>鏍囩涓殑src灞炴�у��  
+    var srcRegex = /<source\s+src="([^"]+)"/i;
+    var srcTwo = /<audio\s+src="([^"]+)"/i;
+    // 鎵ц姝e垯鍖归厤  
+    var match = srcRegex.exec(htmlString);
+    const local = srcTwo.exec(htmlString)
+    // 濡傛灉鍖归厤鎴愬姛锛岃繑鍥炵涓�涓崟鑾风粍鐨勫唴瀹癸紙src灞炴�х殑鍊硷級  
+    if (match && match[1]) {
+      return match[1];
+    } else if (local && local[1]) {
+      return local[1]
+    } else {
+      // 濡傛灉娌℃湁鍖归厤鍒帮紝杩斿洖null  
+      return null;
+    }
+  },
+
+  // 鎷垮埌鍚姏棰橀櫎audio鏍囩澶栧叾浠栧唴瀹�
+  removeVideoAndAudioTags(htmlString) {
+    // 浣跨敤姝e垯琛ㄨ揪寮忓尮閰嶅苟绉婚櫎鎵�鏈夌殑 <video> 鍜� <audio> 鏍囩
+    var cleanedHtml = htmlString.replace(/<video[^>]*>[\s\S]*?<\/video>|<audio[^>]*>[\s\S]*?<\/audio>/gi, '');
+
+    return cleanedHtml;
+  },
+
   // 鏀瑰彉loading鐘舵��
   changeLoadingState() {
     this.setData({
@@ -874,12 +901,13 @@
             );
           }
           // 鍚姏棰樹慨鏀�
-          if (questionObj.questionType == 'singleChoiceArr') {
-            questionObj.stem.stemTxt = questionObj.stem.stemTxt
-              .replace(
-                /\<audio/gi,
-                '<audio @play="play" '
-              )
+          if (questionObj.questionType == 'singleChoice') {
+            const src = this.extractSourceSrc(questionObj.stem.stemTxt)
+            if (src) {
+              questionObj.src = src
+              questionObj.stem.stemTxt = this.removeVideoAndAudioTags(questionObj.stem.stemTxt)
+              console.log('棰樺共', questionObj.stem.stemTxt);
+            }
           }
           // 鑾峰彇鍥剧墖
           if (

--
Gitblit v1.9.1