Skip to navigation

Set up the player

Learn how to import the FastPix Android Player SDK and initialize the player in your Activity.

Required imports

import io.fastpix.media3.PlayerView
import io.fastpix.media3.PlaybackListener
import io.fastpix.media3.core.FastPixPlayer
import io.fastpix.media3.core.PlaybackResolution
import io.fastpix.media3.core.RenditionOrder
import io.fastpix.media3.core.StreamType
import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException

Declare player objects

You can either use PlayerView on its own (it auto-creates a FastPixPlayer with default settings), or create a configured FastPixPlayer and attach it to PlayerView.

Declare the player objects globally in your Activity or Fragment so they’re accessible throughout the lifecycle:

class CustomizedPlayerActivity : AppCompatActivity() {
private lateinit var binding: ActivityCustomizedPlayerBinding
private val playerView get() = binding.playerView
private var player: FastPixPlayer? = null
private lateinit var playbackListener: PlaybackListener
}

Initialize the player

Create a FastPixPlayer instance using the builder pattern and attach it to your PlayerView:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCustomizedPlayerBinding.inflate(layoutInflater)
setContentView(binding.root)
player = FastPixPlayer.Builder(this)
.setAutoplay(true)
.setLoop(false)
.build()
playerView.player = player
}

PlayerView XML tips

When using io.fastpix.media3.PlayerView in your XML layout, keep these properties in mind:

  • Configuration-change survival: PlayerView.retainPlayerOnConfigChange is true by default. Assign an android:id in XML so the view can retain the player instance across rotation.
  • Tap gesture: Set playerView.isTapGestureEnabled = true to enable single-tap toggle play/pause using togglePlayPause().
  • Release: Call playerView.release() in onDestroy when the Activity is finishing (isFinishing == true) to force a release even if retention is enabled.

Clean up resources

Remove the playback listener and release the player when the Activity is destroyed:

override fun onDestroy() {
super.onDestroy()
player?.removePlaybackListener(playbackListener)
if (isFinishing) {
playerView.release()
}
}

What’s next