AudioSuspensionModel.ets 3.89 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 = 800
  private expandHeight: number = 200
  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', 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.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)
      console.log(TAG, 'handlePlayer')
      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.');
    });
  }

  // 销毁悬浮窗 使用destroy对其进行销毁。
  public destroyWindow() {
    /*this.floatWindowClass.get().destroyWindow((err: BusinessError) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('floatWindowClass Failed to destroy the window. Cause: ' + JSON.stringify(err));
        return;
      }
      console.info('floatWindowClass Succeeded in destroying the window.');
    });*/
  }



}