-
-
Notifications
You must be signed in to change notification settings - Fork 759
/
BasicExampleActivity.kt
102 lines (83 loc) · 4.41 KB
/
BasicExampleActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.pierfrancescosoffritti.androidyoutubeplayer.chromecast.sampleapp.examples.basicExample
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.mediarouter.app.MediaRouteButton
import com.google.android.gms.cast.framework.CastContext
import com.pierfrancescosoffritti.androidyoutubeplayer.chromecast.chromecastsender.ChromecastYouTubePlayerContext
import com.pierfrancescosoffritti.androidyoutubeplayer.chromecast.chromecastsender.io.infrastructure.ChromecastConnectionListener
import com.pierfrancescosoffritti.androidyoutubeplayer.chromecast.chromecastsender.utils.PlayServicesUtils
import com.pierfrancescosoffritti.androidyoutubeplayer.chromecast.sampleapp.utils.MediaRouteButtonUtils
import com.pierfrancescosoffritti.androidyoutubeplayer.chromecast.sampleapp.utils.VideoIdsProvider
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.PlayerConstants
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.toFloat
import com.pierfrancescosoffritti.cyplayersample.R
class BasicExampleActivity : AppCompatActivity() {
private val googlePlayServicesAvailabilityRequestCode = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_basic_example)
val mediaRouteButton = findViewById<MediaRouteButton>(R.id.media_route_button)
MediaRouteButtonUtils.initMediaRouteButton(mediaRouteButton)
// can't use CastContext until I'm sure the user has GooglePlayServices
PlayServicesUtils.checkGooglePlayServicesAvailability(
this,
googlePlayServicesAvailabilityRequestCode
) { initChromecast() }
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// can't use CastContext until I'm sure the user has GooglePlayServices
if (requestCode == googlePlayServicesAvailabilityRequestCode)
PlayServicesUtils.checkGooglePlayServicesAvailability(
this,
googlePlayServicesAvailabilityRequestCode
) { initChromecast() }
}
private fun initChromecast() {
ChromecastYouTubePlayerContext(
CastContext.getSharedInstance(this).sessionManager,
SimpleChromecastConnectionListener()
)
}
inner class SimpleChromecastConnectionListener : ChromecastConnectionListener {
override fun onChromecastConnecting() {
Log.d(javaClass.simpleName, "onChromecastConnecting")
}
override fun onChromecastConnected(chromecastYouTubePlayerContext: ChromecastYouTubePlayerContext) {
Log.d(javaClass.simpleName, "onChromecastConnected")
initializeCastPlayer(chromecastYouTubePlayerContext)
}
override fun onChromecastDisconnected() {
Log.d(javaClass.simpleName, "onChromecastDisconnected")
}
private fun initializeCastPlayer(chromecastYouTubePlayerContext: ChromecastYouTubePlayerContext) {
chromecastYouTubePlayerContext.initialize(object : AbstractYouTubePlayerListener() {
override fun onReady(youTubePlayer: YouTubePlayer) {
youTubePlayer.loadVideo(VideoIdsProvider.getNextVideoId(), 0f)
initPlaybackSpeedButtons(youTubePlayer)
}
override fun onPlaybackRateChange(
youTubePlayer: YouTubePlayer,
playbackRate: PlayerConstants.PlaybackRate
) {
val playbackSpeedTextView = findViewById<TextView>(R.id.playback_speed_text_view)
playbackSpeedTextView.text = "Playback speed: ${playbackRate.toFloat()}"
}
})
}
}
fun initPlaybackSpeedButtons(youTubePlayer: YouTubePlayer) {
val playbackSpeed_0_25 = findViewById<Button>(R.id.playback_speed_0_25)
val playbackSpeed_1 = findViewById<Button>(R.id.playback_speed_1)
val playbackSpeed_2 = findViewById<Button>(R.id.playback_speed_2)
playbackSpeed_0_25.setOnClickListener { youTubePlayer.setPlaybackRate(PlayerConstants.PlaybackRate.RATE_0_25) }
playbackSpeed_1.setOnClickListener { youTubePlayer.setPlaybackRate(PlayerConstants.PlaybackRate.RATE_1) }
playbackSpeed_2.setOnClickListener { youTubePlayer.setPlaybackRate(PlayerConstants.PlaybackRate.RATE_2) }
}
}