Skip to content

Commit e655be7

Browse files
committed
add example exoplayer
1 parent 79b36ef commit e655be7

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

example/src/main/java/com/tuanhav95/example/fragment/ExoPlayerTopFragment.kt

+15-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import android.view.View
88
import android.view.ViewGroup
99
import androidx.fragment.app.Fragment
1010
import com.google.android.exoplayer2.ExoPlayerFactory
11+
import com.google.android.exoplayer2.Player
1112
import com.google.android.exoplayer2.SimpleExoPlayer
1213
import com.google.android.exoplayer2.source.ProgressiveMediaSource
14+
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
1315
import com.google.android.exoplayer2.upstream.DataSource
1416
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
1517
import com.google.android.exoplayer2.util.Util
1618
import com.tuanhav95.example.R
1719
import kotlinx.android.synthetic.main.fragment_top_exo_player.*
1820

19-
class ExoPlayerTopFragment : Fragment() {
21+
class ExoPlayerTopFragment : Fragment(), Player.EventListener {
2022

2123
companion object {
2224
const val STREAM_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
@@ -38,17 +40,28 @@ class ExoPlayerTopFragment : Fragment() {
3840
mediaDataSourceFactory = DefaultDataSourceFactory(context, Util.getUserAgent(context, "mediaPlayerSample"))
3941

4042
val mediaSource = ProgressiveMediaSource.Factory(mediaDataSourceFactory).createMediaSource(Uri.parse(STREAM_URL))
41-
43+
simpleExoPlayer.addListener(this)
4244
simpleExoPlayer.prepare(mediaSource, false, false)
4345
simpleExoPlayer.playWhenReady = true
4446

4547
playerView.setShutterBackgroundColor(Color.TRANSPARENT)
4648
playerView.player = simpleExoPlayer
4749
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+
}
4860
}
4961

5062
override fun onStop() {
5163
super.onStop()
64+
simpleExoPlayer.removeListener(this)
5265
simpleExoPlayer.release()
5366
}
5467

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:id="@+id/root"
4-
android:focusable="true"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
6+
android:background="#000000"
7+
android:focusable="true"
78
android:keepScreenOn="true">
89

9-
<com.google.android.exoplayer2.ui.PlayerView
10-
android:id="@+id/playerView"
11-
android:focusable="true"
10+
<com.google.android.exoplayer2.ui.AspectRatioFrameLayout
11+
android:id="@+id/aspectRatioFrameLayout"
1212
android:layout_width="match_parent"
13-
android:layout_height="match_parent"/>
13+
android:layout_height="match_parent">
1414

15+
<com.google.android.exoplayer2.ui.PlayerView
16+
android:id="@+id/playerView"
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent"
19+
android:focusable="true" />
20+
</com.google.android.exoplayer2.ui.AspectRatioFrameLayout>
1521
</FrameLayout>

0 commit comments

Comments
 (0)