Skip to main content

Setup the player

Add the VPPlayerView to the appropriate files of your Android app.

The VPPlayer View is the central UI component of our SDK. This class allows you to easily load new media into the player, manage video and audio playback, and register multiple event listeners that could help you with custom analytics or error handling.

Add the VPPlayer View

Use the following steps and code examples to add the VPPlayer View to the app/res/layout/activity_main.xml and app/java/MainActivity.java files of your app:

  1. In app/res/layout/activity_main.xml, add the player.VPPlayer .
Adding the Player View in activity_main.xml
<?xml version="1.0" encoding="utf-8">
<androidx.constraintLayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<player.VPPlayer
android:id="@+id/player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:controller_layout_id="@layout/vp_player_controls_layout"
app:layout_constrainedHeight="true"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:show_buffering="when_playing" />

</androidx.constraintlayout.widget.ConstraintLayout>
  1. In app/java/DemoActivity.java, instantiate the video player on the onCreate method.
val videoPlayer = findViewById<player.VPPlayer>(R.id.player)
  1. From here on out, you can opt to setup the player through the API(using videoId and playerId) or Model(using videoModel and playerModel)
  1. Create handler method
val handler = (object : VPInterface.ResultHandler {
override fun onSuccess() {
videoPlayer?.set(activity,supportFragmentManager,
"playerId","videoId","videoUrl")
}
override fun onFailure(e: Exception) {
Log.e("setupHandler", e.toString())
}
})
  1. Setup the Project using an activity, API_KEY and Project Id
 videoPlayer?.setup(this,API_KEY,project_Id, handler)
  1. You can also set the player from the API and the video from the model.
var videoModel = VPVideoModel(videoSource = "www.example.com")
videoPlayer?.set(activity, supportFragmentManager,"playerId",videoModel)
note

You can setup player by using: Player Id and Video Id OR Player Id and Playlist Id OR Player Id and Video Url OR only Video Url.

  1. Override onBackPressed(). This allows you to handle properly to close full screen, close settings and other view changes.
override fun onBackPressed() {
if(videoPlayer?.isFullScreen()) {
videoPlayer?.closeFullScreen()
} else {
super.onBackPressed()
}
}
  1. Ovverride onDestroy(). This allows the player to release on destroy of the activity.
override fun onDestroy() {
If (videoPlayer?.isCasting()) {
videoPlayer?.releaseCastPlayer()
} else {
videoPlayer?.releasePlayer()
}
super.onDestroy()
}
  1. You are ready to play a video.