TP-33080 | replaced flipkart youtubePlayer with iframe player (#6915)
TP-33080 | replaced flipkart youtubePlayer with pierfrancescosoffritti player
This commit is contained in:
committed by
GitHub Enterprise
parent
54a42ba45b
commit
3b133e2ec4
@@ -114,7 +114,7 @@ dependencies {
|
||||
api 'in.juspay:hypersdk:2.1.7'
|
||||
|
||||
//youtube video player
|
||||
implementation 'com.github.flipkart-incubator:android-inline-youtube-view:1.0.4'
|
||||
implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:11.1.0'
|
||||
|
||||
implementation hiltLibs.implementation
|
||||
kapt hiltLibs.kapt
|
||||
|
||||
@@ -8,50 +8,40 @@
|
||||
package com.navi.common.video
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.Window
|
||||
import android.view.WindowManager
|
||||
import android.widget.Toast
|
||||
import com.flipkart.youtubeview.models.PlayerStateList
|
||||
import com.flipkart.youtubeview.models.PlayerStateList.PlayerState
|
||||
import com.flipkart.youtubeview.util.ServiceUtil
|
||||
import com.google.android.youtube.player.YouTubeBaseActivity
|
||||
import com.google.android.youtube.player.YouTubeInitializationResult
|
||||
import com.google.android.youtube.player.YouTubePlayer
|
||||
import com.google.android.youtube.player.YouTubePlayer.*
|
||||
import com.google.android.youtube.player.YouTubePlayerView
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import com.navi.common.R
|
||||
import com.navi.common.databinding.NaviYoutubePlayerBinding
|
||||
import com.navi.common.model.ModuleNameV2
|
||||
import com.navi.common.ui.activity.BaseActivity
|
||||
import com.navi.common.utils.CommonNaviAnalytics
|
||||
import com.navi.common.utils.NaviModuleFlowType
|
||||
import com.navi.common.utils.NaviYoutubeEventHelper
|
||||
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer
|
||||
|
||||
class NaviYoutubeActivity : YouTubeBaseActivity(), OnInitializedListener {
|
||||
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener
|
||||
|
||||
|
||||
|
||||
|
||||
class NaviYoutubeActivity : BaseActivity() {
|
||||
/*
|
||||
* Pass video id as extras
|
||||
*/
|
||||
val argVideoId = "videoId"
|
||||
|
||||
/**
|
||||
* Pass api key as extras
|
||||
*/
|
||||
val argApiKey = "apiKey"
|
||||
|
||||
private var youTubePlayer: YouTubePlayer? = null
|
||||
|
||||
@PlayerState
|
||||
private var playerState = PlayerStateList.NONE
|
||||
|
||||
private var videoId: String? = null
|
||||
|
||||
private var isEventRequired: Boolean = false
|
||||
|
||||
private var naviModuleFlowType: String? = null
|
||||
private lateinit var binding: NaviYoutubePlayerBinding
|
||||
|
||||
private val naviAnalyticsEventTracker by lazy { CommonNaviAnalytics.naviAnalytics.YoutubeVideoScreen() }
|
||||
|
||||
companion object {
|
||||
const val TAG = "NaviYoutubeActivity"
|
||||
private const val TAG = "NaviYoutubeActivity"
|
||||
const val EVENTS_REQUIRED = "events_required"
|
||||
const val NAVI_MODULE_FLOW_TYPE = "navi_module_flow_type"
|
||||
const val ARG_VIDEO_ID = "videoId"
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@@ -64,131 +54,22 @@ class NaviYoutubeActivity : YouTubeBaseActivity(), OnInitializedListener {
|
||||
)
|
||||
)
|
||||
}
|
||||
setFullscreen()
|
||||
setContentView(R.layout.navi_youtube_player)
|
||||
val youTubePlayerView: YouTubePlayerView = findViewById(R.id.youtube_player)
|
||||
videoId = getVideoId()
|
||||
val apiKey = getApiKey()
|
||||
binding = DataBindingUtil.setContentView(this, R.layout.navi_youtube_player)
|
||||
lifecycle.addObserver(binding.youtubePlayerView)
|
||||
|
||||
if (ServiceUtil.isYouTubeServiceAvailable(this)) {
|
||||
youTubePlayerView.initialize(apiKey, this)
|
||||
} else {
|
||||
Toast.makeText(this, R.string.youtube_player_not_available, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
binding.youtubePlayerView.addYouTubePlayerListener(object :
|
||||
AbstractYouTubePlayerListener() {
|
||||
override fun onReady(youTubePlayer: YouTubePlayer) {
|
||||
youTubePlayer.loadVideo(videoId.orEmpty(), 0f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun readBundle() {
|
||||
if (intent != null) {
|
||||
isEventRequired = intent.getBooleanExtra(EVENTS_REQUIRED, false)
|
||||
naviModuleFlowType = intent.getStringExtra(NAVI_MODULE_FLOW_TYPE)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onInitializationSuccess(
|
||||
p0: Provider?,
|
||||
player: YouTubePlayer?,
|
||||
restored: Boolean
|
||||
) {
|
||||
youTubePlayer = player
|
||||
youTubePlayer?.setPlayerStyle(PlayerStyle.MINIMAL)
|
||||
youTubePlayer?.setShowFullscreenButton(true)
|
||||
youTubePlayer?.addFullscreenControlFlag(FULLSCREEN_FLAG_CONTROL_ORIENTATION)
|
||||
youTubePlayer?.addFullscreenControlFlag(FULLSCREEN_FLAG_CONTROL_SYSTEM_UI)
|
||||
|
||||
youTubePlayer!!.setPlaybackEventListener(object : PlaybackEventListener {
|
||||
override fun onPlaying() {
|
||||
if (youTubePlayer != null && PlayerStateList.PLAYING != playerState) {
|
||||
playerState = PlayerStateList.PLAYING
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPaused() {
|
||||
handleOnPauseEvent()
|
||||
}
|
||||
|
||||
override fun onStopped() {
|
||||
handleStopEvent()
|
||||
}
|
||||
|
||||
override fun onBuffering(isBuffering: Boolean) {
|
||||
//intentionally left blank
|
||||
}
|
||||
|
||||
override fun onSeekTo(newPositionMillis: Int) {
|
||||
handleOnPauseEvent()
|
||||
}
|
||||
})
|
||||
|
||||
youTubePlayer!!.setPlayerStateChangeListener(object : PlayerStateChangeListener {
|
||||
override fun onLoading() {
|
||||
//intentionally left blank
|
||||
}
|
||||
|
||||
override fun onLoaded(s: String) {
|
||||
//intentionally left blank
|
||||
}
|
||||
|
||||
override fun onAdStarted() {
|
||||
//intentionally left blank
|
||||
}
|
||||
|
||||
override fun onVideoStarted() {
|
||||
//intentionally left blank
|
||||
}
|
||||
|
||||
override fun onVideoEnded() {
|
||||
handleStopEvent()
|
||||
onBackPressed()
|
||||
}
|
||||
|
||||
override fun onError(errorReason: ErrorReason) {
|
||||
//intentionally left blank
|
||||
}
|
||||
})
|
||||
|
||||
player!!.setOnFullscreenListener { handleStopEvent() }
|
||||
|
||||
if (!restored) {
|
||||
youTubePlayer?.loadVideo(getVideoId())
|
||||
}
|
||||
}
|
||||
|
||||
override fun onInitializationFailure(
|
||||
p0: Provider?,
|
||||
p1: YouTubeInitializationResult?
|
||||
) {
|
||||
youTubePlayer = null
|
||||
}
|
||||
|
||||
private fun setFullscreen() {
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE)
|
||||
window.setFlags(
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN
|
||||
)
|
||||
}
|
||||
|
||||
private fun getVideoId(): String? {
|
||||
val extras = if (null != intent) intent.extras else null
|
||||
return extras?.getString(argVideoId)
|
||||
}
|
||||
|
||||
private fun getApiKey(): String? {
|
||||
val extras = if (null != intent) intent.extras else null
|
||||
return extras?.getString(argApiKey)
|
||||
}
|
||||
|
||||
private fun handleOnPauseEvent() {
|
||||
if (youTubePlayer != null && PlayerStateList.PLAYING == playerState
|
||||
|| PlayerStateList.BUFFERING == playerState
|
||||
) {
|
||||
playerState = PlayerStateList.PAUSED
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleStopEvent() {
|
||||
if (youTubePlayer != null && PlayerStateList.PLAYING == playerState || PlayerStateList.BUFFERING == playerState || PlayerStateList.PAUSED == playerState) {
|
||||
playerState = PlayerStateList.STOPPED
|
||||
videoId = intent.getStringExtra(ARG_VIDEO_ID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,33 +81,9 @@ class NaviYoutubeActivity : YouTubeBaseActivity(), OnInitializedListener {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
triggerClickStreamEvent {
|
||||
naviAnalyticsEventTracker.videoWatchedDuration(
|
||||
NaviYoutubeEventHelper.getVideoWatchedDurationEventName(it),
|
||||
duration = youTubePlayer?.currentTimeMillis?.div(1000).toString()
|
||||
)
|
||||
}
|
||||
handleStopEvent()
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
triggerClickStreamEvent {
|
||||
naviAnalyticsEventTracker.onBackButtonPressed(
|
||||
NaviYoutubeEventHelper.getOnYoutubeScreenBackButtonEventName(
|
||||
it
|
||||
)
|
||||
)
|
||||
}
|
||||
finish()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
if (youTubePlayer != null) {
|
||||
youTubePlayer!!.release()
|
||||
}
|
||||
}
|
||||
override val moduleName: ModuleNameV2
|
||||
get() = ModuleNameV2.COMMON
|
||||
|
||||
override val screenName: String
|
||||
get() = TAG
|
||||
}
|
||||
|
||||
@@ -11,29 +11,21 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import com.flipkart.youtubeview.util.ServiceUtil
|
||||
import com.navi.common.BuildConfig
|
||||
import com.navi.common.R
|
||||
import com.navi.common.video.NaviYoutubeActivity.Companion.ARG_VIDEO_ID
|
||||
import javax.inject.Inject
|
||||
|
||||
class YoutubeVideoPlayer @Inject constructor() {
|
||||
private val APIKEY_KEY = "apiKey"
|
||||
private val VIDEOID_KEY = "videoId"
|
||||
|
||||
fun startVideo(videoId: String, context: Context) {
|
||||
if (videoId.isEmpty()) {
|
||||
Toast.makeText(context, R.string.video_id_cannot_be_blank, Toast.LENGTH_LONG).show()
|
||||
return
|
||||
}
|
||||
if (ServiceUtil.isYouTubeServiceAvailable(context)) {
|
||||
val newIntent = Intent(context, NaviYoutubeActivity::class.java)
|
||||
val newBundle = Bundle()
|
||||
newBundle.putString(VIDEOID_KEY, videoId)
|
||||
newBundle.putString(APIKEY_KEY, BuildConfig.YOUTUBE_KEY)
|
||||
newIntent.putExtras(newBundle)
|
||||
context.startActivity(newIntent)
|
||||
} else {
|
||||
Toast.makeText(context, R.string.youtube_player_not_available, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
val newIntent = Intent(context, NaviYoutubeActivity::class.java)
|
||||
val newBundle = Bundle()
|
||||
newBundle.putString(ARG_VIDEO_ID, videoId)
|
||||
newIntent.putExtras(newBundle)
|
||||
context.startActivity(newIntent)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,19 @@
|
||||
~
|
||||
-->
|
||||
|
||||
<com.google.android.youtube.player.YouTubePlayerView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/youtube_player"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:visibility="visible" />
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/black"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical">
|
||||
|
||||
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
|
||||
android:id="@+id/youtube_player_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:autoPlay="false" />
|
||||
</LinearLayout>
|
||||
</layout>
|
||||
Reference in New Issue
Block a user