ExoPlayer非最新版使用
添加依赖
api 'com.google.android.exoplayer:exoplayer:2.12.1'
//初始化播放器
fun initPlayer() {
var videoTrackSelectionFactory = AdaptiveTrackSelection.Factory()
//轨道选择器
var trackSelector = DefaultTrackSelector(requireActivity(), videoTrackSelectionFactory)
//用于控制MediaSource何时缓冲更多的媒体资源以及缓冲多少媒体资源
var loadControl = DefaultLoadControl.Builder()
.setAllocator(DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE))
.setBufferDurationsMs(360000, 600000, 1000, 5000)
.setPrioritizeTimeOverSizeThresholds(false)
.setTargetBufferBytes(C.LENGTH_UNSET)
.createDefaultLoadControl()
//Provides estimates of the currently available bandwidth.
var bandwidthMeter = DefaultBandwidthMeter.Builder(requireActivity()).build()
//渲染器,用于渲染媒体文件。
var renderersFactory = DefaultRenderersFactory(requireActivity())
simpleExoPlayer = SimpleExoPlayer.Builder(requireActivity(), renderersFactory)
.setTrackSelector(trackSelector)
.setLoadControl(loadControl)
.setBandwidthMeter(bandwidthMeter)
.build()
var dataSourceFactory = DefaultDataSourceFactory(requireContext(), Util.getUserAgent(requireContext(), "app_name"))
var currUrl = "播放地址"
//播放的媒体文件
var videoSource: MediaSource
if (currUrl.contains(".m3u8")) {
videoSource = HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(currUrl));
} else {
videoSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(currUrl));
}
simpleExoPlayer?.addListener(object : Player.EventListener {
override fun onPlayerError(error: ExoPlaybackException) {
super.onPlayerError(error)
// showToastS("播放异常")
}
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
// Log.e("ExoPlayer","playWhenReady: $playWhenReady +$playbackState")
when (playbackState) {
Player.STATE_BUFFERING -> {
// showToastS("加载中")
//mBinding.downLoading.startAnimation(animation2)
//mBinding.linlayLoad.visibility = View.VISIBLE
}
Player.STATE_READY -> {
//showToastS("播放中")
//mBinding.downLoading.clearAnimation()
//mBinding.linlayLoad.visibility = View.GONE
}
Player.STATE_ENDED -> {
//showToastS("播放结束")
//mBinding.downLoading.clearAnimation()
//mBinding.linlayLoad.visibility = View.GONE
}
}
}
})
simpleExoPlayer?.prepare(videoSource)
simpleExoPlayer?.playWhenReady = true //自动播放
simpleExoPlayer?.repeatMode = Player.REPEAT_MODE_ONE //循环播放
mBinding.playerView.player = simpleExoPlayer;
}
//生命周期处理
override fun onResume() {
super.onResume()
simpleExoPlayer?.play()
}
override fun onPause() {
super.onPause()
simpleExoPlayer?.pause()
// mBinding.downLoading?.clearAnimation()
}
override fun onDestroy() {
super.onDestroy()
simpleExoPlayer?.release()
}
xml布局文件:
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:controller_layout_id="@layout/playbackcontrol_view"
/>
自定义UI:playbackcontrol_view.xml
控件id不可更改 其余属性随便更改
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120px"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20px"
android:paddingLeft="40px"
android:paddingRight="40px">
<TextView
android:id="@+id/exo_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="20px"
android:textColor="@android:color/white"
android:textSize="40px" />
<TextView
android:id="@+id/exo_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20px"
android:textColor="@android:color/white"
android:textSize="40px" />
<com.google.android.exoplayer2.ui.DefaultTimeBar
android:id="@id/exo_progress"
android:layout_width="match_parent"
android:layout_height="26dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/exo_duration"
android:layout_toRightOf="@+id/exo_position"
android:layout_weight="1"
app:played_color="#FFDE81" />
</RelativeLayout>
<ImageView
android:id="@+id/exo_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/exo_controls_play" />
<ImageView
android:id="@+id/exo_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/exo_controls_pause" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/gl"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true" />
<ImageView
android:id="@+id/exo_rew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp" />
<ImageView
android:id="@+id/exo_ffwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp" />
</RelativeLayout>
</RelativeLayout>