MusicPlayerWidgetCard.ets 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { MusicCardActionConstants } from '../../common/player/MusicCardActionConstants'
  2. import Logger from '../../common/util/Logger'
  3. import {
  4. clampMusicCardWidgetProgress,
  5. resolveMusicCardWidgetCoverSource
  6. } from '../common/MusicPlayerWidgetHelper'
  7. const cardStorage: LocalStorage = new LocalStorage()
  8. const ENTRY_ABILITY_NAME = 'EntryAbility'
  9. const TAG = 'MusicPlayerWidgetCard'
  10. @Entry(cardStorage)
  11. @Component
  12. struct MusicPlayerWidgetCard {
  13. @LocalStorageProp('formId') formId: string = ''
  14. @LocalStorageProp('title') title: string = '未在播放'
  15. @LocalStorageProp('artist') artist: string = '点击打开播放器'
  16. @LocalStorageProp('coverPath') coverPath: string = ''
  17. @LocalStorageProp('coverImageName') coverImageName: string = ''
  18. @LocalStorageProp('hasCoverImage') hasCoverImage: boolean = false
  19. @LocalStorageProp('hasSong') hasSong: boolean = false
  20. @LocalStorageProp('isPlaying') isPlaying: boolean = false
  21. @LocalStorageProp('currentPositionMs') @Watch('syncSliderFromPlayback') currentPositionMs: number = 0
  22. @LocalStorageProp('durationMs') @Watch('syncSliderFromPlayback') durationMs: number = 0
  23. @LocalStorageProp('currentTimeText') currentTimeText: string = '00:00'
  24. @LocalStorageProp('durationTimeText') durationTimeText: string = '00:00'
  25. @State sliderProgressMs: number = 0
  26. aboutToAppear(): void {
  27. Logger.info(TAG,
  28. `musicCard small aboutToAppear formId=${this.formId}, title=${this.title}, artist=${this.artist}, ` +
  29. `coverImageName=${this.coverImageName}, coverPath=${this.coverPath}, hasCoverImage=${this.hasCoverImage}, ` +
  30. `hasSong=${this.hasSong}, isPlaying=${this.isPlaying}, currentPositionMs=${this.currentPositionMs}, ` +
  31. `durationMs=${this.durationMs}`)
  32. this.syncSliderFromPlayback()
  33. }
  34. private syncSliderFromPlayback(): void {
  35. this.sliderProgressMs = clampMusicCardWidgetProgress(this.currentPositionMs, this.durationMs)
  36. }
  37. private postControlAction(action: string): void {
  38. Logger.info(TAG, `musicCard small postControlAction formId=${this.formId}, action=${action}`)
  39. postCardAction(this, {
  40. action: 'call',
  41. abilityName: ENTRY_ABILITY_NAME,
  42. params: {
  43. method: MusicCardActionConstants.CALL_METHOD_HANDLE_ACTION,
  44. ttmusic_music_card_form_id: this.formId,
  45. ttmusic_music_card_action: action,
  46. ttmusic_music_card_source: MusicCardActionConstants.ACTION_SOURCE_WIDGET
  47. }
  48. })
  49. }
  50. private postRouterAction(action: string): void {
  51. Logger.info(TAG, `musicCard small postRouterAction formId=${this.formId}, action=${action}`)
  52. postCardAction(this, {
  53. action: 'router',
  54. abilityName: ENTRY_ABILITY_NAME,
  55. params: {
  56. ttmusic_music_card_form_id: this.formId,
  57. ttmusic_music_card_action: action,
  58. ttmusic_music_card_source: MusicCardActionConstants.ACTION_SOURCE_WIDGET
  59. }
  60. })
  61. }
  62. private resolveCoverSource(): string | Resource {
  63. return resolveMusicCardWidgetCoverSource(this.coverImageName, this.coverPath, this.hasCoverImage)
  64. }
  65. private resolveProgressValue(): number {
  66. if (this.durationMs <= 0) {
  67. return 0
  68. }
  69. return Math.min(100, Math.max(0, Math.floor(this.sliderProgressMs * 100 / this.durationMs)))
  70. }
  71. private resolveTitleArtistText(): string {
  72. const safeTitle = this.title?.trim() ?? ''
  73. const safeArtist = this.artist?.trim() ?? ''
  74. if (safeTitle !== '' && safeArtist !== '') {
  75. return `${safeTitle} - ${safeArtist}`
  76. }
  77. if (safeTitle !== '') {
  78. return safeTitle
  79. }
  80. if (safeArtist !== '') {
  81. return safeArtist
  82. }
  83. return '未在播放'
  84. }
  85. private resolvePlayPauseIcon(): Resource {
  86. return this.isPlaying ? $r('sys.symbol.pause_fill') : $r('sys.symbol.play_fill')
  87. }
  88. @Builder
  89. private ControlButton(imageResource: Resource, action: string, buttonSize: number, symbolSize: number) {
  90. Button() {
  91. SymbolGlyph(imageResource)
  92. .fontSize(symbolSize)
  93. .fontColor([Color.White])
  94. .effectStrategy(SymbolEffectStrategy.HIERARCHICAL)
  95. }
  96. .width(buttonSize * 1.35)
  97. .height(buttonSize * 1.35)
  98. .backgroundColor(Color.Transparent)
  99. .type(ButtonType.Circle)
  100. .stateEffect(true)
  101. .onClick(() => this.postControlAction(action))
  102. }
  103. @Builder
  104. private PlayProgressButton() {
  105. Stack({ alignContent: Alignment.Center }) {
  106. Progress({ value: this.resolveProgressValue(), total: 100, type: ProgressType.Ring })
  107. .width(34)
  108. .height(34)
  109. .color(Color.White)
  110. .style({ strokeWidth: 2 })
  111. Button() {
  112. SymbolGlyph(this.resolvePlayPauseIcon())
  113. .fontSize(19)
  114. .fontColor([Color.White])
  115. .effectStrategy(SymbolEffectStrategy.HIERARCHICAL)
  116. }
  117. .width(34)
  118. .height(34)
  119. .type(ButtonType.Circle)
  120. .backgroundColor('#26FFFFFF')
  121. .stateEffect(false)
  122. .onClick(() => this.postControlAction(MusicCardActionConstants.ACTION_PLAY_PAUSE))
  123. }
  124. .width(34)
  125. .height(34)
  126. }
  127. build() {
  128. Stack() {
  129. Image(this.resolveCoverSource())
  130. .width('100%')
  131. .height('100%')
  132. .objectFit(ImageFit.Cover)
  133. Column()
  134. .width('100%')
  135. .height('100%')
  136. .linearGradient(
  137. { direction: GradientDirection.Right, colors:
  138. [['#00BC70',0.0],['#D81B60',1.0]]})
  139. Column({ space: 12 }) {
  140. Image(this.resolveCoverSource())
  141. .width(60)
  142. .height(60)
  143. .borderRadius(6)
  144. .objectFit(ImageFit.Cover)
  145. Text(this.resolveTitleArtistText())
  146. .fontSize(13)
  147. .fontWeight(FontWeight.Bold)
  148. .fontColor(Color.White)
  149. .maxLines(1)
  150. .textAlign(TextAlign.Center)
  151. .textOverflow({ overflow: TextOverflow.Ellipsis })
  152. Row({ space: 16 }) {
  153. this.ControlButton($r('sys.symbol.backward_end_fill'),
  154. MusicCardActionConstants.ACTION_PREVIOUS, 18, 18)
  155. this.PlayProgressButton()
  156. this.ControlButton($r('sys.symbol.forward_end_fill'),
  157. MusicCardActionConstants.ACTION_NEXT, 18, 18)
  158. }
  159. .justifyContent(FlexAlign.Center)
  160. .alignItems(VerticalAlign.Center)
  161. }
  162. .width('100%')
  163. .height('100%')
  164. .alignItems(HorizontalAlign.Center)
  165. .justifyContent(FlexAlign.Center)
  166. .padding({ left: 16, right: 16, top: 16, bottom: 14 })
  167. }
  168. .width('100%')
  169. .height('100%')
  170. .clip(true)
  171. .onClick(() => this.postRouterAction(MusicCardActionConstants.ACTION_OPEN_PLAYER))
  172. }
  173. }