Skip to content

Commit

Permalink
refactor(player): simplify controller constructor and init
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Mar 19, 2024
1 parent 8f7cb17 commit bc812a9
Showing 1 changed file with 155 additions and 67 deletions.
222 changes: 155 additions & 67 deletions player/src/main/java/video/api/player/ApiVideoPlayerController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ import video.api.player.notifications.ApiVideoPlayerNotificationController
import video.api.player.views.ApiVideoExoPlayerView
import java.io.IOException


/**
* The api.video player controller class.
* Creates a new controller without a view.
*
* @param context the application context
* @param initialVideoOptions initial video options
Expand All @@ -46,38 +47,49 @@ import java.io.IOException
* @param looper the looper where call to the player are executed. By default, it is the current looper or the main looper.
* @constructor Creates a new controller without a view.
*/
class ApiVideoPlayerController
constructor(
private val context: Context,
fun ApiVideoPlayerController(
context: Context,
initialVideoOptions: VideoOptions? = null,
initialAutoplay: Boolean = false,
listener: Listener? = null,
listener: ApiVideoPlayerController.Listener? = null,
looper: Looper = Looper.myLooper() ?: Looper.getMainLooper(),
private val notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
context
)
) {
/**
* Creates a new controller with an [ApiVideoExoPlayerView].
*
* @param context the application context
* @param initialVideoOptions initial video options
* @param initialAutoplay initial autoplay: true to play the video immediately, false otherwise
* @param listener the [ApiVideoPlayerController.Listener] to listen to player events
* @param playerView the player view
* @param looper the looper where call to the player are executed. By default, it is the current looper or the main looper.
*/
constructor(
context: Context,
initialVideoOptions: VideoOptions? = null,
initialAutoplay: Boolean = false,
listener: Listener? = null,
playerView: ApiVideoExoPlayerView,
looper: Looper = Looper.myLooper() ?: Looper.getMainLooper(),
notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
context
)
) : this(
): ApiVideoPlayerController {
return ApiVideoPlayerController(
context,
looper,
notificationController
).apply {
autoplay = initialAutoplay
listener?.let { addListener(it) }
initialVideoOptions?.let { videoOptions = it }
}
}

/**
* Creates a new controller with an [ApiVideoExoPlayerView].
*
* @param context the application context
* @param initialVideoOptions initial video options
* @param initialAutoplay initial autoplay: true to play the video immediately, false otherwise
* @param listener the [ApiVideoPlayerController.Listener] to listen to player events
* @param playerView the player view
* @param looper the looper where call to the player are executed. By default, it is the current looper or the main looper.
*/
fun ApiVideoPlayerController(
context: Context,
initialVideoOptions: VideoOptions? = null,
initialAutoplay: Boolean = false,
listener: ApiVideoPlayerController.Listener? = null,
playerView: ApiVideoExoPlayerView,
looper: Looper = Looper.myLooper() ?: Looper.getMainLooper(),
notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
context
)
): ApiVideoPlayerController {
return ApiVideoPlayerController(
context,
initialVideoOptions,
initialAutoplay,
Expand All @@ -86,32 +98,137 @@ constructor(
looper,
notificationController
)
}

/**
* Creates a new controller with a `media3` [PlayerView].
*
* @param context the application context
* @param initialVideoOptions initial video options
* @param initialAutoplay initial autoplay: true to play the video immediately, false otherwise
* @param listener the [ApiVideoPlayerController.Listener] to listen to player events
* @param playerView the [PlayerView] to use to display the player
* @param looper the looper where call to the player are executed. By default, it is the current looper or the main looper.
*/
fun ApiVideoPlayerController(
context: Context,
initialVideoOptions: VideoOptions? = null,
initialAutoplay: Boolean = false,
listener: ApiVideoPlayerController.Listener? = null,
playerView: PlayerView,
looper: Looper = Looper.myLooper() ?: Looper.getMainLooper(),
notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
context
)
): ApiVideoPlayerController {
return ApiVideoPlayerController(
context,
playerView,
looper,
notificationController
).apply {
autoplay = initialAutoplay
listener?.let { addListener(it) }
initialVideoOptions?.let { videoOptions = it }
}
}

/**
* Creates a new controller with a [SurfaceView].
*
* @param context the application context
* @param initialVideoOptions initial video options
* @param initialAutoplay initial autoplay: true to play the video immediately, false otherwise
* @param listener the [ApiVideoPlayerController.Listener] to listen to player events
* @param surfaceView the [SurfaceView] to use to display the video
* @param looper the looper where call to the player are executed. By default, it is the current looper or the main looper.
*/
fun ApiVideoPlayerController(
context: Context,
initialVideoOptions: VideoOptions? = null,
initialAutoplay: Boolean = false,
listener: ApiVideoPlayerController.Listener? = null,
surfaceView: SurfaceView,
looper: Looper = Looper.myLooper() ?: Looper.getMainLooper(),
notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
context
)
): ApiVideoPlayerController {
return ApiVideoPlayerController(
context,
surfaceView,
looper,
notificationController
).apply {
autoplay = initialAutoplay
listener?.let { addListener(it) }
initialVideoOptions?.let { videoOptions = it }
}
}

