-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPlayerActivity.kt
More file actions
153 lines (117 loc) · 5.74 KB
/
Copy pathPlayerActivity.kt
File metadata and controls
153 lines (117 loc) · 5.74 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.theoplayer.sample.surface
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.theoplayer.android.api.THEOplayerGlobal
import com.theoplayer.android.api.player.AspectRatio
import com.theoplayer.android.api.player.Player
import com.theoplayer.android.api.player.RenderingTarget
import com.theoplayer.sample.common.SourceManager
import com.theoplayer.sample.surface.databinding.ActivityPlayerBinding
class PlayerActivity : AppCompatActivity() {
private lateinit var viewBinding: ActivityPlayerBinding
private lateinit var theoPlayer: Player
private var aspectRatio = AspectRatio.FIT
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// See basic-playback's PlayerActivity for more information about basic setup.
viewBinding = DataBindingUtil.setContentView(this, R.layout.activity_player)
// Enable all debug logs from THEOplayer.
val theoDebugLogger = THEOplayerGlobal.getSharedInstance(this).logger
theoDebugLogger.enableAllTags()
theoPlayer = viewBinding.theoPlayerView.player
setSupportActionBar(viewBinding.toolbar)
viewBinding.theoPlayerView.fullScreenManager.isFullScreenOrientationCoupled = true
theoPlayer.source = SourceManager.BIG_BUCK_BUNNY_HLS
theoPlayer.isAutoplay = true
theoPlayer.setAspectRatio(aspectRatio)
// Set onClickListeners to allow surface switching.
viewBinding.btnSetSurfaceView.setOnClickListener(::setSurfaceView)
viewBinding.btnSetTextureView.setOnClickListener(::setTextureView)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
viewBinding.btnSetSurfaceControl.setOnClickListener(::setSurfaceControl)
}
viewBinding.btnSetCustomSurfaceView.setOnClickListener(::setCustomSurfaceView)
viewBinding.btnSetCustomTextureView.setOnClickListener(::setCustomTextureView)
}
private fun setSurfaceView(view: View) {
// Stop the playback to avoid decoder issues on some devices when switching between rendering targets at runtime.
theoPlayer.stop()
viewBinding.customSurfaceViewContainer.removeAllViews()
viewBinding.customTextureViewContainer.removeAllViews()
// Set the rendering target to SurfaceView.
theoPlayer.setRenderingTarget(RenderingTarget.SURFACE_VIEW)
// Set the source.
theoPlayer.source = SourceManager.BIG_BUCK_BUNNY_HLS
}
private fun setTextureView(view: View) {
// Stop the playback to avoid decoder issues on some devices when switching between rendering targets at runtime.
theoPlayer.stop()
viewBinding.customSurfaceViewContainer.removeAllViews()
viewBinding.customTextureViewContainer.removeAllViews()
// Set the rendering target to TextureView.
theoPlayer.setRenderingTarget(RenderingTarget.TEXTURE_VIEW)
// Set the source.
theoPlayer.source = SourceManager.BIG_BUCK_BUNNY_HLS
}
@RequiresApi(Build.VERSION_CODES.Q)
private fun setSurfaceControl(view: View) {
// Stop the playback to avoid decoder issues on some devices when switching between rendering targets at runtime.
theoPlayer.stop()
viewBinding.customSurfaceViewContainer.removeAllViews()
viewBinding.customTextureViewContainer.removeAllViews()
// Set the rendering target to SurfaceControl. This requires API 29 or later.
theoPlayer.setRenderingTarget(RenderingTarget.SURFACE_CONTROL)
// Set the source.
theoPlayer.source = SourceManager.BIG_BUCK_BUNNY_HLS
}
private fun setCustomSurfaceView(view: View) {
// Stop the playback to avoid decoder issues on some devices when switching between rendering targets at runtime.
theoPlayer.stop()
viewBinding.customSurfaceViewContainer.removeAllViews()
viewBinding.customTextureViewContainer.removeAllViews()
val layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
)
layoutParams.gravity = Gravity.CENTER
val customSurfaceView = CustomSurfaceView(this, theoPlayer, aspectRatio)
customSurfaceView.layoutParams = layoutParams
viewBinding.customSurfaceViewContainer.addView(customSurfaceView)
// Set the source.
theoPlayer.source = SourceManager.BIG_BUCK_BUNNY_HLS
}
private fun setCustomTextureView(view: View) {
// Stop the playback to avoid decoder issues on some devices when switching between rendering targets at runtime.
theoPlayer.stop()
viewBinding.customSurfaceViewContainer.removeAllViews()
viewBinding.customTextureViewContainer.removeAllViews()
val layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
)
layoutParams.gravity = Gravity.CENTER
val customTextureView = CustomTextureView(this, theoPlayer, aspectRatio)
customTextureView.layoutParams = layoutParams
viewBinding.customTextureViewContainer.addView(customTextureView)
// Set the source.
theoPlayer.source = SourceManager.BIG_BUCK_BUNNY_HLS
}
override fun onPause() {
super.onPause()
viewBinding.theoPlayerView.onPause()
}
override fun onResume() {
super.onResume()
viewBinding.theoPlayerView.onResume()
}
override fun onDestroy() {
super.onDestroy()
viewBinding.theoPlayerView.onDestroy()
}
}