PlaybackCoordinator.ets 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { PlaybackStateBridge } from './PlaybackStateBridge'
  2. import { VideoItem } from '../viewmodel/VideoItem'
  3. export interface PlaybackRuntime {
  4. playQueue: (queue: VideoItem[], startIndex: number, source: string) => Promise<void>
  5. playOrPause: () => Promise<void>
  6. playNext: () => Promise<void>
  7. playPrevious: () => Promise<void>
  8. seekTo: (value: string, source?: string) => Promise<void>
  9. }
  10. export class PlaybackCoordinator {
  11. private static instance?: PlaybackCoordinator
  12. private runtime?: PlaybackRuntime
  13. private stateBridge: PlaybackStateBridge = PlaybackStateBridge.getInstance()
  14. public static getInstance(): PlaybackCoordinator {
  15. if (!PlaybackCoordinator.instance) {
  16. PlaybackCoordinator.instance = new PlaybackCoordinator()
  17. }
  18. return PlaybackCoordinator.instance
  19. }
  20. public setRuntime(runtime: PlaybackRuntime): void {
  21. this.runtime = runtime
  22. }
  23. public clearRuntime(runtime?: PlaybackRuntime): void {
  24. if (!runtime || this.runtime === runtime) {
  25. this.runtime = undefined
  26. }
  27. }
  28. public async playQueue(queue: VideoItem[], startIndex: number, source: string): Promise<void> {
  29. if (!this.runtime || queue.length <= 0) {
  30. return
  31. }
  32. this.stateBridge.replaceQueue(queue, startIndex)
  33. await this.runtime.playQueue(queue, this.stateBridge.getSnapshot().currentIndex, source)
  34. }
  35. public async playSong(song: VideoItem, source: string): Promise<void> {
  36. await this.playQueue([song], 0, source)
  37. }
  38. public async playOrPause(): Promise<void> {
  39. await this.runtime?.playOrPause()
  40. }
  41. public async playNext(): Promise<void> {
  42. await this.runtime?.playNext()
  43. }
  44. public async playPrevious(): Promise<void> {
  45. await this.runtime?.playPrevious()
  46. }
  47. public async seekTo(value: string, source?: string): Promise<void> {
  48. await this.runtime?.seekTo(value, source)
  49. }
  50. }