Bläddra i källkod

修复有些歌词文件乱码问题

onecold 1 år sedan
förälder
incheckning
ef122a8fe1
3 ändrade filer med 35 tillägg och 6 borttagningar
  1. 26 5
      entry/src/main/ets/view/LocalMusic.ets
  2. 8 0
      oh-package-lock.json5
  3. 1 1
      oh-package.json5

+ 26 - 5
entry/src/main/ets/view/LocalMusic.ets

@@ -56,6 +56,7 @@ import fs from '@ohos.file.fs';
 import { image } from '@kit.ImageKit';
 import { AVCastPicker, AVCastPickerState, AVCastPickerStyle, avSession } from '@kit.AVSessionKit';
 import * as chardet from '@changwei/chardet';
+import { UniversalDetector } from '@ohos/juniversalchardet';
 import { SettingPage } from '../pages/SettingPage';
 import { secondToTime } from '../common/util/CommUtils';
 import { CommonConstants2 } from '../common/util/CommonConstants2';
@@ -4409,6 +4410,8 @@ export struct LocalMusic {
   }
 
   // readCoverInfo(inputPath:string,outputPath:string){
+  //   console.info("onecold FFmpeg inputPath = "+inputPath);
+  //   console.info("onecold FFmpeg outputPath = "+outputPath);
   //   let commands = ["ffmpeg", "-i", inputPath, "-f ffmetadata",outputPath];
   //   FFmpeg.execute(commands, {
   //     logCallback: (logLevel: number, logMessage: string) => console.log(`[${logLevel}]${logMessage}`),
@@ -6318,13 +6321,15 @@ export struct LocalMusic {
           }
           try {
             // 检测文件编码
-            let detectedEncoding = chardet.detect(buf);
-            console.info("Detected encoding: " + detectedEncoding);
-
+            // let detectedEncoding = chardet.detect(buf);
+            // console.info("onecoldT Detected encoding: " + detectedEncoding);
+            let detectedEncoding = this.detect(arrayBuffer)
+            console.info("onecoldT Detected encoding: " + detectedEncoding);
             // 使用检测到的编码解码,解决中文乱码问题
             let textDecoder = new util.TextDecoder(detectedEncoding || 'utf-8');
+            console.info("onecoldT Detected textDecoder: " + textDecoder);
             let fileContent = textDecoder.decode(buf);
-            console.info(`onecold The content of file1: ${fileContent}`);
+            console.info(`onecoldT The content of file1: ${fileContent}`);
             //解密
             if (isJiaMi) {
               fileContent = StrUtil.unit8ArrayToStr(Base64Util.decodeSync(fileContent))
@@ -6376,6 +6381,21 @@ export struct LocalMusic {
 
   }
 
+  detect(data: ArrayBuffer): string {
+    //创建检测对象
+    let detector: UniversalDetector = new UniversalDetector();
+    // 传入检测数据并实时判断编码格式
+    detector.handleData(data, 0, data.byteLength);
+    // 标记检测数据 读取结束
+    detector.dataEnd();
+    // 获取检测结果
+    let detected: string = detector.getDetectedCharset();
+    // 释放资源
+    detector.reset();
+
+    return detected;
+  }
+
   //初始化悬浮歌词的自定义设置
   initPipLyricSetting() {
     this.lyricControllerXF
@@ -6439,7 +6459,8 @@ export struct LocalMusic {
         throw new Error("Buffer is empty after reading the file");
       }
 
-      let detectedEncoding = chardet.detect(buf);
+      // let detectedEncoding = chardet.detect(buf);
+      let detectedEncoding = this.detect(arrayBuffer)
       console.info("Detected encoding: " + detectedEncoding);
 
       let textDecoder = new util.TextDecoder(detectedEncoding || 'utf-8');

+ 8 - 0
oh-package-lock.json5

@@ -11,6 +11,7 @@
     "@changwei/chardet@^1.0.0": "@changwei/chardet@1.0.0",
     "@chinalike/popup@^0.0.7": "@chinalike/popup@0.0.7",
     "@keke/color-picker@^1.0.4": "@keke/color-picker@1.0.4",
+    "@ohos/juniversalchardet@^2.0.2": "@ohos/juniversalchardet@2.0.2",
     "@ohos/lottie@^2.0.19": "@ohos/lottie@2.0.19",
     "@ohos/pinyin4js@^2.0.2": "@ohos/pinyin4js@2.0.2",
     "@ohos/pulltorefresh@^2.1.1": "@ohos/pulltorefresh@2.1.1",
@@ -69,6 +70,13 @@
       "resolved": "https://repo.harmonyos.com/ohpm/@keke/color-picker/-/color-picker-1.0.4.har",
       "registryType": "ohpm"
     },
+    "@ohos/juniversalchardet@2.0.2": {
+      "name": "@ohos/juniversalchardet",
+      "version": "2.0.2",
+      "integrity": "sha512-EQiclxDnum59j75rI/t5CIOw6nyzbUB0p1hdaRPOJ+FxH7w7nT8tbga08ZwIkuFLzlAO2TtpkgDGAvkAsF3lDw==",
+      "resolved": "https://repo.harmonyos.com/ohpm/@ohos/juniversalchardet/-/juniversalchardet-2.0.2.har",
+      "registryType": "ohpm"
+    },
     "@ohos/lottie@2.0.19": {
       "name": "@ohos/lottie",
       "version": "2.0.19",

+ 1 - 1
oh-package.json5

@@ -24,7 +24,7 @@
     "@sgaolei/lrc_parser": "^1.0.0",
     "@ohos/pinyin4js": "^2.0.2",
     "@simplepeng/spider-man": "^1.0.1",
-//    "@ohos/ijkplayer": "^2.0.6"
+    "@ohos/juniversalchardet": "^2.0.2"
   },
   "dynamicDependencies": {}
 }