AudioSuspensionModel.ets 4.64 KB
import window from '@ohos.window';
import { Logger } from 'wdKit';
import { WDPlayerController } from 'wdPlayer';
import { BusinessError } from '@ohos.base';
import { EmitterEventId, EmitterUtils } from 'wdKit/Index'

const TAG = 'AudioSuspensionModel'

/**
 * 音频悬浮窗公共方法类
 */
export class AudioSuspensionModel {
  public playerController: SubscribedAbstractProperty<WDPlayerController> =  AppStorage.link<WDPlayerController>('playerController')
  public floatWindowClass: SubscribedAbstractProperty<window.Window> =  AppStorage.link<window.Window>('floatWindowClass')
  public srcTitle: string = ''
  private url: string = ''
  private expandWidth: number = vp2px(243)
  private expandHeight: number = vp2px(60)
  // 窗口是否最小化
  private isMinimize: SubscribedAbstractProperty<boolean> = AppStorage.link<boolean>('isMinimize')
  constructor() {
    this.initPlayerController()
  }
  /**
   * 判断音频实例是否已存在,不存在则创建
   */
  private initPlayerController() {
    if(this.playerController === undefined) {
      Logger.info(TAG, 'playerController undefined')
      AppStorage.setOrCreate('playerController', new WDPlayerController());
      this.playerController = AppStorage.link<WDPlayerController>('playerController')
      Logger.info(TAG, 'playerController create success')
      this.playerController.get().onStatusChange = (status: number) => {
        console.info(TAG, 'this.currentStatus Model', status)
        EmitterUtils.sendEvent(EmitterEventId.AUDIO_CHANGE_STATUS, status)
      }
    } else {
      Logger.info(TAG, 'playerController already exit')
    }
  }
  /**
   * 配置音频地址
   */
  public setPlayerUrl(url: string, srcTitle: string) {
    // console.log(TAG,'this.url', this.url)
    // console.log(TAG,'url', url)
    if (this.url === url) {
      this.isMinimize = AppStorage.link<boolean>('isMinimize')
      console.log(TAG, 'this.isMinimize', this.isMinimize?.get())
      if (this.isMinimize?.get()) {
        EmitterUtils.sendEvent(EmitterEventId.AUDIO_WINDOW_EXPAND, 1)
        AppStorage.setOrCreate('isMinimize', false);
      }
      this.playerController.get().switchPlayOrPause()
    } else {
      this.url = url
      this.playerController.get().firstPlay(url)
      this.playerController.get().onCanplay = () => {
        this.playerController.get().play()
      }
      this.srcTitle = srcTitle
      EmitterUtils.sendEvent(EmitterEventId.AUDIO_CHANGE_TITLe, this.srcTitle)
      this.resizeWindow(this.expandWidth, this.expandHeight)
    }
    this.showWindow()
  }
  // 显示悬浮窗。
  public showWindow() {
    // 判断当前窗口是否已显示,使用callback异步回调。
    this.floatWindowClass.get().isShowing((err: BusinessError, data) => {
      const errCode: number = err.code;
      if (errCode) {
        console.error(TAG, 'Failed window is showing Cause:' + JSON.stringify(err));
        return;
      }
      console.info(TAG, 'window is showing: ' + JSON.stringify(data));
      if(data === false) {
        // 显示当前窗口,使用callback异步回调。
        this.floatWindowClass.get().showWindow((err: BusinessError) => {
          let errCode: number = err.code;
          if (errCode) {
            console.error(TAG, 'floatWindowClass Failed to show the window. Cause: ' + JSON.stringify(err));
            return;
          }
          console.info(TAG, 'floatWindowClass Succeeded in showing the window.');
        });
      }
    });
  }

  // 设置悬浮窗尺寸
  public resizeWindow(width: number, height: number) {
    this.floatWindowClass.get().resize(width, height, (err: BusinessError) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error(TAG, 'floatWindowClass Failed to change the window size. Cause:' + JSON.stringify(err));
        return;
      }
      console.info(TAG, 'floatWindowClass Succeeded in changing the window size.');
    });
  }

  public moveWindow(y: number) {
    this.floatWindowClass.get().moveWindowTo(0, vp2px(y), (err: BusinessError) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('floatWindowClass Failed to move the window. Cause:' + JSON.stringify(err));
        return;
      }
      console.info('floatWindowClass Succeeded in moving the window.');
    });
  }

  // 隐藏悬浮窗
  public minimize() {
    this.floatWindowClass.get().minimize((err: BusinessError) => {
      const errCode: number = err.code;
      if (errCode) {
        console.error(TAG, 'Failed to minimize the window. Cause: ' + JSON.stringify(err));
        return;
      }
      AppStorage.setOrCreate('isMinimize', true);
      console.info(TAG, 'Succeeded in minimizing the window.');
    });
  }



}