react-native-theoplayer 11.5.0 → 11.6.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.
- package/CHANGELOG.md +12 -1
- package/README.md +1 -0
- package/android/src/main/java/com/theoplayer/PlayerEventEmitter.kt +9 -0
- package/android/src/main/java/com/theoplayer/ReactTHEOplayerContext.kt +58 -85
- package/android/src/main/java/com/theoplayer/ReactTHEOplayerPackage.kt +3 -0
- package/android/src/main/java/com/theoplayer/media/MediaControlModule.kt +102 -0
- package/android/src/main/java/com/theoplayer/media/MediaControlProxy.kt +376 -0
- package/android/src/main/java/com/theoplayer/media/MediaNotificationBuilder.kt +78 -34
- package/android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt +53 -20
- package/android/src/main/java/com/theoplayer/media/MediaSessionConfig.kt +6 -6
- package/android/src/main/java/com/theoplayer/media/MediaSessionConfigAdapter.kt +23 -20
- package/android/src/main/java/com/theoplayer/player/PlayerModule.kt +0 -1
- package/android/src/main/java/com/theoplayer/presentation/PipUtils.kt +98 -93
- package/android/src/main/java/com/theoplayer/util/ReadableExtension.kt +14 -0
- package/android/src/main/res/drawable/ic_next.xml +9 -0
- package/android/src/main/res/drawable/ic_prev.xml +9 -0
- package/android/src/main/res/values/strings.xml +4 -0
- package/ios/THEOplayerRCTBridge.m +16 -0
- package/ios/THEOplayerRCTDebug.swift +3 -0
- package/ios/THEOplayerRCTMainEventHandler.swift +17 -0
- package/ios/THEOplayerRCTTypeUtils.swift +30 -0
- package/ios/THEOplayerRCTView.swift +15 -5
- package/ios/backgroundAudio/THEOplayerRCTBackgroundAudioManager.swift +1 -1
- package/ios/backgroundAudio/THEOplayerRCTNowPlayingManager.swift +49 -3
- package/ios/backgroundAudio/THEOplayerRCTRemoteCommandsManager.swift +151 -68
- package/ios/backgroundAudio/THEOplayerRCTView+MediaControlConfig.swift +25 -11
- package/ios/mediaControl/THEOplayerRCTMediaControlAPI.swift +79 -0
- package/ios/mediaControl/THEOplayerRCTMediaControlManager.swift +38 -0
- package/ios/pip/THEOplayerRCTPipControlsManager.swift +9 -12
- package/lib/commonjs/api/event/PlayerEvent.js +8 -0
- package/lib/commonjs/api/event/PlayerEvent.js.map +1 -1
- package/lib/commonjs/api/media/MediaControlAPI.js +14 -0
- package/lib/commonjs/api/media/MediaControlAPI.js.map +1 -0
- package/lib/commonjs/api/media/barrel.js +11 -0
- package/lib/commonjs/api/media/barrel.js.map +1 -1
- package/lib/commonjs/api/player/PlayerEventMap.js +1 -0
- package/lib/commonjs/api/player/PlayerEventMap.js.map +1 -1
- package/lib/commonjs/api/player/THEOplayer.js.map +1 -1
- package/lib/commonjs/internal/THEOplayerView.js +4 -0
- package/lib/commonjs/internal/THEOplayerView.js.map +1 -1
- package/lib/commonjs/internal/adapter/THEOplayerAdapter.js +6 -0
- package/lib/commonjs/internal/adapter/THEOplayerAdapter.js.map +1 -1
- package/lib/commonjs/internal/adapter/THEOplayerWebAdapter.js +9 -9
- package/lib/commonjs/internal/adapter/THEOplayerWebAdapter.js.map +1 -1
- package/lib/commonjs/internal/adapter/WebEventForwarder.js +5 -0
- package/lib/commonjs/internal/adapter/WebEventForwarder.js.map +1 -1
- package/lib/commonjs/internal/adapter/event/PlayerEvents.js +7 -1
- package/lib/commonjs/internal/adapter/event/PlayerEvents.js.map +1 -1
- package/lib/commonjs/internal/adapter/media/MediaControlNativeAdapter.js +42 -0
- package/lib/commonjs/internal/adapter/media/MediaControlNativeAdapter.js.map +1 -0
- package/lib/commonjs/internal/adapter/media/MediaControlWebAdapter.js +32 -0
- package/lib/commonjs/internal/adapter/media/MediaControlWebAdapter.js.map +1 -0
- package/lib/commonjs/internal/adapter/web/WebMediaSession.js +78 -16
- package/lib/commonjs/internal/adapter/web/WebMediaSession.js.map +1 -1
- package/lib/commonjs/manifest.json +1 -1
- package/lib/module/api/event/PlayerEvent.js +9 -0
- package/lib/module/api/event/PlayerEvent.js.map +1 -1
- package/lib/module/api/media/MediaControlAPI.js +10 -0
- package/lib/module/api/media/MediaControlAPI.js.map +1 -0
- package/lib/module/api/media/barrel.js +1 -0
- package/lib/module/api/media/barrel.js.map +1 -1
- package/lib/module/api/player/PlayerEventMap.js +1 -0
- package/lib/module/api/player/PlayerEventMap.js.map +1 -1
- package/lib/module/api/player/THEOplayer.js.map +1 -1
- package/lib/module/internal/THEOplayerView.js +5 -1
- package/lib/module/internal/THEOplayerView.js.map +1 -1
- package/lib/module/internal/adapter/THEOplayerAdapter.js +6 -0
- package/lib/module/internal/adapter/THEOplayerAdapter.js.map +1 -1
- package/lib/module/internal/adapter/THEOplayerWebAdapter.js +9 -9
- package/lib/module/internal/adapter/THEOplayerWebAdapter.js.map +1 -1
- package/lib/module/internal/adapter/WebEventForwarder.js +6 -1
- package/lib/module/internal/adapter/WebEventForwarder.js.map +1 -1
- package/lib/module/internal/adapter/event/PlayerEvents.js +5 -0
- package/lib/module/internal/adapter/event/PlayerEvents.js.map +1 -1
- package/lib/module/internal/adapter/media/MediaControlNativeAdapter.js +37 -0
- package/lib/module/internal/adapter/media/MediaControlNativeAdapter.js.map +1 -0
- package/lib/module/internal/adapter/media/MediaControlWebAdapter.js +27 -0
- package/lib/module/internal/adapter/media/MediaControlWebAdapter.js.map +1 -0
- package/lib/module/internal/adapter/web/WebMediaSession.js +78 -16
- package/lib/module/internal/adapter/web/WebMediaSession.js.map +1 -1
- package/lib/module/manifest.json +1 -1
- package/lib/typescript/api/event/PlayerEvent.d.ts +9 -0
- package/lib/typescript/api/event/PlayerEvent.d.ts.map +1 -1
- package/lib/typescript/api/media/MediaControlAPI.d.ts +30 -0
- package/lib/typescript/api/media/MediaControlAPI.d.ts.map +1 -0
- package/lib/typescript/api/media/MediaControlConfiguration.d.ts +5 -1
- package/lib/typescript/api/media/MediaControlConfiguration.d.ts.map +1 -1
- package/lib/typescript/api/media/barrel.d.ts +1 -0
- package/lib/typescript/api/media/barrel.d.ts.map +1 -1
- package/lib/typescript/api/player/PlayerEventMap.d.ts +7 -2
- package/lib/typescript/api/player/PlayerEventMap.d.ts.map +1 -1
- package/lib/typescript/api/player/THEOplayer.d.ts +5 -0
- package/lib/typescript/api/player/THEOplayer.d.ts.map +1 -1
- package/lib/typescript/internal/THEOplayerView.d.ts +1 -0
- package/lib/typescript/internal/THEOplayerView.d.ts.map +1 -1
- package/lib/typescript/internal/adapter/THEOplayerAdapter.d.ts +3 -1
- package/lib/typescript/internal/adapter/THEOplayerAdapter.d.ts.map +1 -1
- package/lib/typescript/internal/adapter/THEOplayerWebAdapter.d.ts +3 -2
- package/lib/typescript/internal/adapter/THEOplayerWebAdapter.d.ts.map +1 -1
- package/lib/typescript/internal/adapter/WebEventForwarder.d.ts +1 -0
- package/lib/typescript/internal/adapter/WebEventForwarder.d.ts.map +1 -1
- package/lib/typescript/internal/adapter/event/PlayerEvents.d.ts +4 -1
- package/lib/typescript/internal/adapter/event/PlayerEvents.d.ts.map +1 -1
- package/lib/typescript/internal/adapter/event/native/NativePlayerEvent.d.ts +1 -0
- package/lib/typescript/internal/adapter/event/native/NativePlayerEvent.d.ts.map +1 -1
- package/lib/typescript/internal/adapter/media/MediaControlNativeAdapter.d.ts +12 -0
- package/lib/typescript/internal/adapter/media/MediaControlNativeAdapter.d.ts.map +1 -0
- package/lib/typescript/internal/adapter/media/MediaControlWebAdapter.d.ts +14 -0
- package/lib/typescript/internal/adapter/media/MediaControlWebAdapter.d.ts.map +1 -0
- package/lib/typescript/internal/adapter/web/WebMediaSession.d.ts +10 -3
- package/lib/typescript/internal/adapter/web/WebMediaSession.d.ts.map +1 -1
- package/package.json +3 -3
- package/react-native-theoplayer.podspec +1 -1
- package/src/api/event/PlayerEvent.ts +10 -0
- package/src/api/media/MediaControlAPI.ts +33 -0
- package/src/api/media/MediaControlConfiguration.ts +5 -1
- package/src/api/media/barrel.ts +1 -0
- package/src/api/player/PlayerEventMap.ts +7 -0
- package/src/api/player/THEOplayer.ts +6 -0
- package/src/internal/THEOplayerView.tsx +8 -0
- package/src/internal/adapter/THEOplayerAdapter.ts +9 -0
- package/src/internal/adapter/THEOplayerWebAdapter.ts +12 -9
- package/src/internal/adapter/WebEventForwarder.ts +7 -0
- package/src/internal/adapter/event/PlayerEvents.ts +10 -0
- package/src/internal/adapter/event/native/NativePlayerEvent.ts +2 -0
- package/src/internal/adapter/media/MediaControlNativeAdapter.ts +38 -0
- package/src/internal/adapter/media/MediaControlWebAdapter.ts +28 -0
- package/src/internal/adapter/web/WebMediaSession.ts +85 -21
- package/src/manifest.json +1 -1
- 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
|
-
|
|
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
|
-
|
|
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
|
|
161
|
-
if (
|
|
162
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
182
|
-
|
|
183
|
-
|
|
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
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
-
|
|
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
|
|
54
|
-
|
|
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
|
-
|
|
176
|
-
|
|
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
|
|
194
|
-
|
|
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
|
-
|
|
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
|
-
|
|
7
|
+
val mediaSessionEnabled: Boolean = true,
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* The amount of seconds the player will skip forward.
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
val skipForwardInterval: Double = 5.0,
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* The amount of seconds the player will skip backward.
|
|
16
16
|
*/
|
|
17
|
-
|
|
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
|
-
|
|
22
|
+
val convertSkipToSeek: Boolean = false,
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* Whether to allow play/pause of live assets.
|
|
26
26
|
*/
|
|
27
|
-
|
|
27
|
+
val allowLivePlayPause: Boolean = false,
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Whether to seek to live when resuming a live stream.
|
|
31
31
|
*/
|
|
32
|
-
|
|
32
|
+
val seekToLiveOnResume: Boolean = false,
|
|
33
33
|
)
|