@@ -8,15 +8,17 @@ import android.view.View
8
8
import android.view.ViewGroup
9
9
import androidx.fragment.app.Fragment
10
10
import com.google.android.exoplayer2.ExoPlayerFactory
11
+ import com.google.android.exoplayer2.Player
11
12
import com.google.android.exoplayer2.SimpleExoPlayer
12
13
import com.google.android.exoplayer2.source.ProgressiveMediaSource
14
+ import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
13
15
import com.google.android.exoplayer2.upstream.DataSource
14
16
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
15
17
import com.google.android.exoplayer2.util.Util
16
18
import com.tuanhav95.example.R
17
19
import kotlinx.android.synthetic.main.fragment_top_exo_player.*
18
20
19
- class ExoPlayerTopFragment : Fragment () {
21
+ class ExoPlayerTopFragment : Fragment (), Player.EventListener {
20
22
21
23
companion object {
22
24
const val STREAM_URL = " http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
@@ -38,17 +40,28 @@ class ExoPlayerTopFragment : Fragment() {
38
40
mediaDataSourceFactory = DefaultDataSourceFactory (context, Util .getUserAgent(context, " mediaPlayerSample" ))
39
41
40
42
val mediaSource = ProgressiveMediaSource .Factory (mediaDataSourceFactory).createMediaSource(Uri .parse(STREAM_URL ))
41
-
43
+ simpleExoPlayer.addListener( this )
42
44
simpleExoPlayer.prepare(mediaSource, false , false )
43
45
simpleExoPlayer.playWhenReady = true
44
46
45
47
playerView.setShutterBackgroundColor(Color .TRANSPARENT )
46
48
playerView.player = simpleExoPlayer
47
49
playerView.requestFocus()
50
+
51
+ aspectRatioFrameLayout.resizeMode = AspectRatioFrameLayout .RESIZE_MODE_FIXED_WIDTH
52
+ aspectRatioFrameLayout.setAspectRatio(16f / 9 )
53
+ }
54
+
55
+ override fun onPlayerStateChanged (playWhenReady : Boolean , playbackState : Int ) {
56
+ super .onPlayerStateChanged(playWhenReady, playbackState)
57
+ if (playbackState == Player .STATE_ENDED ) {
58
+ simpleExoPlayer.playWhenReady = true
59
+ }
48
60
}
49
61
50
62
override fun onStop () {
51
63
super .onStop()
64
+ simpleExoPlayer.removeListener(this )
52
65
simpleExoPlayer.release()
53
66
}
54
67
0 commit comments