android – 使用exo播放器添加全屏视频按钮

前端之家收集整理的这篇文章主要介绍了android – 使用exo播放器添加全屏视频按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用exoPlayer将视频流式传输到我的应用程序,到目前为止工作正常.我现在要做的是添加一些额外的功能,例如右下角的按钮,作为“全屏按钮”.

但是有两个问题.第一个是ExoPlayer似乎没有提供任何Control类,因此您可以简单地添加一个按钮并覆盖其功能.
我想我必须在视频顶部显示该按钮,所以我可能必须将它们包装在FrameLayout中并为按钮添加gravity = bottom或者是否有另一种方式?

第二个问题是:如果用户点击全屏按钮,我接下来该怎么办?全屏添加另一个片段与视频视图?但是,如何从用户点击按钮开始播放视频,而不是从头开始播放?我无法在exoPlayer中找到与特定时间相关的任何内容.

解决方法

如果您使用的是SimpleExoPlayerView,则可以自定义播放器的视图,尤其是Control的视图.查看SimpleExoPlayerView的文档:

Attributes

The following attributes can be set on a SimpleExoPlayerView when used in a layout XML file:

controller_layout_id – Specifies the id of the layout resource to be inflated by the child PlaybackControlView. See below for more details.

  • Corresponding method: None

  • Default: R.id.exo_playback_control_view

因此,基本上您可以为控制器提供自己的布局文件(您可以复制文档中提到的exo_playback_control_view布局,这是默认布局文件,并根据需要自定义.请注意,您需要提供相同的视图ID现有的控件(所以最好实际复制它),如PlaybackControlView的文档中所述:

Overriding the layout file

To customize the layout of PlaybackControlView throughout your app,or just for certain configurations,you can define exo_playback_control_view.xml layout files in your application res/layout* directories. These layouts will override the one provided by the ExoPlayer library,and will be inflated for use by PlaybackControlView. The view identifies and binds its children by looking for the following ids:

  • exo_play – The play button.

  • exo_pause – The pause button.

  • exo_ffwd – The fast forward button.

  • exo_rew – The rewind button.

  • exo_prev – The prevIoUs track button.

  • exo_next – The next track button.

  • exo_position – Text view displaying the current playback position.

  • exo_duration – Text view displaying the current media duration.

  • exo_progress – Seek bar that’s updated during playback and allows seeking.

All child views are optional and so can be omitted if not required,however where defined they must be of the expected type.

以下是带全屏按钮的自定义布局.您可以通过view.findViewById(R.id.exo_fullscreen_button)获取对该按钮的引用,并将OnClickListener附加到该按钮.在onClick()中你可以开始你的全屏活动(你可以在AndroidManifest.xml中或以编程方式定义它的全屏)或显示另一个片段,其中SimpleExoPlayerView占据整个屏幕.
从第二点开始,您可以像这样获得播放位置:playbackPosition = player.getCurrentPosition()并将其作为Intent extra传递给新的全屏Activity / Fragment.然后,在全屏活动/片段中加载视频,提取该playbackPosition值并调用

player.seekTo(playbackPosition);
player.setPlayWhenReady(true);

这是控件布局文件

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingTop="4dp"
    android:orientation="horizontal">

    <ImageButton android:id="@id/exo_prev"
      style="@style/ExoMediaButton.PrevIoUs"/>

    <ImageButton android:id="@id/exo_rew"
      style="@style/ExoMediaButton.Rewind"/>

    <ImageButton android:id="@id/exo_play"
      style="@style/ExoMediaButton.Play"/>

    <ImageButton android:id="@id/exo_pause"
      style="@style/ExoMediaButton.Pause"/>

    <ImageButton android:id="@id/exo_ffwd"
      style="@style/ExoMediaButton.FastForward"/>

    <ImageButton android:id="@id/exo_next"
      style="@style/ExoMediaButton.Next"/>

    // This is the custom button
    <ImageButton
        android:id="@+id/exo_fullscreen_button"
        style="@style/ExoMediaButton"
        android:src="@drawable/ic_fullscreen"/>
  </LinearLayout>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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"/>

    <SeekBar android:id="@id/exo_progress"
      android:layout_width="0dp"
      android:layout_weight="1"
      android:layout_height="32dp"
      android:focusable="false"
      style="?android:attr/progressBarStyleHorizontal"/>

    <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>

</LinearLayout>
原文链接:https://www.f2er.com/android/309827.html

猜你在找的Android相关文章