簡體   English   中英

如何從 ExoPlayer 的播放器視圖中禁用播放速度選項?

[英]How to disable playback speed option from the player view of ExoPlayer?

我正在為 stream HLS視頻點播內容構建一個應用程序。 我打算在應用程序中使用 ExoPlayer。 此外,需要從播放器視圖中禁用播放速度選項、快進選項。 究竟在哪里可以更改代碼?

如何做到這一點? 您能幫助我們完成需要完成的代碼更改嗎?

MainActivity.kt

package com.halil.ozel.exoplayerdemo

import android.app.Activity
import android.os.Bundle
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource
import com.halil.ozel.exoplayerdemo.databinding.ActivityMainBinding

class MainActivity : Activity() {

    private lateinit var binding: ActivityMainBinding
    private var exoPlayer: ExoPlayer? = null
    private var playbackPosition = 0L
    private var playWhenReady = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        preparePlayer()
    }

    private fun preparePlayer() {
        exoPlayer = ExoPlayer.Builder(this).build()
        exoPlayer?.playWhenReady = true
        binding.playerView.player = exoPlayer
        val defaultHttpDataSourceFactory = DefaultHttpDataSource.Factory()
        val mediaItem =
            MediaItem.fromUri(URL)
        val mediaSource =
            HlsMediaSource.Factory(defaultHttpDataSourceFactory).createMediaSource(mediaItem)
        exoPlayer?.setMediaSource(mediaSource)
        exoPlayer?.seekTo(playbackPosition)
        exoPlayer?.playWhenReady = playWhenReady
        exoPlayer?.prepare()
    }

    private fun releasePlayer() {
        exoPlayer?.let { player ->
            playbackPosition = player.currentPosition
            playWhenReady = player.playWhenReady
            player.release()
            exoPlayer = null
        }
    }

    override fun onStop() {
        super.onStop()
        releasePlayer()
    }

    override fun onPause() {
        super.onPause()
        releasePlayer()
    }

    override fun onDestroy() {
        super.onDestroy()
        releasePlayer()
    }

    companion object {
        const val URL =
            "some_HLS_URL.m3u8"
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:argType="http://schemas.android.com/tools"
    android:id="@+id/flRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:keepScreenOn="true">
    <com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        argType:fastforward_increment="0"
        argType:resize_mode="fixed_height"
        argType:rewind_increment="15000"
        argType:show_buffering="when_playing"
        argType:show_fastforward_button="false"
        argType:show_next_button="false"
        argType:show_previous_button="false"
        argType:show_rewind_button="false"
        argType:show_subtitle_button="false"
        argType:use_artwork="true"
        argType:use_controller="true"
        argType:use_sensor_rotation="true">
</com.google.android.exoplayer2.ui.StyledPlayerView>

    </FrameLayout>

如果您按照以下步驟操作,您的問題將得到解決:

第 1 步:您需要自定義 ExoPlayer 視圖。 創建一個新的布局文件。 exo_player_custom_view

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

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

    <FrameLayout
        android:id="@id/exo_bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/exo_styled_bottom_bar_height"
        android:layout_gravity="bottom"
        android:layout_marginTop="@dimen/exo_styled_bottom_bar_margin_top"
        android:background="@color/exo_bottom_bar_background"
        android:layoutDirection="ltr">

        <LinearLayout
            android:id="@id/exo_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|start"
            android:layoutDirection="ltr"
            android:paddingStart="@dimen/exo_styled_bottom_bar_time_padding"
            android:paddingLeft="@dimen/exo_styled_bottom_bar_time_padding"
            android:paddingEnd="@dimen/exo_styled_bottom_bar_time_padding"
            android:paddingRight="@dimen/exo_styled_bottom_bar_time_padding">

            <TextView
                android:id="@id/exo_position"
                style="@style/ExoStyledControls.TimeText.Position" />

            <TextView style="@style/ExoStyledControls.TimeText.Separator" />

            <TextView
                android:id="@id/exo_duration"
                style="@style/ExoStyledControls.TimeText.Duration" />

        </LinearLayout>


        <HorizontalScrollView
            android:id="@id/exo_extra_controls_scroll_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|end"
            android:visibility="invisible">

        </HorizontalScrollView>

    </FrameLayout>

    <View
        android:id="@id/exo_progress_placeholder"
        android:layout_width="match_parent"
        android:layout_height="@dimen/exo_styled_progress_layout_height"
        android:layout_gravity="bottom"
        android:layout_marginBottom="@dimen/exo_styled_progress_margin_bottom" />

    <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:clipToPadding="false"
        android:gravity="center"
        android:padding="@dimen/exo_styled_controls_padding">

        <ImageButton
            android:id="@id/exo_play_pause"
            style="@style/ExoStyledControls.Button.Center.PlayPause" />
    </LinearLayout>

</merge>

Step-2 :將自定義文件添加到xml文件中的相關位置。

你必須正確地給出這個值: app:controller_layout_id="@layout/exo_player_custom_view"

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:argType="http://schemas.android.com/tools"
    android:id="@+id/flRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:keepScreenOn="true">

    <com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:controller_layout_id="@layout/exo_player_custom_view"
        argType:resize_mode="fixed_width"
        argType:show_buffering="when_playing" />

</FrameLayout>

結果:

在此處輸入圖像描述

您可以簡單地隱藏圖標。 如果您查看StyledPlayerControlView.java的實現,設置按鈕的 ID R.id.exo_settings 試試這個隱藏圖標:

binding.playerView.findViewById<View>(R.id.exo_settings).isVisible = false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM