Exoplayer的入门使用(kotlin)

  1. 首先,你需要导入ExoPlayer的包
implementation 'com.google.android.exoplayer:exoplayer:2.11.4'

我在此时查到的最新版的是2.11.4的,你们可以通过github获取到当前最新版本,地址:Exoplayer
再写出使用的仓库:

  repositories {
   
        maven {
    url 'http://maven.aliyun.com/nexus/content/groups/public' } //添加这句,使用阿里云镜像
        google()
        jcenter()
        mavenCentral()
        maven {
    url 'https://maven.google.com' }
    }

此时进行rebuild你将报错,报错信息为:

Invoke-customs are only supported starting with Android O (--min-api 26)

你需要在让他支持java8,写在buiud.gradle(Module:app)中;

android {
   
	
 	buildTypes {
   。。。。。。省略不写}
 	
  	compileOptions {
   
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

到这里,前期工作就已经完成了,接下来就是使用了;

  1. 你需要一个播放器控件
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".Activity.PlayerActivity"
    android:fitsSystemWindows="false">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:controller_layout_id="@layout/control_view"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

其中app:controller_layout_id="@layout/control_view"为我自定义了控制器(播放,暂停等),其中可以将不需要的控件进行删除,需要的控件(播放、暂停)等,你不能改变id;下面是自定义的播放控制器;如果你不需要自定义,将这句代码:app:controller_layout_id="@layout/control_view"删除即可,会使用默认的控制器;控制器的代码你可以通过全局搜索:exo_playback_control_view.xml拿到;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#A4000000"
    android:layoutDirection="ltr"
    android:orientation=
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值