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:
- 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>
- In app/java/DemoActivity.java, instantiate the video player on the
onCreate
method.
val videoPlayer = findViewById<player.VPPlayer>(R.id.player)
- From here on out, you can opt to setup the player through the API(using
videoId
andplayerId
) or Model(usingvideoModel
andplayerModel
)
- API
- Model
- 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())
}
})
- Setup the Project using an activity, API_KEY and Project Id
videoPlayer?.setup(this,API_KEY,project_Id, handler)
- 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.
- Create the VideoModel
var demoVideoModel = VPModel(useCustomVideo = true, video = demoVideoModel, config = demoVideoConfigs, true)
- Create the PlayerConfigsModel
var demoVideoConfigs = VPPlayerConfigs(autostartOnLoad = AutoStartOnLoadModel(onMobile = true),loop = false,autoPlayDefaultEnabled = true)
- Setup the player using the Models above
tip
You should set useCustomVideo
and useCustomConfigs
to true
videoPlayer.set(this,supportFragmentManager, VPModel(useCustomVideo = true,video = demoVideoModel,config = demoVideoConfigs,useCustomConfigs = true))
- Initialize player.
videoPlayer?.initializePlayer()
- 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()
}
}
- 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()
}
- You are ready to play a video.