【问题标题】:How to remove Next and Previous button from SimpleExoPlayerView in Android如何从 Android 中的 SimpleExoPlayerView 中删除下一个和上一个按钮
【发布时间】:2017-09-24 20:53:05
【问题描述】:

我使用 ExoPlayer 在我的 Android 应用上播放音频,SimpleExoPlayerView 作为控制器。默认控制器有五个按钮,播放/暂停、前进、后退、下一个和上一个。我的应用程序只使用播放/暂停、前进和后退,所以我想从控制器中删除下一个和上一个按钮,但我找不到使用 SimpleExoPlayerView 的方法。我如何做到这一点?

【问题讨论】:

    标签: android exoplayer


    【解决方案1】:

    每个按钮都有默认样式。只需覆盖上一个和下一个按钮的样式并添加可见性消失。
    在您的 style.xml 中添加以下样式

    <style name="ExoMediaButton.Previous">
        <item name="android:visibility">gone</item>
    </style>
    
    <style name="ExoMediaButton.Next">
        <item name="android:visibility">gone</item>
    </style>
    

    【讨论】:

    • 我试过 findViewById(R.id.exo_prev).setVisibility(View.GONE); findViewById(R.id.exo_next).setVisibility(View.GONE);没用,这个肯定没用!
    • 短而干净
    • 这确实隐藏了按钮,但是它们仍然存在,当用户点击他们的位置时,它们将按预期工作 - 这可能不是阅读此答案的人想要实现的......
    【解决方案2】:

    您需要一个自定义布局exo_playback_control_view.xml 文件,这是exoplayer 默认情况下查找的用于膨胀控制视图的文件。 将自定义布局命名为 exo_playback_control_view 并使用某些 ids 很重要

    默认控件可以在源代码here release-v2here dev-v2-r2.3.1中看到(确保找到您正在使用的exoplayer的版本)

    您可以将该文件复制到您的 res/layout 目录中,并删除不需要的按钮,这是默认的样子:

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


    medium 上还有一个post,这对自定义exoplayer 有很大帮助;作者写了一个自定义控件:
    <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"/>
    
    </FrameLayout>
    

    注意ids 是必需的

    【讨论】:

    • 我尝试创建一个名为 exo_playback_control_view.xml 的新布局。因为我只需要删除 next-prev 按钮,所以我只是从原始 xml 文件中复制并删除了我不需要的标签。但是,当我执行我的应用程序时,我得到了错误膨胀类 com.google.android.exoplayer2.ui.DefaultTimeBar。我使用 Proguard,但由于它处于调试状态,我认为它应该不是问题,不是吗?
    • 我不确定,应用程序是否运行?时间栏有问题吗?
    • 你提供的链接和我的版本不一样。所以我需要从同一个版本树中复制。现在已经解决了,谢谢。
    • 它是什么版本?可以分享链接吗?
    • 这个方法恐怕在当前版本中根本行不通。
    【解决方案3】:

    在您的 style.xml 中尝试以下操作

        <style name="ExoMediaButton.Previous">
            <item name="android:layout_height">0dp</item>
            <item name="android:layout_width">0dp</item>
        </style>
    
        <style name="ExoMediaButton.Next">
            <item name="android:layout_height">0dp</item>
            <item name="android:layout_width">0dp</item>
        </style>
    

    【讨论】:

    • 简单流畅。
    【解决方案4】:

    注意按钮的可见性是动态改变的
    所以最好将它们的大小设置为零以完全隐藏

        <ImageButton android:id="@id/exo_prev"
            android:layout_width="0dp"
            android:layout_height="0dp"
            style="@style/ExoMediaButton.Previous"
            android:visibility="gone"/>
    
        <ImageButton android:id="@id/exo_rew"
            style="@style/ExoMediaButton.Rewind"/>
    
        <ImageButton android:id="@id/exo_shuffle"
            style="@style/ExoMediaButton.Shuffle"
            android:visibility="gone"/>
    
        <ImageButton android:id="@id/exo_repeat_toggle"
            style="@style/ExoMediaButton"
            android:visibility="gone"/>
    
        <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"
            android:layout_width="0dp"
            android:layout_height="0dp"
            style="@style/ExoMediaButton.Next"
            android:visibility="gone"/>
    
        <ImageButton android:id="@+id/exo_fullscreen"
            android:src="@drawable/fullscreen"
            style="@style/ExoMediaButton"/>
    
        <ImageButton android:id="@+id/exo_vr"
            style="@style/ExoMediaButton"
            android:visibility="gone"/>
    

    【讨论】:

      【解决方案5】:

      exoplayer 的 PlayerView 类有两种隐藏方法

          playerView.setShowNextButton(showNextPrevButtons)
          playerView.setShowPreviousButton(showNextPrevButtons)
      

      【讨论】:

        【解决方案6】:

        你应该hideControl(),我也这样做。

        1. SimpleExoPlayerView一样创建CustomSimpleExoPlerView
        2. 评论所有行有showControl()
        3. 在xml中设置CustomSimpleExoPlayer

        【讨论】:

          猜你喜欢
          • 2019-10-30
          • 1970-01-01
          • 1970-01-01
          • 2017-10-28
          • 2018-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-23
          相关资源
          最近更新 更多