Showing
4 changed files
with
146 additions
and
1 deletions
| @@ -12,6 +12,7 @@ | @@ -12,6 +12,7 @@ | ||
| 12 | "wdJsBridge": "file:../wdJsBridge", | 12 | "wdJsBridge": "file:../wdJsBridge", |
| 13 | "wdBean": "file:../../features/wdBean", | 13 | "wdBean": "file:../../features/wdBean", |
| 14 | "wdRouter": "file:../wdRouter", | 14 | "wdRouter": "file:../wdRouter", |
| 15 | - "wdNetwork": "file:../wdNetwork" | 15 | + "wdNetwork": "file:../wdNetwork", |
| 16 | + "wdPlayer": "file:../../features/wdPlayer" | ||
| 16 | } | 17 | } |
| 17 | } | 18 | } |
| @@ -7,6 +7,8 @@ import { Message } from 'wdJsBridge/src/main/ets/bean/Message'; | @@ -7,6 +7,8 @@ import { Message } from 'wdJsBridge/src/main/ets/bean/Message'; | ||
| 7 | import { DateTimeUtils,EmitterUtils,EmitterEventId } from 'wdKit' | 7 | import { DateTimeUtils,EmitterUtils,EmitterEventId } from 'wdKit' |
| 8 | import { window } from '@kit.ArkUI'; | 8 | import { window } from '@kit.ArkUI'; |
| 9 | import { NativeCallH5Type,NativeCallH5Event,eventParams } from './NativeCallH5Type'; | 9 | import { NativeCallH5Type,NativeCallH5Event,eventParams } from './NativeCallH5Type'; |
| 10 | +import { AudioSuspensionModel } from '../viewmodel/AudioSuspensionModel' | ||
| 11 | +import { BusinessError } from '@kit.BasicServicesKit'; | ||
| 10 | 12 | ||
| 11 | const TAG = 'WdWebLocalComponent'; | 13 | const TAG = 'WdWebLocalComponent'; |
| 12 | 14 | ||
| @@ -44,6 +46,8 @@ export struct WdWebLocalComponent { | @@ -44,6 +46,8 @@ export struct WdWebLocalComponent { | ||
| 44 | @Consume @Watch('pageShowForUpdateData') pageShow :number | 46 | @Consume @Watch('pageShowForUpdateData') pageShow :number |
| 45 | @Consume @Watch('pageHideForUpdateData') pageHide :number | 47 | @Consume @Watch('pageHideForUpdateData') pageHide :number |
| 46 | 48 | ||
| 49 | + private AudioSuspension = new AudioSuspensionModel() | ||
| 50 | + | ||
| 47 | currentChanged(){ | 51 | currentChanged(){ |
| 48 | ///折叠屏转换 暂停播放器 | 52 | ///折叠屏转换 暂停播放器 |
| 49 | this.controller.pause() | 53 | this.controller.pause() |
| @@ -186,6 +190,19 @@ export struct WdWebLocalComponent { | @@ -186,6 +190,19 @@ export struct WdWebLocalComponent { | ||
| 186 | } | 190 | } |
| 187 | } | 191 | } |
| 188 | } | 192 | } |
| 193 | + // 暂停音频悬浮窗 | ||
| 194 | + pauseAudioCom() { | ||
| 195 | + // 判断当前窗口是否已显示,使用callback异步回调。 | ||
| 196 | + this.AudioSuspension.floatWindowClass.get().isShowing((err: BusinessError, data) => { | ||
| 197 | + const errCode: number = err.code; | ||
| 198 | + if (errCode) { | ||
| 199 | + return; | ||
| 200 | + } | ||
| 201 | + if(data) { | ||
| 202 | + this.AudioSuspension.playerController.get()?.pause(); | ||
| 203 | + } | ||
| 204 | + }); | ||
| 205 | + } | ||
| 189 | //播放视频 | 206 | //播放视频 |
| 190 | private setCurrentPageOperate9: (data: Message) => void = (data) => { | 207 | private setCurrentPageOperate9: (data: Message) => void = (data) => { |
| 191 | if (data.handlerName === H5CallNativeType.jsCall_currentPageOperate && data?.data?.operateType === '9') { | 208 | if (data.handlerName === H5CallNativeType.jsCall_currentPageOperate && data?.data?.operateType === '9') { |
| @@ -235,6 +252,7 @@ export struct WdWebLocalComponent { | @@ -235,6 +252,7 @@ export struct WdWebLocalComponent { | ||
| 235 | this.cancelProgressTimer() | 252 | this.cancelProgressTimer() |
| 236 | this.controller.start() | 253 | this.controller.start() |
| 237 | this.startProgressTimer() | 254 | this.startProgressTimer() |
| 255 | + this.pauseAudioCom() | ||
| 238 | } | 256 | } |
| 239 | 257 | ||
| 240 | startProgressTimer() { | 258 | startProgressTimer() { |
| 1 | +import window from '@ohos.window'; | ||
| 2 | +import { Logger } from 'wdKit'; | ||
| 3 | +import { BackgroundAudioController, WDPlayerController } from 'wdPlayer'; | ||
| 4 | +import { BusinessError } from '@ohos.base'; | ||
| 5 | +import { EmitterEventId, EmitterUtils } from 'wdKit/Index' | ||
| 6 | + | ||
| 7 | +const TAG = 'AudioSuspensionModel' | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * 音频悬浮窗公共方法类 | ||
| 11 | + */ | ||
| 12 | +export class AudioSuspensionModel { | ||
| 13 | + public playerController: SubscribedAbstractProperty<WDPlayerController> = AppStorage.link<WDPlayerController>('playerController') | ||
| 14 | + public floatWindowClass: SubscribedAbstractProperty<window.Window> = AppStorage.link<window.Window>('floatWindowClass') | ||
| 15 | + public srcTitle: string = '' | ||
| 16 | + private url: string = '' | ||
| 17 | + // 窗口是否最小化 | ||
| 18 | + private isMinimize: SubscribedAbstractProperty<boolean> = AppStorage.link<boolean>('isMinimize') | ||
| 19 | + constructor() { | ||
| 20 | + this.initPlayerController() | ||
| 21 | + } | ||
| 22 | + /** | ||
| 23 | + * 判断音频实例是否已存在,不存在则创建 | ||
| 24 | + */ | ||
| 25 | + private initPlayerController() { | ||
| 26 | + if(this.playerController === undefined) { | ||
| 27 | + // Logger.info(TAG, 'playerController undefined') | ||
| 28 | + AppStorage.setOrCreate('playerController', new WDPlayerController({loop: false})); | ||
| 29 | + this.playerController = AppStorage.link<WDPlayerController>('playerController') | ||
| 30 | + // Logger.info(TAG, 'playerController create success') | ||
| 31 | + this.playerController.get().onStatusChange = (status: number) => { | ||
| 32 | + // console.info(TAG, 'this.currentStatus Model', status) | ||
| 33 | + EmitterUtils.sendEvent(EmitterEventId.AUDIO_CHANGE_STATUS, status) | ||
| 34 | + } | ||
| 35 | + } else { | ||
| 36 | + // Logger.info(TAG, 'playerController already exit') | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + /** | ||
| 40 | + * 配置音频地址 | ||
| 41 | + */ | ||
| 42 | + public async setPlayerUrl(url: string, srcTitle: string, srcContentId?: string, srcSource?: string) { | ||
| 43 | + /*console.log(TAG,'this.url', this.url) | ||
| 44 | + console.log(TAG,'url', url)*/ | ||
| 45 | + this.playerController.get().keepOnBackground = true | ||
| 46 | + BackgroundAudioController.sharedController().avplayerController = this.playerController.get() | ||
| 47 | + await BackgroundAudioController.sharedController().createSession() | ||
| 48 | + // BackgroundAudioController.sharedController().startContinuousTask() | ||
| 49 | + BackgroundAudioController.sharedController().listenPlayEvents() | ||
| 50 | + await BackgroundAudioController.sharedController().setSessionMetaData(srcContentId ?? "", srcTitle, $r("app.media.system_audio_icon_bk_center"), srcSource ?? "") | ||
| 51 | + BackgroundAudioController.sharedController().stopUseFeatures() | ||
| 52 | + | ||
| 53 | + if (this.url === url) { | ||
| 54 | + this.isMinimize = AppStorage.link<boolean>('isMinimize') | ||
| 55 | + // console.log(TAG, 'this.isMinimize', this.isMinimize?.get()) | ||
| 56 | + if (this.isMinimize?.get()) { | ||
| 57 | + EmitterUtils.sendEvent(EmitterEventId.AUDIO_WINDOW_EXPAND, 1) | ||
| 58 | + AppStorage.setOrCreate('isMinimize', false); | ||
| 59 | + this.playerController.get().resetPlay() | ||
| 60 | + } else { | ||
| 61 | + this.playerController.get().switchPlayOrPause() | ||
| 62 | + } | ||
| 63 | + } else { | ||
| 64 | + this.url = url | ||
| 65 | + this.playerController.get().firstPlay(url) | ||
| 66 | + this.playerController.get().onCanplay = () => { | ||
| 67 | + this.playerController.get().play() | ||
| 68 | + } | ||
| 69 | + this.srcTitle = srcTitle | ||
| 70 | + EmitterUtils.sendEvent(EmitterEventId.AUDIO_CHANGE_TITLe, this.srcTitle) | ||
| 71 | + EmitterUtils.sendEvent(EmitterEventId.AUDIO_WINDOW_EXPAND, 1) | ||
| 72 | + } | ||
| 73 | + this.showWindow() | ||
| 74 | + } | ||
| 75 | + // 显示悬浮窗。 | ||
| 76 | + public showWindow() { | ||
| 77 | + // 判断当前窗口是否已显示,使用callback异步回调。 | ||
| 78 | + this.floatWindowClass.get().isShowing((err: BusinessError, data) => { | ||
| 79 | + const errCode: number = err.code; | ||
| 80 | + if (errCode) { | ||
| 81 | + // console.error(TAG, 'Failed window is showing Cause:' + JSON.stringify(err)); | ||
| 82 | + return; | ||
| 83 | + } | ||
| 84 | + // console.info(TAG, 'window is showing: ' + JSON.stringify(data)); | ||
| 85 | + if(data === false) { | ||
| 86 | + // 显示当前窗口,使用callback异步回调。 | ||
| 87 | + this.floatWindowClass.get().showWindow((err: BusinessError) => { | ||
| 88 | + let errCode: number = err.code; | ||
| 89 | + if (errCode) { | ||
| 90 | + // console.error(TAG, 'floatWindowClass Failed to show the window. Cause: ' + JSON.stringify(err)); | ||
| 91 | + return; | ||
| 92 | + } | ||
| 93 | + // console.info(TAG, 'floatWindowClass Succeeded in showing the window.'); | ||
| 94 | + }); | ||
| 95 | + } | ||
| 96 | + }); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + // 设置悬浮窗尺寸 | ||
| 100 | + public resizeWindow(width: number, height: number) { | ||
| 101 | + this.floatWindowClass.get().resize(width, height, (err: BusinessError) => { | ||
| 102 | + let errCode: number = err.code; | ||
| 103 | + if (errCode) { | ||
| 104 | + // console.error(TAG, 'floatWindowClass Failed to change the window size. Cause:' + JSON.stringify(err)); | ||
| 105 | + return; | ||
| 106 | + } | ||
| 107 | + // console.info(TAG, 'floatWindowClass Succeeded in changing the window size.'); | ||
| 108 | + }); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + // 隐藏悬浮窗 | ||
| 112 | + public minimize() { | ||
| 113 | + this.floatWindowClass.get().minimize((err: BusinessError) => { | ||
| 114 | + const errCode: number = err.code; | ||
| 115 | + if (errCode) { | ||
| 116 | + // console.error(TAG, 'Failed to minimize the window. Cause: ' + JSON.stringify(err)); | ||
| 117 | + return; | ||
| 118 | + } | ||
| 119 | + AppStorage.setOrCreate('isMinimize', true); | ||
| 120 | + // console.info(TAG, 'Succeeded in minimizing the window.'); | ||
| 121 | + }); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + | ||
| 125 | + | ||
| 126 | +} |
-
Please register or login to post a comment