android exoplayer最好用的视频播放器,倍速播放

集成安卓播放器

 

1,主要内容来自官方文档,下面为本人学习后总结的一个播放器。

官方文档:https://exoplayer.dev/hello-world.html

2,第一步:添加依赖,和java 8语法支持

implementation 'com.google.android.exoplayer:exoplayer:2.10.5'

 

3,第二步,创建布局和实现

class PlayerActivity : BaseActivityK() {
    override val contentViewId = com.robot.ocean9.R.layout.play_act
    lateinit var player: SimpleExoPlayer
    lateinit var videoSource: ProgressiveMediaSource

    override fun init() { 
       //创建播放实例
        player = ExoPlayerFactory.newSimpleInstance(this)
      //播放实例赋值给UI。
        play_view.player = player
      //数据源
        var dataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, "hua"))
        var url = "http://clips.vorwaerts-gmbh.de/VfE_html5.mp4"
        videoSource =
            ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(url))
        player.prepare(videoSource)
      //缓存好自动播放
        player.playWhenReady = true

    }

    override fun widgetListener() {
       //一倍播放速率
        btn_cut.setOnClickListener {
            player.playbackParameters = PlaybackParameters(1f)
        }
      //3倍播放速度
        btn_cut2.setOnClickListener {
            player.playbackParameters = PlaybackParameters(3f)
        }


    }


    override fun onDestroy() {
        super.onDestroy()
        player.release()//释放资源
    }

}

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"> 
    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/play_view"
        android:layout_width="match_parent"
        android:layout_height="200dp" /> 
    <Button
        android:id="@+id/btn_cut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="one" />

    <Button
        android:id="@+id/btn_cut2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="two" /> 
</LinearLayout>

4,运行项目,就可以播放了。更多功能看官方文档

5,如果想自定义视频控件,可以代替原生的控件。控件名字为exo_player_control_view.xml

只要把控件的Id和原生的保持一致,功能就可以保存。例如原生的播放按钮id 为 exo_play

下面为我自定义的控件。如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton android:id="@id/exo_play"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="#CC000000"
        style="@style/ExoMediaButton.Play"/>

    <ImageButton android:id="@id/exo_pause"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="#CC000000"
        style="@style/ExoMediaButton.Pause"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginTop="4dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView android:id="@id/exo_position"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textStyle="bold"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:includeFontPadding="false"
            android:textColor="#FFBEBEBE"/>

        <View android:id="@id/exo_progress_placeholder"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="26dp"/>

        <TextView android:id="@id/exo_duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textStyle="bold"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:includeFontPadding="false"
            android:textColor="#FFBEBEBE"/>

    </LinearLayout>

</FrameLayout>

 

运行后如下:

 

到此完成,多么完美的exoplayer

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值