react-native-theoplayer 11.5.0 → 11.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (282) hide show
  1. package/CHANGELOG.md +32 -1
  2. package/README.md +70 -0
  3. package/android/build.gradle +1 -0
  4. package/android/src/main/java/com/theoplayer/PlayerConfigAdapter.kt +6 -0
  5. package/android/src/main/java/com/theoplayer/PlayerEventEmitter.kt +11 -2
  6. package/android/src/main/java/com/theoplayer/ReactTHEOplayerContext.kt +95 -93
  7. package/android/src/main/java/com/theoplayer/ReactTHEOplayerPackage.kt +6 -0
  8. package/android/src/main/java/com/theoplayer/ReactTHEOplayerView.kt +51 -3
  9. package/android/src/main/java/com/theoplayer/ads/AdEventAdapter.kt +4 -0
  10. package/android/src/main/java/com/theoplayer/ads/AdsModule.kt +6 -6
  11. package/android/src/main/java/com/theoplayer/integration/Integration.kt +72 -0
  12. package/android/src/main/java/com/theoplayer/integration/IntegrationRegistration.kt +68 -0
  13. package/android/src/main/java/com/theoplayer/integration/PlayerFacade.kt +187 -0
  14. package/android/src/main/java/com/theoplayer/integration/PlayerFacadeAdState.kt +19 -0
  15. package/android/src/main/java/com/theoplayer/integration/PlayerFacadeAds.kt +73 -0
  16. package/android/src/main/java/com/theoplayer/integration/PlayerFacadeAdsBridge.kt +112 -0
  17. package/android/src/main/java/com/theoplayer/integration/PlayerFacadeEventSubscription.kt +11 -0
  18. package/android/src/main/java/com/theoplayer/integration/PlayerFacadeEvents.kt +139 -0
  19. package/android/src/main/java/com/theoplayer/media/MediaControlModule.kt +102 -0
  20. package/android/src/main/java/com/theoplayer/media/MediaControlProxy.kt +376 -0
  21. package/android/src/main/java/com/theoplayer/media/MediaNotificationBuilder.kt +78 -34
  22. package/android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt +53 -20
  23. package/android/src/main/java/com/theoplayer/media/MediaSessionConfig.kt +6 -6
  24. package/android/src/main/java/com/theoplayer/media/MediaSessionConfigAdapter.kt +23 -20
  25. package/android/src/main/java/com/theoplayer/metrics/MetricsModule.kt +37 -0
  26. package/android/src/main/java/com/theoplayer/player/PlayerModule.kt +0 -1
  27. package/android/src/main/java/com/theoplayer/presentation/PipUtils.kt +98 -93
  28. package/android/src/main/java/com/theoplayer/theoads/THEOadsModule.kt +1 -1
  29. package/android/src/main/java/com/theoplayer/util/ReadableExtension.kt +14 -0
  30. package/android/src/main/res/drawable/ic_next.xml +9 -0
  31. package/android/src/main/res/drawable/ic_prev.xml +9 -0
  32. package/android/src/main/res/values/strings.xml +4 -0
  33. package/android/src/test/java/com/theoplayer/integration/PlayerFacadeAdsBridgeTest.kt +250 -0
  34. package/android/src/test/java/com/theoplayer/integration/PlayerFacadeConfigurationTest.kt +30 -0
  35. package/android/src/test/java/com/theoplayer/integration/PlayerFacadeTest.kt +507 -0
  36. package/android/src/test/java/com/theoplayer/integration/PlayerFacadeTestHost.kt +104 -0
  37. package/ios/THEOplayerRCTBridge.m +35 -0
  38. package/ios/THEOplayerRCTDebug.swift +3 -0
  39. package/ios/THEOplayerRCTMainEventHandler.swift +17 -0
  40. package/ios/THEOplayerRCTPlayerAPI.swift +31 -0
  41. package/ios/THEOplayerRCTTypeUtils.swift +30 -0
  42. package/ios/THEOplayerRCTView.swift +15 -5
  43. package/ios/Theoplayer-Bridging-Header.h +0 -1
  44. package/ios/backgroundAudio/THEOplayerRCTBackgroundAudioManager.swift +1 -1
  45. package/ios/backgroundAudio/THEOplayerRCTNowPlayingManager.swift +49 -3
  46. package/ios/backgroundAudio/THEOplayerRCTRemoteCommandsManager.swift +151 -68
  47. package/ios/backgroundAudio/THEOplayerRCTView+MediaControlConfig.swift +25 -11
  48. package/ios/mediaControl/THEOplayerRCTMediaControlAPI.swift +79 -0
  49. package/ios/mediaControl/THEOplayerRCTMediaControlManager.swift +38 -0
  50. package/ios/metrics/THEOplayerRCTMetricsAPI.swift +31 -0
  51. package/ios/pip/THEOplayerRCTPipControlsManager.swift +9 -12
  52. package/lib/commonjs/api/barrel.js +31 -20
  53. package/lib/commonjs/api/barrel.js.map +1 -1
  54. package/lib/commonjs/api/event/PlayerEvent.js +8 -0
  55. package/lib/commonjs/api/event/PlayerEvent.js.map +1 -1
  56. package/lib/commonjs/api/media/MediaControlAPI.js +14 -0
  57. package/lib/commonjs/api/media/MediaControlAPI.js.map +1 -0
  58. package/lib/commonjs/api/media/barrel.js +11 -0
  59. package/lib/commonjs/api/media/barrel.js.map +1 -1
  60. package/lib/commonjs/api/metrics/MetricsAPI.js +2 -0
  61. package/lib/commonjs/api/metrics/MetricsAPI.js.map +1 -0
  62. package/lib/commonjs/api/metrics/barrel.js +17 -0
  63. package/lib/commonjs/api/metrics/barrel.js.map +1 -0
  64. package/lib/commonjs/api/player/Integration.js +2 -0
  65. package/lib/commonjs/api/player/Integration.js.map +1 -0
  66. package/lib/commonjs/api/player/PlayerEventMap.js +1 -0
  67. package/lib/commonjs/api/player/PlayerEventMap.js.map +1 -1
  68. package/lib/commonjs/api/player/THEOplayer.js.map +1 -1
  69. package/lib/commonjs/api/player/barrel.js +11 -0
  70. package/lib/commonjs/api/player/barrel.js.map +1 -1
  71. package/lib/commonjs/api/track/MediaTrack.js +16 -0
  72. package/lib/commonjs/api/track/MediaTrack.js.map +1 -1
  73. package/lib/commonjs/internal/THEOplayerView.js +6 -0
  74. package/lib/commonjs/internal/THEOplayerView.js.map +1 -1
  75. package/lib/commonjs/internal/THEOplayerView.web.js +3 -1
  76. package/lib/commonjs/internal/THEOplayerView.web.js.map +1 -1
  77. package/lib/commonjs/internal/adapter/DefaultNativePlayerState.js +1 -0
  78. package/lib/commonjs/internal/adapter/DefaultNativePlayerState.js.map +1 -1
  79. package/lib/commonjs/internal/adapter/THEOplayerAdapter.js +24 -0
  80. package/lib/commonjs/internal/adapter/THEOplayerAdapter.js.map +1 -1
  81. package/lib/commonjs/internal/adapter/THEOplayerWebAdapter.js +29 -12
  82. package/lib/commonjs/internal/adapter/THEOplayerWebAdapter.js.map +1 -1
  83. package/lib/commonjs/internal/adapter/WebEventForwarder.js +6 -1
  84. package/lib/commonjs/internal/adapter/WebEventForwarder.js.map +1 -1
  85. package/lib/commonjs/internal/adapter/event/PlayerEvents.js +7 -1
  86. package/lib/commonjs/internal/adapter/event/PlayerEvents.js.map +1 -1
  87. package/lib/commonjs/internal/adapter/media/MediaControlNativeAdapter.js +42 -0
  88. package/lib/commonjs/internal/adapter/media/MediaControlNativeAdapter.js.map +1 -0
  89. package/lib/commonjs/internal/adapter/media/MediaControlWebAdapter.js +32 -0
  90. package/lib/commonjs/internal/adapter/media/MediaControlWebAdapter.js.map +1 -0
  91. package/lib/commonjs/internal/adapter/metrics/MetricsNativeAdapter.js +18 -0
  92. package/lib/commonjs/internal/adapter/metrics/MetricsNativeAdapter.js.map +1 -0
  93. package/lib/commonjs/internal/adapter/metrics/MetricsWebAdapter.js +16 -0
  94. package/lib/commonjs/internal/adapter/metrics/MetricsWebAdapter.js.map +1 -0
  95. package/lib/commonjs/internal/adapter/web/IntegrationRegistration.js +6 -0
  96. package/lib/commonjs/internal/adapter/web/IntegrationRegistration.js.map +1 -0
  97. package/lib/commonjs/internal/adapter/web/PlayerFacade.js +258 -0
  98. package/lib/commonjs/internal/adapter/web/PlayerFacade.js.map +1 -0
  99. package/lib/commonjs/internal/adapter/web/PlayerFacadeAPI.js +6 -0
  100. package/lib/commonjs/internal/adapter/web/PlayerFacadeAPI.js.map +1 -0
  101. package/lib/commonjs/internal/adapter/web/PlayerFacadeAdsEvents.js +114 -0
  102. package/lib/commonjs/internal/adapter/web/PlayerFacadeAdsEvents.js.map +1 -0
  103. package/lib/commonjs/internal/adapter/web/PlayerFacadeIntegration.js +6 -0
  104. package/lib/commonjs/internal/adapter/web/PlayerFacadeIntegration.js.map +1 -0
  105. package/lib/commonjs/internal/adapter/web/PlayerFacadeTypes.js +6 -0
  106. package/lib/commonjs/internal/adapter/web/PlayerFacadeTypes.js.map +1 -0
  107. package/lib/commonjs/internal/adapter/web/WebMediaSession.js +78 -16
  108. package/lib/commonjs/internal/adapter/web/WebMediaSession.js.map +1 -1
  109. package/lib/commonjs/manifest.json +1 -1
  110. package/lib/module/api/barrel.js +1 -0
  111. package/lib/module/api/barrel.js.map +1 -1
  112. package/lib/module/api/event/PlayerEvent.js +9 -0
  113. package/lib/module/api/event/PlayerEvent.js.map +1 -1
  114. package/lib/module/api/media/MediaControlAPI.js +10 -0
  115. package/lib/module/api/media/MediaControlAPI.js.map +1 -0
  116. package/lib/module/api/media/barrel.js +1 -0
  117. package/lib/module/api/media/barrel.js.map +1 -1
  118. package/lib/module/api/metrics/MetricsAPI.js +2 -0
  119. package/lib/module/api/metrics/MetricsAPI.js.map +1 -0
  120. package/lib/module/api/metrics/barrel.js +4 -0
  121. package/lib/module/api/metrics/barrel.js.map +1 -0
  122. package/lib/module/api/player/Integration.js +2 -0
  123. package/lib/module/api/player/Integration.js.map +1 -0
  124. package/lib/module/api/player/PlayerEventMap.js +1 -0
  125. package/lib/module/api/player/PlayerEventMap.js.map +1 -1
  126. package/lib/module/api/player/THEOplayer.js.map +1 -1
  127. package/lib/module/api/player/barrel.js +1 -0
  128. package/lib/module/api/player/barrel.js.map +1 -1
  129. package/lib/module/api/track/MediaTrack.js +16 -0
  130. package/lib/module/api/track/MediaTrack.js.map +1 -1
  131. package/lib/module/internal/THEOplayerView.js +7 -1
  132. package/lib/module/internal/THEOplayerView.js.map +1 -1
  133. package/lib/module/internal/THEOplayerView.web.js +3 -1
  134. package/lib/module/internal/THEOplayerView.web.js.map +1 -1
  135. package/lib/module/internal/adapter/DefaultNativePlayerState.js +1 -0
  136. package/lib/module/internal/adapter/DefaultNativePlayerState.js.map +1 -1
  137. package/lib/module/internal/adapter/THEOplayerAdapter.js +24 -0
  138. package/lib/module/internal/adapter/THEOplayerAdapter.js.map +1 -1
  139. package/lib/module/internal/adapter/THEOplayerWebAdapter.js +29 -12
  140. package/lib/module/internal/adapter/THEOplayerWebAdapter.js.map +1 -1
  141. package/lib/module/internal/adapter/WebEventForwarder.js +7 -2
  142. package/lib/module/internal/adapter/WebEventForwarder.js.map +1 -1
  143. package/lib/module/internal/adapter/event/PlayerEvents.js +5 -0
  144. package/lib/module/internal/adapter/event/PlayerEvents.js.map +1 -1
  145. package/lib/module/internal/adapter/media/MediaControlNativeAdapter.js +37 -0
  146. package/lib/module/internal/adapter/media/MediaControlNativeAdapter.js.map +1 -0
  147. package/lib/module/internal/adapter/media/MediaControlWebAdapter.js +27 -0
  148. package/lib/module/internal/adapter/media/MediaControlWebAdapter.js.map +1 -0
  149. package/lib/module/internal/adapter/metrics/MetricsNativeAdapter.js +13 -0
  150. package/lib/module/internal/adapter/metrics/MetricsNativeAdapter.js.map +1 -0
  151. package/lib/module/internal/adapter/metrics/MetricsWebAdapter.js +11 -0
  152. package/lib/module/internal/adapter/metrics/MetricsWebAdapter.js.map +1 -0
  153. package/lib/module/internal/adapter/web/IntegrationRegistration.js +4 -0
  154. package/lib/module/internal/adapter/web/IntegrationRegistration.js.map +1 -0
  155. package/lib/module/internal/adapter/web/PlayerFacade.js +253 -0
  156. package/lib/module/internal/adapter/web/PlayerFacade.js.map +1 -0
  157. package/lib/module/internal/adapter/web/PlayerFacadeAPI.js +4 -0
  158. package/lib/module/internal/adapter/web/PlayerFacadeAPI.js.map +1 -0
  159. package/lib/module/internal/adapter/web/PlayerFacadeAdsEvents.js +109 -0
  160. package/lib/module/internal/adapter/web/PlayerFacadeAdsEvents.js.map +1 -0
  161. package/lib/module/internal/adapter/web/PlayerFacadeIntegration.js +4 -0
  162. package/lib/module/internal/adapter/web/PlayerFacadeIntegration.js.map +1 -0
  163. package/lib/module/internal/adapter/web/PlayerFacadeTypes.js +4 -0
  164. package/lib/module/internal/adapter/web/PlayerFacadeTypes.js.map +1 -0
  165. package/lib/module/internal/adapter/web/WebMediaSession.js +78 -16
  166. package/lib/module/internal/adapter/web/WebMediaSession.js.map +1 -1
  167. package/lib/module/manifest.json +1 -1
  168. package/lib/typescript/__tests__/PlayerFacade.test.d.ts +2 -0
  169. package/lib/typescript/__tests__/PlayerFacade.test.d.ts.map +1 -0
  170. package/lib/typescript/__tests__/PlayerFacadeWebAdapter.test.d.ts +2 -0
  171. package/lib/typescript/__tests__/PlayerFacadeWebAdapter.test.d.ts.map +1 -0
  172. package/lib/typescript/__tests__/index.test.d.ts +13 -0
  173. package/lib/typescript/__tests__/index.test.d.ts.map +1 -1
  174. package/lib/typescript/api/THEOplayerView.d.ts +4 -0
  175. package/lib/typescript/api/THEOplayerView.d.ts.map +1 -1
  176. package/lib/typescript/api/barrel.d.ts +1 -0
  177. package/lib/typescript/api/barrel.d.ts.map +1 -1
  178. package/lib/typescript/api/config/PlayerConfiguration.d.ts +15 -0
  179. package/lib/typescript/api/config/PlayerConfiguration.d.ts.map +1 -1
  180. package/lib/typescript/api/event/AdEvent.d.ts +1 -1
  181. package/lib/typescript/api/event/PlayerEvent.d.ts +9 -0
  182. package/lib/typescript/api/event/PlayerEvent.d.ts.map +1 -1
  183. package/lib/typescript/api/media/MediaControlAPI.d.ts +30 -0
  184. package/lib/typescript/api/media/MediaControlAPI.d.ts.map +1 -0
  185. package/lib/typescript/api/media/MediaControlConfiguration.d.ts +5 -1
  186. package/lib/typescript/api/media/MediaControlConfiguration.d.ts.map +1 -1
  187. package/lib/typescript/api/media/barrel.d.ts +1 -0
  188. package/lib/typescript/api/media/barrel.d.ts.map +1 -1
  189. package/lib/typescript/api/metrics/MetricsAPI.d.ts +21 -0
  190. package/lib/typescript/api/metrics/MetricsAPI.d.ts.map +1 -0
  191. package/lib/typescript/api/metrics/barrel.d.ts +2 -0
  192. package/lib/typescript/api/metrics/barrel.d.ts.map +1 -0
  193. package/lib/typescript/api/player/Integration.d.ts +88 -0
  194. package/lib/typescript/api/player/Integration.d.ts.map +1 -0
  195. package/lib/typescript/api/player/PlayerEventMap.d.ts +7 -2
  196. package/lib/typescript/api/player/PlayerEventMap.d.ts.map +1 -1
  197. package/lib/typescript/api/player/THEOplayer.d.ts +24 -0
  198. package/lib/typescript/api/player/THEOplayer.d.ts.map +1 -1
  199. package/lib/typescript/api/player/barrel.d.ts +1 -0
  200. package/lib/typescript/api/player/barrel.d.ts.map +1 -1
  201. package/lib/typescript/api/track/MediaTrack.d.ts +21 -4
  202. package/lib/typescript/api/track/MediaTrack.d.ts.map +1 -1
  203. package/lib/typescript/internal/THEOplayerView.d.ts +1 -0
  204. package/lib/typescript/internal/THEOplayerView.d.ts.map +1 -1
  205. package/lib/typescript/internal/adapter/DefaultNativePlayerState.d.ts +1 -0
  206. package/lib/typescript/internal/adapter/DefaultNativePlayerState.d.ts.map +1 -1
  207. package/lib/typescript/internal/adapter/NativePlayerState.d.ts +1 -0
  208. package/lib/typescript/internal/adapter/NativePlayerState.d.ts.map +1 -1
  209. package/lib/typescript/internal/adapter/THEOplayerAdapter.d.ts +8 -1
  210. package/lib/typescript/internal/adapter/THEOplayerAdapter.d.ts.map +1 -1
  211. package/lib/typescript/internal/adapter/THEOplayerWebAdapter.d.ts +8 -2
  212. package/lib/typescript/internal/adapter/THEOplayerWebAdapter.d.ts.map +1 -1
  213. package/lib/typescript/internal/adapter/WebEventForwarder.d.ts +1 -0
  214. package/lib/typescript/internal/adapter/WebEventForwarder.d.ts.map +1 -1
  215. package/lib/typescript/internal/adapter/event/PlayerEvents.d.ts +4 -1
  216. package/lib/typescript/internal/adapter/event/PlayerEvents.d.ts.map +1 -1
  217. package/lib/typescript/internal/adapter/event/native/NativePlayerEvent.d.ts +1 -0
  218. package/lib/typescript/internal/adapter/event/native/NativePlayerEvent.d.ts.map +1 -1
  219. package/lib/typescript/internal/adapter/media/MediaControlNativeAdapter.d.ts +12 -0
  220. package/lib/typescript/internal/adapter/media/MediaControlNativeAdapter.d.ts.map +1 -0
  221. package/lib/typescript/internal/adapter/media/MediaControlWebAdapter.d.ts +14 -0
  222. package/lib/typescript/internal/adapter/media/MediaControlWebAdapter.d.ts.map +1 -0
  223. package/lib/typescript/internal/adapter/metrics/MetricsNativeAdapter.d.ts +7 -0
  224. package/lib/typescript/internal/adapter/metrics/MetricsNativeAdapter.d.ts.map +1 -0
  225. package/lib/typescript/internal/adapter/metrics/MetricsWebAdapter.d.ts +8 -0
  226. package/lib/typescript/internal/adapter/metrics/MetricsWebAdapter.d.ts.map +1 -0
  227. package/lib/typescript/internal/adapter/web/IntegrationRegistration.d.ts +53 -0
  228. package/lib/typescript/internal/adapter/web/IntegrationRegistration.d.ts.map +1 -0
  229. package/lib/typescript/internal/adapter/web/PlayerFacade.d.ts +26 -0
  230. package/lib/typescript/internal/adapter/web/PlayerFacade.d.ts.map +1 -0
  231. package/lib/typescript/internal/adapter/web/PlayerFacadeAPI.d.ts +33 -0
  232. package/lib/typescript/internal/adapter/web/PlayerFacadeAPI.d.ts.map +1 -0
  233. package/lib/typescript/internal/adapter/web/PlayerFacadeAdsEvents.d.ts +21 -0
  234. package/lib/typescript/internal/adapter/web/PlayerFacadeAdsEvents.d.ts.map +1 -0
  235. package/lib/typescript/internal/adapter/web/PlayerFacadeIntegration.d.ts +43 -0
  236. package/lib/typescript/internal/adapter/web/PlayerFacadeIntegration.d.ts.map +1 -0
  237. package/lib/typescript/internal/adapter/web/PlayerFacadeTypes.d.ts +13 -0
  238. package/lib/typescript/internal/adapter/web/PlayerFacadeTypes.d.ts.map +1 -0
  239. package/lib/typescript/internal/adapter/web/WebMediaSession.d.ts +10 -3
  240. package/lib/typescript/internal/adapter/web/WebMediaSession.d.ts.map +1 -1
  241. package/package.json +3 -3
  242. package/react-native-theoplayer.podspec +7 -7
  243. package/src/__tests__/PlayerFacade.test.ts +579 -0
  244. package/src/__tests__/PlayerFacadeWebAdapter.test.ts +193 -0
  245. package/src/__tests__/index.test.tsx +15 -1
  246. package/src/api/THEOplayerView.ts +5 -0
  247. package/src/api/barrel.ts +1 -0
  248. package/src/api/config/PlayerConfiguration.ts +16 -0
  249. package/src/api/event/AdEvent.ts +1 -1
  250. package/src/api/event/PlayerEvent.ts +10 -0
  251. package/src/api/media/MediaControlAPI.ts +33 -0
  252. package/src/api/media/MediaControlConfiguration.ts +5 -1
  253. package/src/api/media/barrel.ts +1 -0
  254. package/src/api/metrics/MetricsAPI.ts +20 -0
  255. package/src/api/metrics/barrel.ts +1 -0
  256. package/src/api/player/Integration.ts +97 -0
  257. package/src/api/player/PlayerEventMap.ts +7 -0
  258. package/src/api/player/THEOplayer.ts +27 -0
  259. package/src/api/player/barrel.ts +1 -0
  260. package/src/api/track/MediaTrack.ts +21 -4
  261. package/src/internal/THEOplayerView.tsx +10 -2
  262. package/src/internal/THEOplayerView.web.tsx +2 -2
  263. package/src/internal/adapter/DefaultNativePlayerState.ts +1 -0
  264. package/src/internal/adapter/NativePlayerState.ts +1 -0
  265. package/src/internal/adapter/THEOplayerAdapter.ts +33 -0
  266. package/src/internal/adapter/THEOplayerWebAdapter.ts +38 -12
  267. package/src/internal/adapter/WebEventForwarder.ts +12 -3
  268. package/src/internal/adapter/event/PlayerEvents.ts +10 -0
  269. package/src/internal/adapter/event/native/NativePlayerEvent.ts +2 -0
  270. package/src/internal/adapter/media/MediaControlNativeAdapter.ts +38 -0
  271. package/src/internal/adapter/media/MediaControlWebAdapter.ts +28 -0
  272. package/src/internal/adapter/metrics/MetricsNativeAdapter.ts +12 -0
  273. package/src/internal/adapter/metrics/MetricsWebAdapter.ts +14 -0
  274. package/src/internal/adapter/web/IntegrationRegistration.ts +57 -0
  275. package/src/internal/adapter/web/PlayerFacade.ts +281 -0
  276. package/src/internal/adapter/web/PlayerFacadeAPI.ts +35 -0
  277. package/src/internal/adapter/web/PlayerFacadeAdsEvents.ts +119 -0
  278. package/src/internal/adapter/web/PlayerFacadeIntegration.ts +45 -0
  279. package/src/internal/adapter/web/PlayerFacadeTypes.ts +14 -0
  280. package/src/internal/adapter/web/WebMediaSession.ts +85 -21
  281. package/src/manifest.json +1 -1
  282. package/android/src/main/java/com/theoplayer/media/MediaQueueNavigator.kt +0 -33
