react-native-theoplayer 2.2.0 → 2.3.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 CHANGED
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [2.3.0] - 23-04-14
9
+
10
+ ### Changed
11
+
12
+ - Updated picture-in-picture controls on Android to include forward/rewind buttons and disabled pause button for ads.
13
+
8
14
  ## [2.2.0] - 23-04-12
9
15
 
10
16
  ### Fixed
@@ -0,0 +1,244 @@
1
+ package com.theoplayer.presentation
2
+
3
+ import android.app.PendingIntent
4
+ import android.app.PictureInPictureParams
5
+ import android.app.RemoteAction
6
+ import android.content.BroadcastReceiver
7
+ import android.content.Context
8
+ import android.content.Intent
9
+ import android.content.IntentFilter
10
+ import android.graphics.Rect
11
+ import android.graphics.drawable.Icon
12
+ import android.os.Build
13
+ import android.util.Rational
14
+ import android.view.SurfaceView
15
+ import android.view.TextureView
16
+ import android.view.View
17
+ import android.view.ViewGroup
18
+ import androidx.annotation.RequiresApi
19
+ import com.facebook.react.uimanager.ThemedReactContext
20
+ import com.theoplayer.R
21
+ import com.theoplayer.ReactTHEOplayerContext
22
+ import com.theoplayer.android.api.ads.ima.GoogleImaAdEvent
23
+ import com.theoplayer.android.api.ads.ima.GoogleImaAdEventType
24
+ import com.theoplayer.android.api.event.EventListener
25
+ import com.theoplayer.android.api.event.player.PlayerEvent
26
+ import com.theoplayer.android.api.event.player.PlayerEventTypes
27
+ import com.theoplayer.android.api.player.Player
28
+
29
+ private const val EXTRA_ACTION = "EXTRA_ACTION"
30
+ private const val ACTION_MEDIA_CONTROL = "pip_media_control"
31
+ private const val ACTION_PLAY = 0
32
+ private const val ACTION_PAUSE = ACTION_PLAY + 1
33
+ private const val ACTION_RWD = ACTION_PLAY + 2
34
+ private const val ACTION_FFD = ACTION_PLAY + 3
35
+ private const val ACTION_IGNORE = ACTION_PLAY + 999
36
+ private const val SKIP_TIME = 15
37
+
38
+ private val PIP_ASPECT_RATIO_DEFAULT = Rational(16, 9)
39
+ private val PIP_ASPECT_RATIO_MIN = Rational(100, 239)
40
+ private val PIP_ASPECT_RATIO_MAX = Rational(239, 100)
41
+
42
+ class PipUtils(
43
+ private val viewCtx: ReactTHEOplayerContext,
44
+ private val reactContext: ThemedReactContext
45
+ ) {
46
+
47
+ private var enabled: Boolean = false
48
+ private var onPlayerAction: EventListener<PlayerEvent<*>>? = null
49
+ private var onAdAction: EventListener<GoogleImaAdEvent>? = null
50
+ private val playerEvents = listOf(PlayerEventTypes.PLAY, PlayerEventTypes.PAUSE)
51
+ private val adEvents = listOf(GoogleImaAdEventType.STARTED, GoogleImaAdEventType.CONTENT_RESUME_REQUESTED)
52
+ private val broadcastReceiver: BroadcastReceiver = buildBroadcastReceiver()
53
+
54
+ private val player: Player
55
+ get() = viewCtx.player
56
+
57
+ init {
58
+ onPlayerAction = EventListener {
59
+ updatePipParams()
60
+ }
61
+ onAdAction = EventListener {
62
+ updatePipParams()
63
+ }
64
+ }
65
+
66
+ fun enable() {
67
+ if (enabled) {
68
+ return
69
+ }
70
+ playerEvents.forEach { action ->
71
+ player.addEventListener(action, onPlayerAction)
72
+ }
73
+ adEvents.forEach { action ->
74
+ player.ads.addEventListener(action, onAdAction)
75
+ }
76
+ reactContext.currentActivity?.registerReceiver(
77
+ broadcastReceiver,
78
+ IntentFilter(ACTION_MEDIA_CONTROL)
79
+ )
80
+ enabled = true
81
+ }
82
+
83
+ fun disable() {
84
+ if (!enabled) {
85
+ return
86
+ }
87
+ playerEvents.forEach { action ->
88
+ player.removeEventListener(action, onPlayerAction)
89
+ }
90
+ adEvents.forEach { action ->
91
+ player.ads.removeEventListener(action, onAdAction)
92
+ }
93
+ try {
94
+ reactContext.currentActivity?.unregisterReceiver(broadcastReceiver)
95
+ } catch (ignore: IllegalArgumentException) { /*ignore*/}
96
+ enabled = false
97
+ }
98
+
99
+ fun destroy() {
100
+ disable()
101
+ }
102
+
103
+ @RequiresApi(Build.VERSION_CODES.O)
104
+ fun buildPipActions(
105
+ paused: Boolean,
106
+ enablePlayPause: Boolean,
107
+ enableTrickPlay: Boolean
108
+ ): List<RemoteAction> {
109
+ return mutableListOf<RemoteAction>().apply {
110
+
111
+ // Trick-play: Rewind
112
+ if (enableTrickPlay) {
113
+ add(
114
+ buildRemoteAction(
115
+ ACTION_RWD,
116
+ R.drawable.ic_rewind,
117
+ R.string.rwd_pip,
118
+ R.string.rwd_desc_pip
119
+ )
120
+ )
121
+ }
122
+
123
+ // Play/pause
124
+ // Always add this button, but send an ACTION_IGNORE if disabled.
125
+ add(
126
+ if (paused) {
127
+ buildRemoteAction(
128
+ if (enablePlayPause) ACTION_PLAY else ACTION_IGNORE,
129
+ R.drawable.ic_play,
130
+ R.string.play_pip,
131
+ R.string.play_desc_pip
132
+ )
133
+ } else {
134
+ buildRemoteAction(
135
+ if (enablePlayPause) ACTION_PAUSE else ACTION_IGNORE,
136
+ R.drawable.ic_pause,
137
+ R.string.pause_pip,
138
+ R.string.pause_desc_pip
139
+ )
140
+ }
141
+ )
142
+
143
+ // Trick-play: Fast Forward
144
+ if (enableTrickPlay) {
145
+ add(
146
+ buildRemoteAction(
147
+ ACTION_FFD,
148
+ R.drawable.ic_fast_forward,
149
+ R.string.ffd_pip,
150
+ R.string.ffd_desc_pip
151
+ )
152
+ )
153
+ }
154
+ }
155
+ }
156
+
157
+ private fun getSafeAspectRatio(width: Int, height: Int): Rational {
158
+ val aspectRatio = Rational(width, height)
159
+ if (aspectRatio.isNaN || aspectRatio.isInfinite || aspectRatio.isZero) {
160
+ // Default aspect ratio
161
+ return PIP_ASPECT_RATIO_DEFAULT
162
+ }
163
+ if (aspectRatio > PIP_ASPECT_RATIO_MAX) {
164
+ return PIP_ASPECT_RATIO_MAX
165
+ }
166
+ if (aspectRatio < PIP_ASPECT_RATIO_MIN) {
167
+ return PIP_ASPECT_RATIO_MIN
168
+ }
169
+ return aspectRatio
170
+ }
171
+
172
+ private fun getContentViewRect(view: ViewGroup): Rect? {
173
+ for (i in 0 until view.childCount) {
174
+ val child: View = view.getChildAt(i)
175
+ if (child is ViewGroup) {
176
+ return getContentViewRect(child)
177
+ } else if (child as? SurfaceView != null || child as? TextureView != null) {
178
+ val visibleRect = Rect()
179
+ child.getGlobalVisibleRect(visibleRect)
180
+ return visibleRect
181
+ }
182
+ }
183
+ return null
184
+ }
185
+
186
+ @RequiresApi(Build.VERSION_CODES.O)
187
+ fun getPipParams(): PictureInPictureParams {
188
+ val view = viewCtx.playerView
189
+ val player = view.player
190
+ val visibleRect = getContentViewRect(view)
191
+ val isAd = player.ads.isPlaying
192
+ val isLive = player.duration.isInfinite()
193
+ val enablePlayPause = !isAd
194
+ val enableTrickPlay = !isAd && !isLive
195
+
196
+ return PictureInPictureParams.Builder()
197
+ .setSourceRectHint(visibleRect)
198
+ // Must be between 2.39:1 and 1:2.39 (inclusive)
199
+ .setAspectRatio(getSafeAspectRatio(view.player.videoWidth, view.player.videoHeight))
200
+ .setActions(
201
+ buildPipActions(player.isPaused, enablePlayPause, enableTrickPlay)
202
+ )
203
+ .build()
204
+ }
205
+
206
+ private fun updatePipParams() {
207
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
208
+ reactContext.currentActivity?.setPictureInPictureParams(getPipParams())
209
+ }
210
+ }
211
+
212
+ private fun buildBroadcastReceiver(): BroadcastReceiver {
213
+ return object : BroadcastReceiver() {
214
+ @RequiresApi(Build.VERSION_CODES.O)
215
+ override fun onReceive(context: Context?, intent: Intent?) {
216
+ intent?.getIntExtra(EXTRA_ACTION, -1)?.let { action ->
217
+ when (action) {
218
+ ACTION_PLAY -> player.play()
219
+ ACTION_PAUSE -> player.pause()
220
+ ACTION_FFD -> player.currentTime += SKIP_TIME
221
+ ACTION_RWD -> player.currentTime -= SKIP_TIME
222
+ }
223
+ reactContext.currentActivity?.setPictureInPictureParams(getPipParams())
224
+ }
225
+ }
226
+ }
227
+ }
228
+
229
+ @RequiresApi(Build.VERSION_CODES.O)
230
+ private fun buildRemoteAction(
231
+ requestId: Int,
232
+ iconId: Int,
233
+ titleId: Int,
234
+ descId: Int
235
+ ): RemoteAction {
236
+ val intent = Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_ACTION, requestId)
237
+ val pendingIntent =
238
+ PendingIntent.getBroadcast(reactContext, requestId, intent, PendingIntent.FLAG_IMMUTABLE)
239
+ val icon: Icon = Icon.createWithResource(reactContext, iconId)
240
+ val title = reactContext.getString(titleId)
241
+ val desc = reactContext.getString(descId)
242
+ return RemoteAction(icon, title, desc, pendingIntent)
243
+ }
244
+ }
@@ -1,19 +1,12 @@
1
1
  package com.theoplayer.presentation
