Exo Player — HMS Video Kit Comparison by building a flavored Android Application . Part IV

Single Player Activity

This activity highly depends on flavor dimensions. Before creating those dimension functions we’ll first initialize what we can on this activity, starting with our layout.

Notice “include” section will be called from flavor dimensions.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context=".ui.activities.SinglePlayerActivity">

    <TextView
        android:id="@+id/sp_more"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginTop="125dp"
        android:background="@drawable/notch_more"
        android:clickable="true"
        android:elevation="2dp"
        android:focusable="true"
        android:fontFamily="@font/bicubik"
        android:foreground="?attr/selectableItemBackgroundBorderless"
        android:gravity="center_horizontal"
        android:padding="3dp"
        android:text="@string/more"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

        <include
            android:id="@+id/layout_video_includer"
            layout="@layout/layout_video"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center" />

</androidx.constraintlayout.widget.ConstraintLayout>

Interface for Single Player

class ISinglePlayer {

    interface ViewSingle{
        fun init()
        fun initDialog()
        fun extractYoutubeUrl()
    }
}

Presenter for Single Player

class SinglePlayerPresenter constructor(private val presenter: ISinglePlayer.ViewSingle) {

    fun onViewsCreate(){
        presenter.init()
    }
}

Finally let’s build activity for our single player:


class SinglePlayerActivity : AppCompatActivity(), ISinglePlayer.ViewSingle {

    private lateinit var binding: ActivitySinglePlayerBinding

    private var url: String? = ""
    private var ytUrl: String? = ""
    private var type: Int? = 0

    lateinit var dialog: Dialog
    private lateinit var dCancel: CircleImageView
    private lateinit var dRecycler: RecyclerView

    private val hmsGmsVideoHelper: HmsGmsVideoHelper by lazy {
        HmsGmsVideoHelper(this)
    }

    private val presenter: SinglePlayerPresenter by lazy {
        SinglePlayerPresenter(this)
    }
...
}

Let’s start by filling overridden methods and custom ones:

  • initDialog(), This will help us to instantiate dialog that helps our users to select from our predefined videos.
override fun initDialog() {
    dialog = Dialog(this, R.style.BlurTheme)
    dialog.window!!.attributes.windowAnimations = R.style.DialogSlide
    dialog.setContentView(R.layout.dialog_more)
    dialog.setCanceledOnTouchOutside(true)

    dCancel = dialog.findViewById(R.id.d_close_dialog)
    dRecycler = dialog.findViewById(R.id.d_video_item_recycler)
}
  • onDestroy

override fun onDestroy() {
    super.onDestroy()
    hmsGmsVideoHelper.releasePlayer()
}
  • init
override fun init() {
    initDialog()
}
  • onCreate
override fun onCreate(savedInstanceState: Bundle?) {
    setTheme(R.style.Theme_ExoVideoReference_TransStatusBar)
    super.onCreate(savedInstanceState)
    binding = ActivitySinglePlayerBinding.inflate(layoutInflater)
    setContentView(binding.root)
    supportActionBar?.hide()
    Slidr.attach(this)
    presenter.onViewsCreate()

    type = intent.getIntExtra("type", 0)
    when (type) {
        0 -> {
            showLogInfo(Constants.mSinglePlayerActivity, "url")
            url = intent.getStringExtra("url")
            hmsGmsVideoHelper.readyPlayer(url!!, url!!)
        }
        1 -> {
/* Not using this for this article */
            showToast(this, "yt")
            ytUrl = intent.getStringExtra("ytUrl")
            extractYoutubeUrl()

        }
    }

    sp_more.setOnClickListener {
        dialog.show()
        dRecycler.layoutManager = LinearLayoutManager(this)
        val adapter = SingleVideoItemAdapter(this,this)
        dRecycler.adapter = adapter
        dRecycler.setHasFixedSize(true)
    }
    dCancel.setOnClickListener {
        dialog.dismiss()
    }
}