/**
* Creates a new controller with a [Surface].
*
* @param context the application context
* @param initialVideoOptions initial video options
* @param initialAutoplay initial autoplay: true to play the video immediately, false otherwise
* @param listener the [ApiVideoPlayerController.Listener] to listen to player events
* @param surface the [Surface] to use to display the video
* @param looper the looper where call to the player are executed. By default, it is the current looper or the main looper.
*/
fun ApiVideoPlayerController(
context: Context,
initialVideoOptions: VideoOptions? = null,
initialAutoplay: Boolean = false,
listener: ApiVideoPlayerController.Listener? = null,
surface: Surface,
looper: Looper = Looper.myLooper() ?: Looper.getMainLooper(),
notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
context
)
): ApiVideoPlayerController {
return ApiVideoPlayerController(
context,
surface,
looper,
notificationController
).apply {
autoplay = initialAutoplay
listener?.let { addListener(it) }
initialVideoOptions?.let { videoOptions = it }
}
}

/**
* The api.video player controller class.
*
* @param context the application context
* @param looper the looper where call to the player are executed. By default, it is the current looper or the main looper.
* @constructor Creates a new controller without a view.
*/
class ApiVideoPlayerController(
private val context: Context,
looper: Looper = Looper.myLooper() ?: Looper.getMainLooper(),
private val notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
context
)
) {
/**
* Creates a new controller with a `media3` [PlayerView].
*
* @param context the application context
* @param initialVideoOptions initial video options
* @param initialAutoplay initial autoplay: true to play the video immediately, false otherwise
* @param listener the [ApiVideoPlayerController.Listener] to listen to player events
* @param playerView the [PlayerView] to use to display the player
* @param looper the looper where call to the player are executed. By default, it is the current looper or the main looper.
*/
constructor(
context: Context,
initialVideoOptions: VideoOptions? = null,
initialAutoplay: Boolean = false,
listener: Listener? = null,
playerView: PlayerView,
looper: Looper = Looper.myLooper() ?: Looper.getMainLooper(),
notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
context
)
) : this(
context,
initialVideoOptions,
initialAutoplay,
listener,
looper,
notificationController
) {
Expand All @@ -122,27 +239,18 @@ constructor(
* Creates a new controller with a [SurfaceView].
*
* @param context the application context
* @param initialVideoOptions initial video options
* @param initialAutoplay initial autoplay: true to play the video immediately, false otherwise
* @param listener the [ApiVideoPlayerController.Listener] to listen to player events
* @param surfaceView the [SurfaceView] to use to display the video
* @param looper the looper where call to the player are executed. By default, it is the current looper or the main looper.
*/
constructor(
context: Context,
initialVideoOptions: VideoOptions? = null,
initialAutoplay: Boolean = false,
listener: Listener? = null,
surfaceView: SurfaceView,
looper: Looper = Looper.myLooper() ?: Looper.getMainLooper(),
notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
context
)
) : this(
context,
initialVideoOptions,
initialAutoplay,
listener,
looper,
notificationController
) {
Expand All @@ -153,27 +261,18 @@ constructor(
* Creates a new controller with a [Surface].
*
* @param context the application context
* @param initialVideoOptions initial video options
* @param initialAutoplay initial autoplay: true to play the video immediately, false otherwise
* @param listener the [ApiVideoPlayerController.Listener] to listen to player events
* @param surface the [Surface] to use to display the video
* @param looper the looper where call to the player are executed. By default, it is the current looper or the main looper.
*/
constructor(
context: Context,
initialVideoOptions: VideoOptions? = null,
initialAutoplay: Boolean = false,
listener: Listener? = null,
surface: Surface,
looper: Looper = Looper.myLooper() ?: Looper.getMainLooper(),
notificationController: ApiVideoPlayerNotificationController? = ApiVideoPlayerNotificationController(
context
)
) : this(
context,
initialVideoOptions,
initialAutoplay,
listener,
looper,
notificationController
) {
Expand Down Expand Up @@ -328,6 +427,7 @@ constructor(
addAnalyticsListener(exoplayerListener)
Handler(context.mainLooper).post {
notificationController?.showNotification(this)
prepare()
}
}

Expand Down Expand Up @@ -489,18 +589,6 @@ constructor(
exoplayer.setPlaybackSpeed(value)
}

init {
listener?.let { addListener(it) }

handler.post {
initialVideoOptions?.let {
videoOptions = it
}
autoplay = initialAutoplay
exoplayer.prepare()
}
}

/**
* Add a listener to the player
*
Expand Down

0 comments on commit bc812a9

Please sign in to comment.