2
2
 
3
3
  import android.app.AppOpsManager
4
- import android.app.PictureInPictureParams
5
4
  import android.content.BroadcastReceiver
6
5
  import android.content.Context
7
6
  import android.content.Intent
8
7
  import android.content.IntentFilter
9
8
  import android.content.pm.PackageManager
10
- import android.graphics.Rect
11
9
  import android.os.Build
12
- import android.util.Rational
13
- import android.view.SurfaceView
14
- import android.view.TextureView
15
- import android.view.View
16
- import android.view.ViewGroup
17
10
  import androidx.activity.ComponentActivity
18
11
  import androidx.core.view.WindowInsetsCompat
19
12
  import androidx.core.view.WindowInsetsControllerCompat
@@ -25,10 +18,6 @@ import com.theoplayer.android.api.error.ErrorCode
25
18
  import com.theoplayer.android.api.error.THEOplayerException
26
19
  import com.theoplayer.android.api.player.PresentationMode
27
20
 
28
- private val PIP_ASPECT_RATIO_DEFAULT = Rational(16, 9)
29
- private val PIP_ASPECT_RATIO_MIN = Rational(100, 239)
30
- private val PIP_ASPECT_RATIO_MAX = Rational(239, 100)
31
-
32
21
  class PresentationManager(
33
22
  private val viewCtx: ReactTHEOplayerContext,
34
23
  private val reactContext: ThemedReactContext,
@@ -38,6 +27,8 @@ class PresentationManager(
38
27
  private var onUserLeaveHintReceiver: BroadcastReceiver? = null
39
28
  private var onPictureInPictureModeChanged: BroadcastReceiver? = null
40
29
 
30
+ private val pipUtils: PipUtils = PipUtils(viewCtx, reactContext)
31
+
41
32
  var currentPresentationMode: PresentationMode = PresentationMode.INLINE
42
33
  private set
43
34
 
@@ -57,15 +48,9 @@ class PresentationManager(
57
48
  // Dispatch event on every PiP mode change
58
49
  val inPip = intent?.getBooleanExtra("isInPictureInPictureMode", false) ?: false
59
50
  if (inPip) {
60
- updatePresentationMode(PresentationMode.PICTURE_IN_PICTURE)
51
+ onEnterPip()
61
52
  } else {
62
- val pipCtx: PresentationModeChangePipContext = if ((reactContext.currentActivity as? ComponentActivity)
63
- ?.lifecycle?.currentState == Lifecycle.State.CREATED) {
64
- PresentationModeChangePipContext.CLOSED
65
- } else {
66
- PresentationModeChangePipContext.RESTORED
67
- }
68
- updatePresentationMode(PresentationMode.INLINE, PresentationModeChangeContext(pipCtx))
53
+ onExitPip()
69
54
  }
70
55
  }
71
56
  }
@@ -85,6 +70,7 @@ class PresentationManager(
85
70
  try {
86
71
  reactContext.currentActivity?.unregisterReceiver(onUserLeaveHintReceiver)
87
72
  reactContext.currentActivity?.unregisterReceiver(onPictureInPictureModeChanged)
73
+ pipUtils.destroy()
88
74
  } catch (ignore: Exception) {
89
75
  }
90
76
  }
@@ -104,20 +90,6 @@ class PresentationManager(
104
90
  }
105
91
  }
