百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

ExoPlayer系列(1)-自定义UI

bigegpt 2024-08-21 12:01 2 浏览

  • ExoPlayer布局
  • 修改属性
  • 复写布局文件
  • 自定义布局文件
  • 三种自定义的实现方式。
  • 总结
  • 参考资料

ExoPlayer布局

ExoPlayer包含了几个UI的控件,最主要的是下面的两个:

  • StyledPlayerView它是一个播放界面的UI控件,包括展示Video,字幕,音乐播放中的专辑画面,还有一些控制播放的UI,这部分的UI被封装在了StyledPlayerControlView中,只不过StyledPlayerView依赖了它
  • StyledPlayerControlView它是一个控制ExoPlayer的UI,里面包含了,播放,暂停,快进,快退,进度条等UI的显示。

上面这两个控件是ExoPlayer库中定义好的,可以拿来即用,但是作为一个设计优秀的库,这些UI控件不是必须的,开发者可以自定义自己的UI,也就是不使用上面的UI控件,自己开发一套UI,但是这种情况带来的后果就是加大了开发者的负担。但是,ExoPlayer的优秀之处是又提供了一种更为方便的自定义UI的方式,还是使用上面的2个控件配合下面三种方式去自定义UI:

  • 通过修改这两个控件的attributes的属性(或者调用相应的方法)。
  • 通过全局的复写这几个控件的布局文件。
  • 对于某个UI,自定义布局文件。

稍微介绍一下上面的方式,就是整个控件框架还是使用上面的两个控件,通过修改它们的属性和自定义布局文件来达到自定义UI的效果,为了有一个更为直观的认识,下面通过一些示例,解释一下上面提到的3种方式。

修改属性

比如修改

app:resize_mode="fill"

则为效果图填充效果,如果修改为

app:resize_mode="fit"

则为自适应效果。

  <com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@+id/style_player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
-       app:resize_mode="fill"
+       app:resize_mode="fit"
        app:hide_on_touch="true"
        app:show_timeout="5000"
        />

左图为fill,右图为fit

复写布局文件

