Skip to content

Commit

Permalink
Separate example
Browse files Browse the repository at this point in the history
  • Loading branch information
daytime-em committed Aug 7, 2024
1 parent d035d2d commit b6a9da5
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 63 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Expand Down Expand Up @@ -31,6 +32,8 @@
<activity
android:name=".examples.background.BackgroundPlayActivity"
android:exported="false" />
<activity android:name=".examples.basic.PlayerReuseActivity"
android:exported="false" />

<service
android:name=".examples.background.BackgroundPlayService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.mux.stats.muxdatasdkformedia3.databinding.ActivityMainBinding
import com.mux.stats.muxdatasdkformedia3.databinding.ListitemExampleBinding
import com.mux.stats.muxdatasdkformedia3.examples.basic.BasicPlayerActivity
import com.mux.stats.muxdatasdkformedia3.examples.basic.ComposeUiExampleActivity
import com.mux.stats.muxdatasdkformedia3.examples.basic.PlayerReuseActivity
import com.mux.stats.muxdatasdkformedia3.examples.ima.ImaClientAdsActivity
import com.mux.stats.muxdatasdkformedia3.examples.ima.ImaServerAdsActivity

Expand Down Expand Up @@ -53,7 +54,11 @@ class MainActivity : AppCompatActivity() {
Example(
title = "Compose UI With Shared Player",
destination = Intent(this, ComposeUiExampleActivity::class.java)
)
),
Example(
title = "Reusing a Player for multiple MediaItems",
destination = Intent(this, PlayerReuseActivity::class.java),
),
// TODO: post-beta, add APIs for talking to a `MediaSessionService`
// Example(
// title = "Background playback",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.mux.stats.muxdatasdkformedia3.examples.basic

import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.annotation.OptIn
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.Player.PositionInfo
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView.KEEP_SCREEN_ON
Expand All @@ -26,10 +23,6 @@ import com.mux.stats.sdk.core.model.CustomerViewData
import com.mux.stats.sdk.muxstats.MuxDataSdk
import com.mux.stats.sdk.muxstats.MuxStatsSdkMedia3
import com.mux.stats.sdk.muxstats.monitorWithMuxData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.time.Duration