@@ -0,0 +1,376 @@
1
+ package com.theoplayer.media
2
+
3
+ import android.support.v4.media.session.PlaybackStateCompat
4
+ import com.theoplayer.android.api.event.EventListener
5
+ import com.theoplayer.android.api.event.ads.AdEvent
6
+ import com.theoplayer.android.api.event.ads.AdsEventTypes
7
+ import com.theoplayer.android.api.event.player.PlayerEvent
8
+ import com.theoplayer.android.api.event.player.PlayerEventTypes
9
+ import com.theoplayer.android.api.player.Player
10
+ import com.theoplayer.android.api.timerange.TimeRanges
11
+ import com.theoplayer.android.connector.mediasession.MediaSessionConnector
12
+ import com.theoplayer.android.connector.mediasession.PlaybackCallback
13
+ import com.theoplayer.android.connector.mediasession.QueueNavigator
14
+
15
+ typealias MediaControlHandler = () -> Unit
16
+
17
+ /**
18
+ * Notified whenever the set of media control actions the [MediaControlProxy] currently accepts changes.
19
+ */
20
+ fun interface AvailableActionsListener {
21
+ fun onAvailableActionsChanged()
22
+ }
23
+
24
+ /**
25
+ * A media control action that can be presented next to the play/pause action.
26
+ */
27
+ enum class MediaControlSlot {
28
+ SKIP_TO_PREVIOUS,
29
+ SKIP_TO_NEXT,
30
+ REWIND,
31
+ FAST_FORWARD
32
+ }
33
+
34
+ /**
35
+ * The media control actions that are currently available, in display order: an optional queue or
36
+ * trick-play action, the play/pause action and another optional queue or trick-play action.
37
+ *
38
+ * Consumers presenting their own media controls, such as the media notification and the PiP window,
39
+ * render these with their own action types instead of deciding themselves which actions apply.
40
+ */
41
+ data class MediaControlSlots(
42
+ val leading: MediaControlSlot?,
43
+ val playPauseEnabled: Boolean,
44
+ val trailing: MediaControlSlot?
45
+ )
46
+
47
+ private const val DEFAULT_ACTIVE_ITEM_ID = 0L
48
+
49
+ private const val AVAILABLE_QUEUE_ACTIONS = (PlaybackStateCompat.ACTION_SKIP_TO_NEXT or
50
+ PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
51
+
52
+ /**
53
+ * MediaControlProxy serves as a proxy for media control actions sent by the MediaSession. It allows
54
+ * external code to register handlers for specific media control actions, which will be invoked when the
55
+ * corresponding action is received from the MediaSession. If no handler is registered for a given action,
56
+ * the proxy will fall back to default behavior (e.g. calling player.play() for a PLAY action).
57
+ */
58
+ class MediaControlProxy : PlaybackCallback, QueueNavigator {
59
+
60
+ private var player: Player? = null
61
+ private var connector: MediaSessionConnector? = null
62
+ private var serviceBinder: MediaPlaybackService.MediaPlaybackBinder? = null
63
+ private var config: MediaSessionConfig = MediaSessionConfig()
64
+ private val handlers: MutableMap<MediaControlAction, MediaControlHandler> = mutableMapOf()
65
+ private val availableActionsListeners: MutableList<AvailableActionsListener> = mutableListOf()
66
+
67
+ // The availability of the playback actions changes when switching between content and ads, or when the
68
+ // stream turns out to be live.
69
+ private val onPlayerAction = EventListener<PlayerEvent<*>> { invalidateAvailableActions() }
70
+ private val onAdAction = EventListener<AdEvent<*>> { invalidateAvailableActions() }
71
+ private val playerEvents = listOf(PlayerEventTypes.LOADEDMETADATA)
72
+
73
+ // Use the integration-agnostic ad break events, so client-side (IMA), server-side (SSAI, THEOads) and
74
+ // custom ad integrations are all covered.
75
+ private val adEvents = listOf(AdsEventTypes.AD_BREAK_BEGIN, AdsEventTypes.AD_BREAK_END)
76
+
77
+ /**
78
+ * Sets the player whose state determines which actions are currently available. The proxy listens for
79
+ * the events that change that availability, independently of the media session it is attached to.
80
+ */
81
+ fun setPlayer(player: Player?) {
82
+ this.player?.let { current ->
83
+ playerEvents.forEach { current.removeEventListener(it, onPlayerAction) }
84
+ adEvents.forEach { current.ads.removeEventListener(it, onAdAction) }
85
+ }
86
+ this.player = player
87
+ player?.let { current ->
88
+ playerEvents.forEach { current.addEventListener(it, onPlayerAction) }
89
+ adEvents.forEach { current.ads.addEventListener(it, onAdAction) }
90
+ }
91
+ invalidateAvailableActions()
92
+ }
93
+
94
+ /**
95
+ * Routes the media session's playback and queue actions through this proxy.
96
+ */
97
+ fun attach(
98
+ connector: MediaSessionConnector,
99
+ serviceBinder: MediaPlaybackService.MediaPlaybackBinder?,
100
+ config: MediaSessionConfig
101
+ ) {
102
+ // Stop routing actions through any previously attached media session.
103
+ detach()
104
+
105
+ this.connector = connector
106
+ this.serviceBinder = serviceBinder
107
+ this.config = config
108
+
109
+ connector.apply {
110
+ queueNavigator = this@MediaControlProxy
111
+ // All playback actions are routed through this proxy
112
+ playbackCallback = this@MediaControlProxy
113
+ }
114
+
115
+ applyConfig()
116
+ }
117
+
118
+ /**
119
+ * Sets the media session configuration, which decides whether media controls are enabled at all and
120
+ * how the proxy handles the actions it accepts. The proxy needs it even while there is no media
121
+ * session to attach to, as it also drives the PiP controls.
122
+ */
123
+ fun setConfig(config: MediaSessionConfig) {
124
+ this.config = config
125
+ applyConfig()
126
+ }
127
+
128
+ private fun applyConfig() {
129
+ connector?.apply {
130
+ skipForwardInterval = config.skipForwardInterval
131
+ skipBackwardsInterval = config.skipBackwardInterval
132
+ }
133
+ invalidateAvailableActions()
134
+ }
135
+
136
+ private fun detach() {
137
+ connector?.apply {
138
+ queueNavigator = null
139
+ playbackCallback = null
140
+ }
141
+ connector = null
142
+ serviceBinder = null
143
+ }
144
+
145
+ fun destroy() {
146
+ detach()
147
+ setPlayer(null)
148
+ availableActionsListeners.clear()
149
+ }
150
+
151
+ /**
152
+ * Adds a listener that is notified whenever the set of available actions changes, for consumers that
153
+ * present their own media controls, such as the PiP window.
154
+ */
155
+ fun addAvailableActionsListener(listener: AvailableActionsListener) {
156
+ availableActionsListeners.add(listener)
157
+ }
158
+
159
+ fun removeAvailableActionsListener(listener: AvailableActionsListener) {
160
+ availableActionsListeners.remove(listener)
161
+ }
162
+
163
+ /**
164
+ * The playback actions the MediaSession should advertise, based on the actions the proxy currently
165
+ * accepts. Queue actions are not included here: those are reported through [QueueNavigator].
166
+ */
167
+ private val enabledPlaybackActions: Long
168
+ get() {
169
+ var actions = PlaybackStateCompat.ACTION_STOP or PlaybackStateCompat.ACTION_SET_PLAYBACK_SPEED
170
+ if (playPauseEnabled) {
171
+ actions = actions or PlaybackStateCompat.ACTION_PLAY_PAUSE or
172
+ PlaybackStateCompat.ACTION_PLAY or
173
+ PlaybackStateCompat.ACTION_PAUSE
174
+ }
175
+ if (trickPlayEnabled) {
176
+ actions = actions or PlaybackStateCompat.ACTION_SEEK_TO or
177
+ PlaybackStateCompat.ACTION_FAST_FORWARD or
178
+ PlaybackStateCompat.ACTION_REWIND
179
+ }
180
+ return actions
181
+ }
182
+
183
+ /**
184
+ * Reports the currently available actions to the MediaSession, the media notification and any
185
+ * registered [AvailableActionsListener].
186
+ */
187
+ private fun invalidateAvailableActions() {
188
+ connector?.apply {
189
+ enabledPlaybackActions = this@MediaControlProxy.enabledPlaybackActions
190
+ invalidatePlaybackState()
191
+ }
192
+ serviceBinder?.updateNotification()
193
+ availableActionsListeners.forEach { it.onAvailableActionsChanged() }
194
+ }
195
+
196
+ fun setHandler(action: MediaControlAction, handler: MediaControlHandler) {
197
+ handlers[action] = handler
198
+
199
+ // Make sure the MediaSession and notification know about the updated set of supported actions.
200
+ invalidateAvailableActions()
201
+ }
202
+
203
+ fun removeHandler(action: MediaControlAction) {
204
+ handlers.remove(action)
205
+
206
+ // Make sure the MediaSession and notification know about the updated set of supported actions.
207
+ invalidateAvailableActions()
208
+ }
209
+
210
+ fun invokeHandler(action: MediaControlAction): Boolean {
211
+ if (!handlers.containsKey(action)) {
212
+ return false
213
+ }
214
+ handlers[action]?.invoke()
215
+ return true
216
+ }
217
+
218
+ fun hasHandler(action: MediaControlAction): Boolean {
219
+ return handlers.containsKey(action)
220
+ }
221
+
222
+ /**
223
+ * Whether media controls should be presented at all for this player.
224
+ */
225
+ val mediaControlsEnabled: Boolean
226
+ get() = config.mediaSessionEnabled
227
+
228
+ val queueActionsEnabled: Boolean
229
+ get() = hasHandler(MediaControlAction.SKIP_TO_NEXT) ||
230
+ hasHandler(MediaControlAction.SKIP_TO_PREVIOUS) || config.convertSkipToSeek
231
+
232
+ val trickPlayEnabled: Boolean
233
+ get() = !isInAd && !isLive
234
+
235
+ val playPauseEnabled: Boolean
236
+ get() = !isInAd && (!isLive || config.allowLivePlayPause)
237
+
238
+ /**
239
+ * The media controls that are currently available, for consumers that present their own controls.
240
+ * Queue actions take precedence over the trick-play actions.
241
+ */
242
+ val controlSlots: MediaControlSlots
243
+ get() = MediaControlSlots(
244
+ leading = when {
245
+ queueActionsEnabled -> MediaControlSlot.SKIP_TO_PREVIOUS
246
+ trickPlayEnabled -> MediaControlSlot.REWIND
247
+ else -> null
248
+ },
249
+ playPauseEnabled = playPauseEnabled,
250
+ trailing = when {
251
+ queueActionsEnabled -> MediaControlSlot.SKIP_TO_NEXT
252
+ trickPlayEnabled -> MediaControlSlot.FAST_FORWARD
253
+ else -> null
254
+ }
255
+ )
256
+
257
+ override fun onPlay() {
258
+ // Make sure the session is currently active and ready to receive commands, but only if enabled.
259
+ if (mediaControlsEnabled) {
260
+ connector?.setActive(true)
261
+ }
262
+
263
+ // Don't allow play actions during ads, or on live streams if not configured to allow it.
264
+ if (!playPauseEnabled) return
265
+
266
+ // Check if an external handler is registered for the PLAY keycode, and invoke it if so
267
+ if (invokeHandler(MediaControlAction.PLAY)) return
268
+
269
+ player?.play()
270
+
271
+ // Optionally seek to live, if configured.
272
+ if (config.seekToLiveOnResume && isLive) {
273
+ player?.currentTime = Double.POSITIVE_INFINITY
274
+ }
275
+ }
276
+
277
+ override fun onPause() {
278
+ // Don't allow pause actions during ads, or on live streams if not configured to allow it.
279
+ if (!playPauseEnabled) return
280
+
281
+ // Check if an external handler is registered for the PAUSE keycode, and invoke it if so
282
+ if (invokeHandler(MediaControlAction.PAUSE)) return
283
+
284
+ player?.pause()
285
+ }
286
+
287
+ override fun onStop() {
288
+ serviceBinder?.stopForegroundService()
289
+ }
290
+
291
+ override fun onFastForward() {
292
+ // Don't allow skip actions during ads, or on live streams.
293
+ if (!trickPlayEnabled) return
294
+
295
+ skip(connector?.skipForwardInterval ?: 0.0)
296
+ }
297
+
298
+ override fun onRewind() {
299
+ // Don't allow skip actions during ads, or on live streams.
300
+ if (!trickPlayEnabled) return
301
+
302
+ skip(-(connector?.skipBackwardsInterval ?: 0.0))
303
+ }
304
+
305
+ override fun onSetPlaybackSpeed(speed: Float) {
306
+ player?.playbackRate = speed.toDouble()
307
+ }
308
+
309
+ override fun onSeekTo(positionMs: Long) {
310
+ // Don't allow seek actions during ads, or on live streams.
311
+ if (!trickPlayEnabled) return
312
+
313
+ player?.currentTime = 1e-03 * positionMs
314
+ }
315
+
316
+ private fun skip(skipTime: Double) {
317
+ val player = this.player ?: return
318
+
319
+ val currentTime: Double = player.currentTime
320
+ val seekable: TimeRanges = player.seekable
321
+ if (java.lang.Double.isNaN(currentTime) || seekable.length() == 0) {
322
+ return
323
+ }
324
+ for (i in 0 until seekable.length()) {
325
+ if (seekable.getStart(i) <= currentTime && seekable.getEnd(i) >= currentTime) {
326
+ player.currentTime = seekable.getEnd(i)
327
+ .coerceAtMost(seekable.getStart(i).coerceAtLeast(currentTime + skipTime))
328
+ }
329
+ }
330
+ }
331
+
332
+ private val isLive: Boolean
333
+ get() = player?.duration?.isInfinite() == true
334
+
335
+ private val isInAd: Boolean
336
+ get() = player?.ads?.isPlaying == true
337
+
338
+ override fun getSupportedQueueNavigatorActions(player: Player): Long {
339
+ return if (queueActionsEnabled) {
340
+ AVAILABLE_QUEUE_ACTIONS
341
+ } else {
342
+ 0L
343
+ }
344
+ }
345
+
346
+ override fun getActiveQueueItemId(player: Player): Long {
347
+ return DEFAULT_ACTIVE_ITEM_ID
348
+ }
349
+
350
+ override fun onSkipToPrevious(player: Player) {
351
+ // Check if an external handler is registered for the MEDIA_NEXT keycode, and invoke it if so
352
+ if (invokeHandler(MediaControlAction.SKIP_TO_PREVIOUS)) return
353
+
354
+ // Check if we need to treat a MEDIA_PREVIOUS keycode as a MEDIA_REWIND
355
+ if (config.convertSkipToSeek) {
356
+ player.currentTime -= config.skipBackwardInterval
357
+ }
358
+ }
359
+
360
+ override fun onSkipToQueueItem(
361
+ player: Player,
362
+ id: Long
363
+ ) {
364
+ // Unsupported action
365
+ }
366
+
367
+ override fun onSkipToNext(player: Player) {
368
+ // Check if an external handler is registered for the MEDIA_NEXT keycode, and invoke it if so
369
+ if (invokeHandler(MediaControlAction.SKIP_TO_NEXT)) return
370
+
371
+ // Otherwise default logic: Check if we need to treat a MEDIA_NEXT keycode as a MEDIA_FAST_FORWARD
372
+ if (config.convertSkipToSeek) {
373
+ player.currentTime += config.skipForwardInterval
374
+ }
375
+ }
376
+ }
@@ -28,6 +28,14 @@ import com.theoplayer.android.connector.mediasession.MediaSessionConnector
28
28
 
29
29
  private const val TAG = "MediaNotification"
30
30
 
31
+ // The controls to show while there is no player context to ask, matching a player that is not in an ad
32
+ // and not live.
33
+ private val DEFAULT_CONTROL_SLOTS = MediaControlSlots(
34
+ leading = MediaControlSlot.REWIND,
35
+ playPauseEnabled = true,
36
+ trailing = MediaControlSlot.FAST_FORWARD
37
+ )
38
+
31
39
  class MediaNotificationBuilder(
32
40
  private val context: Context,
33
41
  private val notificationManager: NotificationManager,
@@ -86,12 +94,62 @@ class MediaNotificationBuilder(
86
94
  )
87
95
  )
88
96
 
97
+ private val skipToPreviousAction = NotificationCompat.Action(
98
+ R.drawable.ic_prev, context.getString(R.string.skip_to_previous),
99
+ MediaButtonReceiver.buildMediaButtonPendingIntent(
100
+ context,
101
+ PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
102
+ )
103
+ )
104
+
105
+ private val skipToNextAction = NotificationCompat.Action(
106
+ R.drawable.ic_next, context.getString(R.string.skip_to_next),
107
+ MediaButtonReceiver.buildMediaButtonPendingIntent(
108
+ context,
109
+ PlaybackStateCompat.ACTION_SKIP_TO_NEXT
110
+ )
111
+ )
112
+
113
+ // Placeholder action, used when no media controls should be shown, as clearActions() does not work.
114
+ private val placeholderAction = NotificationCompat.Action(0, null, null)
115
+
116
+ /**
117
+ * Builds the notification actions for the media controls that the proxy reports as available.
118
+ */
119
+ private fun buildActions(
120
+ @PlaybackStateCompat.State playbackState: Int,
121
+ slots: MediaControlSlots
122
+ ): List<NotificationCompat.Action> {
123
+ val actions = listOfNotNull(
124
+ slots.leading?.let { notificationAction(it) },
125
+ if (slots.playPauseEnabled) playPauseAction(playbackState) else null,
126
+ slots.trailing?.let { notificationAction(it) }
127
+ )
128
+ // Add an empty placeholder action if there are none, as clearActions() does not work.
129
+ return actions.ifEmpty { listOf(placeholderAction) }
130
+ }
131
+
132
+ private fun notificationAction(slot: MediaControlSlot): NotificationCompat.Action {
133
+ return when (slot) {
134
+ MediaControlSlot.SKIP_TO_PREVIOUS -> skipToPreviousAction
135
+ MediaControlSlot.SKIP_TO_NEXT -> skipToNextAction
136
+ MediaControlSlot.REWIND -> rewindAction
137
+ MediaControlSlot.FAST_FORWARD -> forwardAction
138
+ }
139
+ }
140
+
141
+ private fun playPauseAction(@PlaybackStateCompat.State playbackState: Int): NotificationCompat.Action? {
142
+ return when (playbackState) {
143
+ PlaybackStateCompat.STATE_PAUSED -> playAction
144
+ PlaybackStateCompat.STATE_PLAYING -> pauseAction
145
+ else -> null
146
+ }
147
+ }
148
+
89
149
  fun build(
90
150
  @PlaybackStateCompat.State playbackState: Int,
91
151
  largeIcon: Bitmap?,
92
- enableMediaControls: Boolean = true,
93
- enableContentIntent: Boolean = true,
94
- enableCancelButton: Boolean = true
152
+ mediaControlProxy: MediaControlProxy?
95
153
  ): Notification {
96
154
  val builder = channelId.let {
97
155
  if (it != null) {
@@ -114,9 +172,7 @@ class MediaNotificationBuilder(
114
172
  }
115
173
 
116
174
  // Enable launching the session activity by clicking the notification
117
- if (enableContentIntent) {
118
- setContentIntent(mediaSession.controller.sessionActivity)
119
- }
175
+ setContentIntent(mediaSession.controller.sessionActivity)
120
176
 
121
177
  // Stop the service when the notification is swiped to dismiss
122
178
  setDeleteIntent(
@@ -157,19 +213,14 @@ class MediaNotificationBuilder(
157
213
  // on the eyes and avoid extremely bright or fluorescent colors.
158
214
  color = ContextCompat.getColor(context, R.color.app_primary_color)
159
215
 
160
- // Add play/pause, rewind and fast-forward buttons.
161
- if (enableMediaControls) {
162
- addAction(rewindAction)
163
- if (playbackState == PlaybackStateCompat.STATE_PAUSED) {
164
- addAction(playAction)
165
- } else if (playbackState == PlaybackStateCompat.STATE_PLAYING) {
166
- addAction(pauseAction)
167
- }
168
- addAction(forwardAction)
216
+ // Add the play/pause and queue or trick-play buttons.
217
+ val actions = if (mediaControlProxy?.mediaControlsEnabled != false) {
218
+ buildActions(playbackState, mediaControlProxy?.controlSlots ?: DEFAULT_CONTROL_SLOTS)
169
219
  } else {
170
220
  // Add empty placeholder action as clearActions() does not work.
171
- addAction(0, null, null)
221
+ listOf(placeholderAction)
172
222
  }
223
+ actions.forEach { addAction(it) }
173
224
 
174
225
  // Take advantage of MediaStyle features
175
226
  val style = androidx.media.app.NotificationCompat.MediaStyle()
@@ -178,26 +229,19 @@ class MediaNotificationBuilder(
178
229
  // companion devices to access and control the session.
179
230
  style.setMediaSession(mediaSession.sessionToken)
180
231
 
181
- // Add up to 3 actions to be shown in the notification's standard-sized contentView.
182
- if (enableMediaControls) {
183
- // The Rewind, Play/Pause and FastForward actions.
184
- style.setShowActionsInCompactView(0, 1, 2)
185
- } else {
186
- // The placeholder action, which was added above.
187
- style.setShowActionsInCompactView(0)
188
- }
232
+ // Show all actions added above in the notification's standard-sized contentView. There are never
233
+ // more than the 3 actions that fit: a play/pause action with a queue or trick-play action on each side.
234
+ style.setShowActionsInCompactView(*IntArray(actions.size) { it })
189
235
 
190
- if (enableCancelButton) {
191
- // In Android 5.0 (API level 21) and later you can swipe away a notification to
192
- // stop the player once the service is no longer running in the foreground.
193
- style.setShowCancelButton(true)
194
- style.setCancelButtonIntent(
195
- MediaButtonReceiver.buildMediaButtonPendingIntent(
196
- context,
197
- PlaybackStateCompat.ACTION_STOP
198
- )
236
+ // In Android 5.0 (API level 21) and later you can swipe away a notification to
237
+ // stop the player once the service is no longer running in the foreground.
238
+ style.setShowCancelButton(true)
239
+ style.setCancelButtonIntent(
240
+ MediaButtonReceiver.buildMediaButtonPendingIntent(
241
+ context,
242
+ PlaybackStateCompat.ACTION_STOP
199
243
  )
200
- }
244
+ )
201
245
  setStyle(style)
202
246
  }
203
247
  return builder.build()
@@ -15,6 +15,7 @@ import androidx.core.content.ContextCompat
15
15
  import androidx.media.session.MediaButtonReceiver
16
16
  import com.theoplayer.ReactTHEOplayerContext
17
17
  import com.theoplayer.android.api.player.Player
18
+ import com.theoplayer.android.api.source.SourceDescription
18
19
  import com.theoplayer.android.connector.mediasession.MediaSessionConnector
19
20
 
20
21
  private const val STOP_SERVICE_IF_APP_REMOVED = true
@@ -30,7 +31,11 @@ class MediaPlaybackService : Service() {
30
31
 
31
32
  private var playerContext: ReactTHEOplayerContext? = null
32
33
 
33
- private var mediaSessionConfig: MediaSessionConfig = MediaSessionConfig()
34
+ // The artwork is cached per source: the notification is rebuilt on every state or available action
35
+ // change, and fetching it again each time would make the artwork flicker.
36
+ private var largeIconSource: SourceDescription? = null
37
+ private var largeIcon: Bitmap? = null
38
+ private val placeholderIcon: Bitmap? by lazy { loadPlaceHolderIcon(this) }
34
39
 
35
40
  private val player: Player?
36
41
  get() = playerContext?.player
@@ -50,9 +55,8 @@ class MediaPlaybackService : Service() {
50
55
  service.connectPlayerContext(playerContext)
51
56
  }
52
57
 
53
- fun setEnablePlaybackControls(newConfig: MediaSessionConfig) {
54
- mediaSessionConfig = newConfig
55
- updateNotification()
58
+ fun isActivePlayerContext(playerContext: ReactTHEOplayerContext): Boolean {
59
+ return service.isActivePlayerContext(playerContext)
56
60
  }
57
61
 
58
62
  fun updateNotification() {
@@ -157,6 +161,10 @@ class MediaPlaybackService : Service() {
157
161
  updateNotification()
158
162
  }
159
163
 
164
+ private fun isActivePlayerContext(playerContext: ReactTHEOplayerContext): Boolean {
165
+ return this.playerContext === playerContext
166
+ }
167
+
160
168
  private fun updateNotification() {
161
169
  val player = player
162
170
  when {
@@ -172,16 +180,8 @@ class MediaPlaybackService : Service() {
172
180
  // not be killed if the system is low on memory.
173
181
  when (playbackState) {
174
182
  PlaybackStateCompat.STATE_PAUSED -> {
175
- // Fetch large icon asynchronously
176
- fetchImageFromMetadata(player?.source) { largeIcon ->
177
- notificationManager.notify(
178
- NOTIFICATION_ID,
179
- notificationBuilder.build(
180
- playbackState,
181
- largeIcon,
182
- mediaSessionConfig.mediaSessionEnabled
183
- )
184
- )
183
+ withLargeIcon { largeIcon ->
184
+ notificationManager.notify(NOTIFICATION_ID, buildNotification(playbackState, largeIcon))
185
185
  }
186
186
  }
187
187
 
@@ -190,11 +190,9 @@ class MediaPlaybackService : Service() {
190
190
  // with one or more transport controls. The notification should also include useful
191
191
  // information from the session's metadata.
192
192
 
193
- // Get the foreground service started in time before fetching an icon.
194
- startForegroundWithPlaybackState(playbackState, loadPlaceHolderIcon(this))
195
-
196
- // Fetch the correct large icon asynchronously.
197
- fetchImageFromMetadata(player?.source) { largeIcon ->
193
+ // Get the foreground service started in time, with a placeholder icon if the artwork for this
194
+ // source still has to be fetched.
195
+ withLargeIcon(showPlaceholderWhileFetching = true) { largeIcon ->
198
196
  startForegroundWithPlaybackState(playbackState, largeIcon)
199
197
  }
200
198
  }
@@ -213,6 +211,41 @@ class MediaPlaybackService : Service() {
213
211
  }
214
212
  }
215
213
 
214
+ /**
215
+ * Passes the artwork for the current source to [block], fetching it only once per source.
216
+ *
217
+ * When [showPlaceholderWhileFetching] is set, [block] is called synchronously with a placeholder
218
+ * first, so the foreground service can be started in time.
219
+ */
220
+ private fun withLargeIcon(
221
+ showPlaceholderWhileFetching: Boolean = false,
222
+ block: (Bitmap?) -> Unit
223
+ ) {
224
+ val source = player?.source
225
+ if (source != null && source == largeIconSource) {
226
+ block(largeIcon)
227
+ return
228
+ }
229
+ if (showPlaceholderWhileFetching) {
230
+ block(placeholderIcon)
231
+ }
232
+ fetchImageFromMetadata(source) { icon ->
233
+ largeIconSource = source
234
+ largeIcon = icon
235
+ block(icon)
236
+ }
237
+ }
238
+
239
+ /**
240
+ * Builds the media notification for the player context that currently owns the media session.
241
+ */
242
+ private fun buildNotification(
243
+ @PlaybackStateCompat.State playbackState: Int,
244
+ largeIcon: Bitmap? = null
245
+ ): Notification {
246
+ return notificationBuilder.build(playbackState, largeIcon, playerContext?.mediaControlProxy)
247
+ }
248
+
216
249
  private fun startForegroundWithPlaybackState(
217
250
  @PlaybackStateCompat.State playbackState: Int,
218
251
  largeIcon: Bitmap? = null
@@ -221,7 +254,7 @@ class MediaPlaybackService : Service() {
221
254
  ServiceCompat.startForeground(
222
255
  this,
223
256
  NOTIFICATION_ID,
224
- notificationBuilder.build(playbackState, largeIcon, mediaSessionConfig.mediaSessionEnabled),
257
+ buildNotification(playbackState, largeIcon),
225
258
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
226
259
  ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
227
260
  else 0
@@ -4,30 +4,30 @@ data class MediaSessionConfig (
4
4
  /**
5
5
  * Whether or not the media session should be enabled.
6
6
  */
7
- var mediaSessionEnabled: Boolean = true,
7
+ val mediaSessionEnabled: Boolean = true,
8
8
 
9
9
  /**
10
10
  * The amount of seconds the player will skip forward.
11
11
  */
12
- var skipForwardInterval: Double = 5.0,
12
+ val skipForwardInterval: Double = 5.0,
13
13
 
14
14
  /**
15
15
  * The amount of seconds the player will skip backward.
16
16
  */
17
- var skipBackwardInterval: Double = 5.0,
17
+ val skipBackwardInterval: Double = 5.0,
18
18
 
19
19
  /**
20
20
  * Whether "skip track" events should be handled the same as "fast-forward/rewind".
21
21
  */
22
- var convertSkipToSeek: Boolean = false,
22
+ val convertSkipToSeek: Boolean = false,
23
23
 
24
24
  /**
25
25
  * Whether to allow play/pause of live assets.
26
26
  */
27
- var allowLivePlayPause: Boolean = false,
27
+ val allowLivePlayPause: Boolean = false,
28
28
 
29
29
  /**
30
30
  * Whether to seek to live when resuming a live stream.
31
31
  */
32
- var seekToLiveOnResume: Boolean = false,
32
+ val seekToLiveOnResume: Boolean = false,
33
33
  )