Android SDK Integration Examples
This section demonstrates integration examples of the Teleport SDK with popular Android players: ExoPlayer and Media3.
1. ExoPlayer Integration Example
Integration with ExoPlayer involves creating a TeleportDataSourceFactory and passing it to ExoPlayer.Builder.
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
import com.google.android.exoplayer2.ui.PlayerView
import com.teleport.TeleportApi
import com.teleport.TeleportConfiguration
import com.teleport.exoplayer.TeleportDataSourceFactory // Import for ExoPlayer
class MainActivity : AppCompatActivity() {
private var exoPlayer: ExoPlayer? = null
private var teleportApi: TeleportApi? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 1. Create Teleport configuration with your API_KEY
val teleportConfiguration = TeleportConfiguration.Builder("YOUR_API_KEY") // Replace with your API_KEY
.build()
// 2. Initialize TeleportApi
val teleportApi = TeleportApi(this, teleportConfiguration)
.also { this.teleportApi = it }
// 3. Create a DataSource factory through which the player will interact with Teleport
val dataSourceFactory = TeleportDataSourceFactory(teleportApi)
// 4. Create MediaSourceFactory using our TeleportDataSourceFactory
val mediaSourceFactory = DefaultMediaSourceFactory(this)
.setDataSourceFactory(dataSourceFactory)
// 5. Create ExoPlayer, passing our MediaSourceFactory to it
val exoPlayer = ExoPlayer.Builder(this)
.setMediaSourceFactory(mediaSourceFactory)
.build()
.also { this.exoPlayer = it }
// 6. Add a listener to track player events (e.g., buffering)
exoPlayer.addListener(object : Player.Listener {
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
if (playbackState == Player.STATE_BUFFERING) {
teleportApi.buffering() // Notify Teleport about buffering
}
}
})
// 7. Bind the player to PlayerView and set the media item
val playerView = findViewById<PlayerView>(R.id.playerView)
playerView.player = exoPlayer
val item = MediaItem.fromUri("YOUR_STREAM_URL") // Replace with the URL of your video stream
exoPlayer.setMediaItem(item)
}
override fun onResume() {
super.onResume()
exoPlayer?.play() // Resume playback when activity resumes
}
override fun onPause() {
super.onPause()
exoPlayer?.pause() // Pause playback when activity pauses
}
override fun onDestroy() {
super.onDestroy()
exoPlayer?.release() // Release ExoPlayer resources
exoPlayer = null
// Important: Release TeleportApi resources after releasing player resources
teleportApi?.release()
teleportApi = null
}
}Important
Don't forget to replace "YOUR_API_KEY" with your actual API key from the Teleport cabinet and "YOUR_STREAM_URL" with the URL of your video stream.
2. Media3 Integration Example
Media3 integration is very similar to ExoPlayer integration, as Media3 is the next generation of Android media libraries built on ExoPlayer. The main difference lies in using the corresponding Media3 packages.
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.media3.exoplayer.ExoPlayer // Changed to Media3
import androidx.media3.common.MediaItem // Changed to Media3
import androidx.media3.common.Player // Changed to Media3
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory // Changed to Media3
import androidx.media3.ui.PlayerView // Changed to Media3
import com.teleport.TeleportApi
import com.teleport.TeleportConfiguration
import com.teleport.media3.TeleportDataSourceFactory // Changed to Media3
class MainActivity : AppCompatActivity() {
private var exoPlayer: ExoPlayer? = null // Type remains ExoPlayer
private var teleportApi: TeleportApi? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val teleportConfiguration = TeleportConfiguration.Builder("YOUR_API_KEY").build()
val teleportApi = TeleportApi(this, teleportConfiguration)
.also { this.teleportApi = it }
val dataSourceFactory = TeleportDataSourceFactory(teleportApi)
val mediaSourceFactory = DefaultMediaSourceFactory(this)
.setDataSourceFactory(dataSourceFactory)
val exoPlayer = ExoPlayer.Builder(this)
.setMediaSourceFactory(mediaSourceFactory)
.build()
.also { this.exoPlayer = it }
exoPlayer.addListener(object : Player.Listener {
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
if (playbackState == Player.STATE_BUFFERING) {
teleportApi.buffering()
}
}
})
val playerView = findViewById<PlayerView>(R.id.playerView)
playerView.player = exoPlayer
val item = MediaItem.fromUri("YOUR_STREAM_URL")
exoPlayer.setMediaItem(item)
}
override fun onResume() {
super.onResume()
exoPlayer?.play()
}
override fun onPause() {
super.onPause()
exoPlayer?.pause()
}
override fun onDestroy() {
super.onDestroy()
exoPlayer?.release()
exoPlayer = null
teleportApi?.release()
teleportApi = null
}
}Note
The main difference when integrating with Media3 is the use of classes from androidx.media3.* packages instead of com.google.android.exoplayer2.* and the use of com.teleport.media3.TeleportDataSourceFactory. The initialization and interaction logic remains almost identical.