Przeglądaj źródła

实现了与其他应用同时播放的功能

onecold 8 miesięcy temu
rodzic
commit
1a3283b271

+ 3 - 1
entry/src/main/ets/pages/SettingPage.ets

@@ -26,6 +26,7 @@ import { LogCollector } from '../common/util/LogCollector';
 import { Uploader, UploadConfig } from '../common/network/Uploader';
 import { UpTokenUtil } from '../common/util/UpTokenUtil';
 import { ServerLogUtil } from '../common/util/ServerLogUtil';
+import { audio } from '@kit.AudioKit'
 
 @Preview
 // @Entry
@@ -2195,13 +2196,14 @@ export struct SettingPage {
                 .onChange((checked: boolean) => {
                   this.isSameTimePlay = checked;
                   PreferencesUtil.put(SettingPage.IS_SAMETIME_PLAY, this.isSameTimePlay)
+                  this.sendChangeEvent()
                 })
                 .width(50)
                 .height(30);
             }
             .height(55).clickEffect({ level: ClickEffectLevel.HEAVY })
 
-            .visibility(Visibility.None)
+            // .visibility(Visibility.None)
 
             Line().width('100%').height(0.3).backgroundColor($r('app.color.index_tab_unselected_font_color'))
             // 播放背景随音乐封面

+ 32 - 6
entry/src/main/ets/view/LocalMusic.ets

@@ -128,6 +128,7 @@ import {
 } from '@kit.UIDesignKit';
 import { GlobalAlphabetIndexerModifier } from '../Styels/GlobalStyles'
 import { getFirstLetter } from '../Constants';
+import { audio } from '@kit.AudioKit';
 const TAG = 'LocalMusic';
 const HICAR_LOG_TAG = 'HiCarWindow';
 
@@ -1208,7 +1209,10 @@ export struct LocalMusic {
         console.info('onecold startPlayOrResumePlay 514')
         this.startPlayOrResumePlay();
       } else if (event.hintType === InterruptHintType.INTERRUPT_HINT_STOP) {
-        this.stop();
+        console.info(`onecold 与其他应用音频冲突 被截断点 INTERRUPT_HINT_STOP`)
+        if(!this.isSameTimePlay){
+          this.stop();
+        }
       }
 
     }
@@ -1415,6 +1419,29 @@ export struct LocalMusic {
     this.fastForwardSeconds  = PreferencesUtil.getStringSync('fastForwardSeconds', '10')
     this.isShowBackFast  = PreferencesUtil.getBooleanSync('isShowBackFast', false)
     this.isCopyFileToDownLoad = PreferencesUtil.getBooleanSync(SettingPage.IS_COPYFILE_TO_DOWNLOAD, false)
+    let audioManager = audio.getAudioManager();
+    let audioSessionManager: audio.AudioSessionManager = audioManager.getSessionManager();
+    if(this.isSameTimePlay){
+      //与其他应用同时播放,关键代码
+      let strategy: audio.AudioSessionStrategy = {
+        concurrencyMode: audio.AudioConcurrencyMode.CONCURRENCY_MIX_WITH_OTHERS
+      };
+      audioSessionManager.activateAudioSession(strategy).then(() => {
+        console.info('onecold Succeeded in doing activateAudioSession.');
+      }).catch((err: BusinessError) => {
+        console.error(`Failed to activateAudioSession. Code: ${err.code}, message: ${err.message}`);
+      });
+    }else{
+      let isActivated = audioSessionManager.isAudioSessionActivated();
+      if(isActivated){
+        audioSessionManager.deactivateAudioSession().then(() => {
+          console.info('Succeeded in doing deactivateAudioSession.');
+        }).catch((err: BusinessError) => {
+          console.error(`Failed to deactivateAudioSession. Code: ${err.code}, message: ${err.message}`);
+        });
+      }
+
+    }
     if(this.volumeSmall){
       this.volume = 0.5
     }else{
@@ -13568,11 +13595,10 @@ export struct LocalMusic {
           } else {
             this.jumpTopTime = 0
             this.jumpEndTime = 0
-            console.error('onecold 最后一首: isFirstStartPlay = ' + this.isFirstStartPlay);
-            console.error('onecold 最后一首: isMemoryLastPlay = ' + this.isMemoryLastPlay);
-            console.error('onecold 最后一首: PreferencesUtil.getBooleanSync(SettingPage.iS_MUSIC_MEMORY_PLAY, false) = ' +
-            PreferencesUtil.getBooleanSync(SettingPage.iS_MUSIC_MEMORY_PLAY, false));
-
+            // console.error('onecold 最后一首: isFirstStartPlay = ' + this.isFirstStartPlay);
+            // console.error('onecold 最后一首: isMemoryLastPlay = ' + this.isMemoryLastPlay);
+            // console.error('onecold 最后一首: PreferencesUtil.getBooleanSync(SettingPage.iS_MUSIC_MEMORY_PLAY, false) = ' +
+            // PreferencesUtil.getBooleanSync(SettingPage.iS_MUSIC_MEMORY_PLAY, false));
             //记忆播放(每一首都会记忆播放)
             if (StrUtil.isNotEmpty(this.videoUrl) && !this.videoUrl.toLowerCase().endsWith('.ts')) {
               setTimeout(() => { //rmvb和flac格式马上记忆播放会报错,会延迟1s在读取记忆播放。