2024-03-28 11:22:04 +01:00
|
|
|
package com.brentvatne.react
|
|
|
|
|
|
2024-10-10 22:59:41 +02:00
|
|
|
import com.brentvatne.common.api.Source
|
2024-03-28 11:22:04 +01:00
|
|
|
import com.brentvatne.exoplayer.ReactExoplayerView
|
2024-05-28 02:00:38 -07:00
|
|
|
import com.facebook.react.bridge.Promise
|
2024-03-28 11:22:04 +01:00
|
|
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
|
|
|
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
|
|
|
|
import com.facebook.react.bridge.ReactMethod
|
2024-10-10 22:59:41 +02:00
|
|
|
import com.facebook.react.bridge.ReadableMap
|
2024-03-28 11:22:04 +01:00
|
|
|
import com.facebook.react.bridge.UiThreadUtil
|
2024-03-31 19:15:14 +02:00
|
|
|
import com.facebook.react.uimanager.UIManagerHelper
|
|
|
|
|
import com.facebook.react.uimanager.common.UIManagerType
|
2024-03-28 11:22:04 +01:00
|
|
|
import kotlin.math.roundToInt
|
|
|
|
|
|
|
|
|
|
class VideoManagerModule(reactContext: ReactApplicationContext?) : ReactContextBaseJavaModule(reactContext) {
|
2024-05-06 21:51:17 +02:00
|
|
|
override fun getName(): String = REACT_CLASS
|
2024-03-28 11:22:04 +01:00
|
|
|
|
|
|
|
|
private fun performOnPlayerView(reactTag: Int, callback: (ReactExoplayerView?) -> Unit) {
|
|
|
|
|
UiThreadUtil.runOnUiThread {
|
2024-04-18 16:18:14 +08:00
|
|
|
try {
|
|
|
|
|
val uiManager = UIManagerHelper.getUIManager(
|
|
|
|
|
reactApplicationContext,
|
|
|
|
|
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) UIManagerType.FABRIC else UIManagerType.DEFAULT
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
val view = uiManager?.resolveView(reactTag)
|
|
|
|
|
|
|
|
|
|
if (view is ReactExoplayerView) {
|
|
|
|
|
callback(view)
|
|
|
|
|
} else {
|
|
|
|
|
callback(null)
|
|
|
|
|
}
|
|
|
|
|
} catch (e: Exception) {
|
2024-03-28 11:22:04 +01:00
|
|
|
callback(null)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ReactMethod
|
2024-07-12 17:27:42 +09:00
|
|
|
fun setPlayerPauseStateCmd(reactTag: Int, paused: Boolean?) {
|
2024-03-28 11:22:04 +01:00
|
|
|
performOnPlayerView(reactTag) {
|
|
|
|
|
it?.setPausedModifier(paused!!)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ReactMethod
|
2024-08-07 14:39:41 +02:00
|
|
|
@Suppress("UNUSED_PARAMETER") // codegen compatibility
|
2024-07-12 17:27:42 +09:00
|
|
|
fun seekCmd(reactTag: Int, time: Float, tolerance: Float) {
|
2024-03-28 11:22:04 +01:00
|
|
|
performOnPlayerView(reactTag) {
|
|
|
|
|
it?.seekTo((time * 1000f).roundToInt().toLong())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 13:51:48 +03:30
|
|
|
@ReactMethod
|
2024-07-12 17:27:42 +09:00
|
|
|
fun setVolumeCmd(reactTag: Int, volume: Float) {
|
2024-05-20 13:51:48 +03:30
|
|
|
performOnPlayerView(reactTag) {
|
|
|
|
|
it?.setVolumeModifier(volume)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 02:00:38 -07:00
|
|
|
@ReactMethod
|
2024-07-12 17:27:42 +09:00
|
|
|
fun setFullScreenCmd(reactTag: Int, fullScreen: Boolean) {
|
2024-05-28 02:00:38 -07:00
|
|
|
performOnPlayerView(reactTag) {
|
2024-07-12 17:27:42 +09:00
|
|
|
it?.setFullscreen(fullScreen)
|
2024-05-28 02:00:38 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-10 22:59:41 +02:00
|
|
|
@ReactMethod
|
|
|
|
|
fun setSourceCmd(reactTag: Int, source: ReadableMap?) {
|
|
|
|
|
performOnPlayerView(reactTag) {
|
|
|
|
|
it?.setSrc(Source.parse(source, reactApplicationContext))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 00:11:26 +03:30
|
|
|
@ReactMethod
|
2024-07-12 17:27:42 +09:00
|
|
|
fun getCurrentPosition(reactTag: Int, promise: Promise) {
|
2024-06-11 00:11:26 +03:30
|
|
|
performOnPlayerView(reactTag) {
|
2024-07-12 17:27:42 +09:00
|
|
|
it?.getCurrentPosition(promise)
|
2024-06-11 00:11:26 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 11:22:04 +01:00
|
|
|
companion object {
|
|
|
|
|
private const val REACT_CLASS = "VideoManager"
|
|
|
|
|
}
|
|
|
|
|
}
|