106
92
 
107
- private fun getContentViewRect(view: ViewGroup): Rect? {
108
- for (i in 0 until view.childCount) {
109
- val child: View = view.getChildAt(i)
110
- if (child is ViewGroup) {
111
- return getContentViewRect(child)
112
- } else if (child as? SurfaceView != null || child as? TextureView != null) {
113
- val visibleRect = Rect()
114
- child.getGlobalVisibleRect(visibleRect)
115
- return visibleRect
116
- }
117
- }
118
- return null
119
- }
120
-
121
93
  private fun enterPip() {
122
94
  // PiP not supported
123
95
  if (!supportsPip || Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
@@ -141,33 +113,28 @@ class PresentationManager(
141
113
  }
142
114
 
143
115
  try {
144
- val view = viewCtx.playerView
145
- val visibleRect = getContentViewRect(view)
146
- reactContext.currentActivity?.enterPictureInPictureMode(
147
- PictureInPictureParams.Builder().setSourceRectHint(visibleRect)
148
- // Must be between 2.39:1 and 1:2.39 (inclusive)
149
- .setAspectRatio(getSafeAspectRatio(view.player.videoWidth, view.player.videoHeight))
150
- // The active MediaSession will connect the controls
151
- .build()
152
- )
116
+ pipUtils.enable()
117
+ reactContext.currentActivity?.enterPictureInPictureMode(pipUtils.getPipParams())
153
118
  } catch (_: Exception) {
154
119
  onPipError()
155
120
  }
156
121
  }
157
122
 
158
- private fun getSafeAspectRatio(width: Int, height: Int): Rational {
159
- val aspectRatio = Rational(width, height)
160
- if (aspectRatio.isNaN || aspectRatio.isInfinite || aspectRatio.isZero) {
161
- // Default aspect ratio
162
- return PIP_ASPECT_RATIO_DEFAULT
163
- }
164
- if (aspectRatio > PIP_ASPECT_RATIO_MAX) {
165
- return PIP_ASPECT_RATIO_MAX
166
- }
167
- if (aspectRatio < PIP_ASPECT_RATIO_MIN) {
168
- return PIP_ASPECT_RATIO_MIN
169
- }
170
- return aspectRatio
123
+ private fun onEnterPip() {
124
+ updatePresentationMode(PresentationMode.PICTURE_IN_PICTURE)
125
+ }
126
+
127
+ private fun onExitPip() {
128
+ val pipCtx: PresentationModeChangePipContext =
129
+ if ((reactContext.currentActivity as? ComponentActivity)
130
+ ?.lifecycle?.currentState == Lifecycle.State.CREATED
131
+ ) {
132
+ PresentationModeChangePipContext.CLOSED
133
+ } else {
134
+ PresentationModeChangePipContext.RESTORED
135
+ }
136
+ updatePresentationMode(PresentationMode.INLINE, PresentationModeChangeContext(pipCtx))
137
+ pipUtils.disable()
171
138
  }
172
139
 
173
140
  private fun hasPipPermission(): Boolean {
@@ -0,0 +1,9 @@
1
+ <vector xmlns:android="http://schemas.android.com/apk/res/android"
2
+ android:width="24dp"
3
+ android:height="24dp"
4
+ android:viewportWidth="24"
5
+ android:viewportHeight="24">
6
+ <path
7
+ android:fillColor="#FF000000"
8
+ android:pathData="M4,18l8.5,-6L4,6v12zM13,6v12l8.5,-6L13,6z"/>
9
+ </vector>
@@ -0,0 +1,9 @@
1
+ <vector xmlns:android="http://schemas.android.com/apk/res/android"
2
+ android:width="24dp"
3
+ android:height="24dp"
4
+ android:viewportWidth="24"
5
+ android:viewportHeight="24">
6
+ <path
7
+ android:fillColor="#FF000000"
8
+ android:pathData="M11,18L11,6l-8.5,6 8.5,6zM11.5,12l8.5,6L20,6l-8.5,6z"/>
9
+ </vector>
@@ -2,6 +2,14 @@
2
2
  <resources>
3
3
  <string name="pause">Pause</string>
4
4
  <string name="play">Play</string>
5
+ <string name="pause_pip">Pause</string>
6
+ <string name="pause_desc_pip">Pause video</string>
7
+ <string name="play_pip">Play</string>
8
+ <string name="play_desc_pip">Play video</string>
9
+ <string name="ffd_pip">Fast Forward</string>
10
+ <string name="ffd_desc_pip">Fast Forward video</string>
11
+ <string name="rwd_pip">Rewind</string>
12
+ <string name="rwd_desc_pip">Rewind video</string>
5
13
 
6
14
  <string name="background_playback_service_description">THEOplayer service providing background playback.</string>
7
15
  <string name="notification_channel_id">theoplayer_default_channel</string>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-theoplayer",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "A THEOplayer video component for react-native.",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
@@ -44,7 +44,6 @@ Pod::Spec.new do |s|
44
44
  if theofeatures.include?("CHROMECAST")
45
45
  puts "Adding THEOplayer-Integration-GoogleCast"
46
46
  s.ios.dependency "THEOplayer-Integration-GoogleCast"
47
- s.ios.dependency "google-cast-sdk-dynamic-xcframework-no-bluetooth"
48
47
  end
49
48
  end
50
49