class BasicPlayerActivity : AppCompatActivity() {

Expand Down Expand Up @@ -71,61 +64,6 @@ class BasicPlayerActivity : AppCompatActivity() {
newPlayer.setMediaItem(createMediaItem(mediaUrl))
newPlayer.prepare()
newPlayer.playWhenReady = true

lifecycleScope.launch(Dispatchers.Main) {
val useEnable = false
val stopBeforeThirdVideo = false

delay(10_000)
Log.d("TestingEnable", "disabling")
if (useEnable) {
muxStats?.disable()
}

Log.d("TestingEnable", "playing Steve video")
newPlayer.setMediaItem(MediaItem.fromUri(Uri.parse(Constants.VOD_TEST_URL_STEVE)))
if (!useEnable) {
Log.d("TestingEnable", "calling videoChange() 1")
muxStats?.videoChange(CustomerVideoData().apply {
videoTitle = "Old Keynote (Second)"
})
}
newPlayer.prepare()
newPlayer.play()
delay(10_000)

Log.d("TestingEnable", "playing Big Buck Bunny")
// debugging: stop() the player => play,playing,pause,...,[actual start]
// debugging: don't stop() the player => play,...,rebufferstart,....,rebufferend,[actual start]
// both cases should not send playing (should result in 'starting up' always)
if (stopBeforeThirdVideo) {
newPlayer.stop()
}

newPlayer.setMediaItem(MediaItem.fromUri(Uri.parse(Constants.VOD_TEST_URL_BIG_BUCK_BUNNY)))
if (!useEnable) {
Log.d("TestingEnable", "calling videoChange() 2")
muxStats?.videoChange(CustomerVideoData().apply {
videoTitle = "Big Buck Bunny (Third)"
})
} else {
Log.d("TestingEnable", "calling enable()")
muxStats?.enable(CustomerData().apply {
customerVideoData = CustomerVideoData().apply {
videoTitle = "Big Buck Bunny (Third)"
}
})
}
Log.d("TestingEnable", "About to prepare the player. The current state is ${newPlayer.playbackState}")
newPlayer.prepare()
Log.d("TestingEnable", "Just called prepare on the player. The current state is ${newPlayer.playbackState}")
newPlayer.play()
// debugging: Maybe try calling enable() _after_

delay(10_000)
Log.w("TestingEnable", "test over")
muxStats?.disable()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package com.mux.stats.muxdatasdkformedia3.examples.basic

import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.annotation.OptIn
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView.KEEP_SCREEN_ON
import androidx.media3.ui.PlayerView.SHOW_BUFFERING_WHEN_PLAYING
import com.mux.stats.muxdatasdkformedia3.Constants
import com.mux.stats.muxdatasdkformedia3.databinding.ActivityPlayerBinding
import com.mux.stats.muxdatasdkformedia3.toMediaItem
import com.mux.stats.sdk.core.model.CustomerData
import com.mux.stats.sdk.core.model.CustomerPlayerData
import com.mux.stats.sdk.core.model.CustomerVideoData
import com.mux.stats.sdk.core.model.CustomerViewData
import com.mux.stats.sdk.muxstats.MuxDataSdk
import com.mux.stats.sdk.muxstats.MuxStatsSdkMedia3
import com.mux.stats.sdk.muxstats.monitorWithMuxData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class PlayerReuseActivity : AppCompatActivity() {

private lateinit var view: ActivityPlayerBinding
private var player: Player? = null
private var muxStats: MuxStatsSdkMedia3<ExoPlayer>? = null

@OptIn(UnstableApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
view = ActivityPlayerBinding.inflate(layoutInflater)
setContentView(view.root)

view.playerView.apply {
setShowBuffering(SHOW_BUFFERING_WHEN_PLAYING)
}
window.addFlags(KEEP_SCREEN_ON)
}

override fun onResume() {
super.onResume()
startPlaying(
intent.getStringExtra(EXTRA_URL) ?: Constants.VOD_TEST_URL_DRAGON_WARRIOR_LADY
)
}

override fun onPause() {
stopPlaying()
super.onPause()
}

private fun startPlaying(mediaUrl: String) {
stopPlaying()

player = createPlayer().also { newPlayer ->
muxStats = monitorPlayer(newPlayer)

view.playerView.player = newPlayer
newPlayer.setMediaItem(createMediaItem(mediaUrl))
newPlayer.prepare()
newPlayer.playWhenReady = true

// now change the video a couple of times
lifecycleScope.launch(Dispatchers.Main) {
val usingEnableAndDisable = false // If false, will use videoChange instead
val stopBeforeThirdVideo = false

delay(10_000)
Log.d(TAG, "disabling")
if (usingEnableAndDisable) {
muxStats?.disable()
}

Log.d(TAG, "playing Steve video")
newPlayer.setMediaItem(MediaItem.fromUri(Uri.parse(Constants.VOD_TEST_URL_STEVE)))
if (!usingEnableAndDisable) {
Log.d(TAG, "calling videoChange() 1")
muxStats?.videoChange(CustomerVideoData().apply {
videoTitle = "Old Keynote (Second)"
})
}
newPlayer.prepare()
newPlayer.play()
delay(10_000)

Log.d(TAG, "playing Big Buck Bunny")
// debugging: stop() the player => play,playing,pause,...,[actual start]
// debugging: don't stop() the player => play,...,rebufferstart,....,rebufferend,[actual start]
// both cases should not send playing (should result in 'starting up' always)
if (stopBeforeThirdVideo) {
newPlayer.stop()
}

newPlayer.setMediaItem(MediaItem.fromUri(Uri.parse(Constants.VOD_TEST_URL_BIG_BUCK_BUNNY)))
if (!usingEnableAndDisable) {
Log.d(TAG, "calling videoChange() 2")
muxStats?.videoChange(CustomerVideoData().apply {
videoTitle = "Big Buck Bunny (Third)"
})
} else {
Log.d(TAG, "calling enable()")
muxStats?.enable(CustomerData().apply {
customerVideoData = CustomerVideoData().apply {
videoTitle = "Big Buck Bunny (Third)"
}
})
}
Log.d(TAG, "About to prepare the player. The current state is ${newPlayer.playbackState}")
newPlayer.prepare()
Log.d(TAG, "Just called prepare on the player. The current state is ${newPlayer.playbackState}")
newPlayer.play()
// debugging: Maybe try calling enable() _after_

delay(10_000)
Log.w(TAG, "test over")
muxStats?.disable()
}
}
}

private fun createMediaItem(mediaUrl: String): MediaItem {
return mediaUrl.toMediaItem().buildUpon()
.setMediaMetadata(
MediaMetadata.Builder()
.setTitle("Sample app, BasicPlayerActivity")
.setDescription("A Basic test video")
.build()
).build()
}

private fun stopPlaying() {
player?.let { oldPlayer ->
oldPlayer.stop()
oldPlayer.release()
}
// Make sure to release() your muxStats whenever the user is done with the player
muxStats?.release()
}

private fun monitorPlayer(player: ExoPlayer): MuxStatsSdkMedia3<ExoPlayer> {
// You can add your own data to a View, which will override any data we collect
val customerData = CustomerData(
CustomerPlayerData().apply { },
CustomerVideoData().apply {
videoId = "A Custom ID"
videoTitle = "Sintel (First)"
},
CustomerViewData().apply { }
)

return player.monitorWithMuxData(
context = this,
envKey = Constants.MUX_DATA_ENV_KEY,
customerData = customerData,
playerView = view.playerView,
logLevel = MuxDataSdk.LogcatLevel.DEBUG,
)
}

private fun createPlayer(): ExoPlayer {
return ExoPlayer.Builder(this)
.build().apply {
addListener(object : Player.Listener {
override fun onPlayerError(error: PlaybackException) {
Log.e(javaClass.simpleName, "player error!", error)
Toast.makeText(this@PlayerReuseActivity, error.localizedMessage, Toast.LENGTH_SHORT)
.show()
}
})
}
}

companion object {
const val EXTRA_URL: String = "com.mux.video.url"
const val TAG = "PlayerReuseActivity"
}
}

0 comments on commit b6a9da5

Please sign in to comment.