Basic Playback
Create the player
To create a player instance, use the PlayerBuilder with your media URL and
configuration. The following examples show how to create a player for both
clear and DRM-protected content.
// Sample Player Creation for Clear Content
val url = "https://storage.googleapis.com/shaka-demo-assets/angel-one-widevine/dash.mpd"
val player = PlayerBuilder()
.mediaURL(url)
.mediaType(MediaType.DASH)
.drmScheme(DRMScheme.NONE)
.drmLicenseURL("")
.build(applicationContext)
// Sample Player Creation for Platform DRM Protected Content
val player = PlayerBuilder()
.mediaURL(url)
.mediaType(MediaType.DASH)
.drmScheme(DRMScheme.WIDEVINE)
.drmLicenseURL("https://cwip-shaka-proxy.appspot.com/no_auth")
.build(applicationContext)
Attach listeners
Your application can listen to events such as changes in player state, buffering state, seek state, and playback errors by registering a listener/delegate.
player.addListener(this)
Attach player to a view
- View's XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/playerRoot"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="27dp"
android:layout_marginBottom="27dp"
android:layout_marginLeft="48dp"
android:layout_marginRight="48dp"/>
- Activity / Fragment
class PlayerActivity : AppCompatActivity(), Player.Listener {
private lateinit var playerRoot: FrameLayout
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_player)
playerRoot = findViewById(R.id.playerRoot)
}
override fun onResume() {
super.onResume()
if (this::player.isInitialized) {
player.addListener(this)
player.play()
}
}
private fun attachToPlayer() {
player.attachRendererView(
playerRoot,
FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
)
}
override fun onPlayerViewAvailable(playerView: FrameLayout) {
attachToPlayer()
}
}
Start playback
The Player supports the following control operations:
- play - Plays a paused or loaded content.
- pause - Pauses a playing content.
- seek - Seeks content to a specified position in the playback window.
Warning: Client application developers shouldn't perform any operations on the underlying raw player. The Player libraries behavior is undefined if you do so.
Stop and abort
The player provides two methods to stop playback:
- Invoking
stop()stops rendering and releases all underlying resources. - Invoking
abort(error: Error)has the same effect asstop(). Additionally, theonError()callback on the attachedPlayer.Listeneris invoked.
Note: Use
abort(error: Error)when another component (without user intervention) aborts the playback because of some policy restrictions and you need to show the appropriate error on the UI.
Warning: Don't implement Audio focus on the application side when you're using QP library versions 7.0.135 and above. QP libraries internally handle Audio focus. Ignoring this warning causes playback issues.
Managing playback behavior when the app loses visibility (TV)
On Android, managing playback when your app is no longer visible to the user is important for both user experience and efficient resource utilization. To stop playback when the device is switched off or HDMI is switched, implement the following changes in your player's activity:
override fun onStop() {
player.stop()
super.onStop()
}
Note: It's up to you to implement the above change based on expected behavior. Android recommends stopping or pausing playback on device switch off. Refer to Android docs.