在我们app的res目录定义一个布局文件,名称必须定义成exo_styled_player_control_view.xml,里面控件id的名称是固定的,必现是ExoPlayer定义好的,比如下面控制播放和暂停的按钮必id必须是`exo_play_pause

<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_pause"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="@color/design_default_color_error"
        style="@style/ExoMediaButton.Play"/>


</FrameLayout>

device-2021-12-29-200806

自定义布局文件

如果想替换StyledPlayerView中默认的播放控制的UI,可以复写其布局文件,做法是把controller_layout_id替换成我们自己的布局文件custom_control_layout。

<com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@+id/style_player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:resize_mode="fit"
        app:hide_on_touch="true"
        app:show_timeout="5000"
 +      app:controller_layout_id="@layout/custom_control_layout"
        />

但是custom_control_layout.xml这个布局有一定的要求,那就是控件id的名称是固定的,必现是ExoPlayer定义好的,比如下面控制播放和暂停的按钮必id必须是exo_play_pause.具体这些id是什么,可以查阅StyledPlayerControlView这个类。

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


    <View android:id="@id/exo_controls_background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/exo_black_opacity_60"/>


    <LinearLayout
        android:id="@id/exo_center_controls"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:padding="@dimen/exo_styled_controls_padding"
        android:clipToPadding="false">


        <ImageView android:id="@id/exo_prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_custom_previous"
            android:layout_gravity="center"
            />


        <ImageView android:id="@id/exo_play_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_custom_play_pasue"/>


        <ImageView
            android:id="@id/exo_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_custom_next"
            android:layout_gravity="center"
            />
    </LinearLayout>


</merge>

三种自定义的实现方式。

上面的三种自定义的实现方式,有助于我们自己写自定义View的时候借鉴,下面介绍一下其具体的实现方式,方便我们借鉴。这样,我们后面写的自定义View,功能可以更多,扩展性更好。

  • 修改属性

其实很好理解,自定义View的时候,预置了很多的属性,通过修改这些属性达到自定义Ui的作用,下面的代码就是上面设置resizeMode的实现方式。

public StyledPlayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
 		...
    int resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT;
    
    if (attrs != null) {
      TypedArray a =
          context
              .getTheme()
              .obtainStyledAttributes(
                  attrs, R.styleable.StyledPlayerView, defStyleAttr, /* defStyleRes= */ 0);
      try {
       ...
        resizeMode = a.getInt(R.styleable.StyledPlayerView_resize_mode, resizeMode);
    	...
      } finally {
        a.recycle();
      }
      ...
    }
  }
  • 重写布局文件

上面的例子,在app moudule重写了exo_styled_player_control_view.xml布局文件,其实在StyledPlayerControlView 里有个同名的布局文件。在运行时,app module的布局文件会覆盖exoplayer库里的同名布局文件。但是这里要注意,在布局文件中使用的控件id,必现是StyledPlayerView或者StyledPlayerControlView中定义好的。

public StyledPlayerControlView(
      Context context,
      @Nullable AttributeSet attrs,
      int defStyleAttr,
      @Nullable AttributeSet playbackAttrs) {
    super(context, attrs, defStyleAttr);
    int controllerLayoutId = R.layout.exo_styled_player_control_view;
  ...
}
  • 自定义布局文件
public StyledPlayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    ...
    int playerLayoutId = R.layout.exo_styled_player_view;
   
    if (attrs != null) {
      TypedArray a =
          context
              .getTheme()
              .obtainStyledAttributes(
                  attrs, R.styleable.StyledPlayerView, defStyleAttr, /* defStyleRes= */ 0);
      try {
     		...
        playerLayoutId =
            a.getResourceId(R.styleable.StyledPlayerView_player_layout_id, playerLayoutId);
     
      } finally {
        a.recycle();
      }
    }
	...
    LayoutInflater.from(context).inflate(playerLayoutId, this);
   ...
  }
  • 上面的代码解释了,如果我们定义一个新的布局,然后可以替换StyledPlayerView中的默认布局。StyledPlayerControlView也是类似的原理。

总结

本节主要描述了如何使用ExoPlayer提供的UI控件来自定义播放界面,下一节将讲解一下Track selection

参考资料

  • Sample地址
  • ExoPlayer官方文档

相关推荐

悠悠万事,吃饭为大(悠悠万事吃饭为大,什么意思)

新媒体编辑:杜岷赵蕾初审:程秀娟审核:汤小俊审签:周星...

高铁扒门事件升级版!婚宴上‘冲喜’老人团:我们抢的是社会资源

凌晨两点改方案时,突然收到婚庆团队发来的视频——胶东某酒店宴会厅,三个穿大红棉袄的中年妇女跟敢死队似的往前冲,眼瞅着就要扑到新娘的高额钻石项链上。要不是门口小伙及时阻拦,这婚礼造型团队熬了三个月的方案...

微服务架构实战:商家管理后台与sso设计,SSO客户端设计

SSO客户端设计下面通过模块merchant-security对SSO客户端安全认证部分的实现进行封装,以便各个接入SSO的客户端应用进行引用。安全认证的项目管理配置SSO客户端安全认证的项目管理使...

还在为 Spring Boot 配置类加载机制困惑?一文为你彻底解惑

在当今微服务架构盛行、项目复杂度不断攀升的开发环境下,SpringBoot作为Java后端开发的主流框架,无疑是我们手中的得力武器。然而,当我们在享受其自动配置带来的便捷时,是否曾被配置类加载...

Seata源码—6.Seata AT模式的数据源代理二

大纲1.Seata的Resource资源接口源码2.Seata数据源连接池代理的实现源码3.Client向Server发起注册RM的源码4.Client向Server注册RM时的交互源码5.数据源连接...

30分钟了解K8S(30分钟了解微积分)

微服务演进方向o面向分布式设计(Distribution):容器、微服务、API驱动的开发;o面向配置设计(Configuration):一个镜像,多个环境配置;o面向韧性设计(Resista...

SpringBoot条件化配置(@Conditional)全面解析与实战指南

一、条件化配置基础概念1.1什么是条件化配置条件化配置是Spring框架提供的一种基于特定条件来决定是否注册Bean或加载配置的机制。在SpringBoot中,这一机制通过@Conditional...

一招解决所有依赖冲突(克服依赖)

背景介绍最近遇到了这样一个问题,我们有一个jar包common-tool,作为基础工具包,被各个项目在引用。突然某一天发现日志很多报错。一看是NoSuchMethodError,意思是Dis...

你读过Mybatis的源码?说说它用到了几种设计模式

学习设计模式时,很多人都有类似的困扰——明明概念背得滚瓜烂熟,一到写代码就完全想不起来怎么用。就像学了一堆游泳技巧,却从没下过水实践,很难真正掌握。其实理解一个知识点,就像看立体模型,单角度观察总...

golang对接阿里云私有Bucket上传图片、授权访问图片

1、为什么要设置私有bucket公共读写:互联网上任何用户都可以对该Bucket内的文件进行访问,并且向该Bucket写入数据。这有可能造成您数据的外泄以及费用激增,若被人恶意写入违法信息还可...

spring中的资源的加载(spring加载原理)

最近在网上看到有人问@ContextConfiguration("classpath:/bean.xml")中除了classpath这种还有其他的写法么,看他的意思是想从本地文件...

Android资源使用(android资源文件)

Android资源管理机制在Android的开发中,需要使用到各式各样的资源,这些资源往往是一些静态资源,比如位图,颜色,布局定义,用户界面使用到的字符串,动画等。这些资源统统放在项目的res/独立子...

如何深度理解mybatis?(如何深度理解康乐服务质量管理的5个维度)

深度自定义mybatis回顾mybatis的操作的核心步骤编写核心类SqlSessionFacotryBuild进行解析配置文件深度分析解析SqlSessionFacotryBuild干的核心工作编写...

@Autowired与@Resource原理知识点详解

springIOCAOP的不多做赘述了,说下IOC:SpringIOC解决的是对象管理和对象依赖的问题,IOC容器可以理解为一个对象工厂,我们都把该对象交给工厂,工厂管理这些对象的创建以及依赖关系...

java的redis连接工具篇(java redis client)

在Java里,有不少用于连接Redis的工具,下面为你介绍一些主流的工具及其特点:JedisJedis是Redis官方推荐的Java连接工具,它提供了全面的Redis命令支持,且...