创建一个简单的 exoplayer 以从 url 流式传输视频

问题描述

我是 android studio 的完全初学者。

我正在开发一个简单的应用程序,我必须在其中从 URL 流式传输视频。只是一个简单的 Exoplayer 我尝试了下面的代码,但它不起作用。如果有人知道请帮忙。

XML

<com.google.android.exoplayer2.ui.PlayerView
    android:id="@+id/exoplayer"
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:foregroundGravity="center"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

JAVA

    PlayerView playerView;
    SimpleExoPlayer simpleExoPlayer;

    simpleExoPlayer = new SimpleExoPlayer.Builder(this).build();
    playerView = findViewById(R.id.exoPlayerView);
    playerView.setPlayer(simpleExoPlayer);
    MediaItem mediaItem = MediaItem.fromUri(my video url);
    simpleExoPlayer.addMediaItem(mediaItem);
    simpleExoPlayer.prepare();
    simpleExoPlayer.play();

解决方法

首先确保您已将 Internet 权限添加到清单 <uses-permission android:name="android.permission.INTERNET" />。之后,您可以尝试将 android:usesCleartextTraffic="true" 添加到应用程序标记的清单中。

以下是清单文件的示例:

<manifest ...>
  ...
  <uses-permission android:name="android.permission.INTERNET"/>
  ...
  <application
    android:usesCleartextTraffic="true"
    ...>

    ...

  </application>
</manifest>

在你的 *.java 类中添加这个

simpleExoPlayer = new SimpleExoPlayer.Builder(this).build();
playerView = findViewById(R.id.exoPlayerView);
playerView.setPlayer(simpleExoPlayer);
MediaItem mediaItem = MediaItem.fromUri("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");
simpleExoPlayer.addMediaItem(mediaItem);
simpleExoPlayer.prepare();
simpleExoPlayer.setPlayWhenReady(true);

从 URL 播放视频时需要 setPlayWhenReady(true)

  • ... 代表空格 - 或您的数据/内容
,

它对我有用

exo_player_view 在你的 activity_main.xml

中添加这个 ExoplayerView
  <com.google.android.exoplayer2.ui.PlayerView
        app:use_controller="true"
        android:id="@+id/exo_player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:controller_layout_id="@layout/exoplayer_controller"
        app:fastforward_increment="10000"
        app:hide_on_touch="true"
        app:player_layout_id="@layout/exo_player_view"
        app:resize_mode="fixed_width"
        app:rewind_increment="10000"
        app:show_timeout="2000"/>

exoplayer_controller

为 exoplayer_controller.xml 创建一个新布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/exo_controller"
android:background="#80000000"
android:orientation="horizontal">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="end">
    <ImageView
        android:layout_margin="10dp"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/ic_video_settings_white_18dp"
        android:id="@+id/exo_track_selection_view"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1x"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:clickable="true"
        android:id="@+id/exo_playback_speed"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:id="@+id/exo_subtitle"
        android:src="@drawable/exo_ic_subtitle_off"/>
    <ImageView
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exo_sound"
        android:src="@drawable/ic_surround_sound_white"/>
    <ImageView
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exo_bookmark"
        android:src="@drawable/ic_bookmark"/>

    <ImageView
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exo_settings"
        android:src="@drawable/ic_more_vert"/>

</LinearLayout>

<LinearLayout
    android:weightSum="4"
    android:id="@+id/control_Set"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/exo_rew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="44dp"
        android:src="@drawable/ic_10sec_back" />

    <ImageView
        android:layout_weight="1"
        android:id="@+id/exo_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_play_circle"/>

    <ImageView
        android:layout_weight="1"
        android:id="@+id/exo_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_pause_circle"/>

    <ImageView
        android:id="@+id/exo_ffwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:src="@drawable/ic_10sec_forward" />
</LinearLayout>

<LinearLayout
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layoutDirection="rtl">
    <ImageView
        android:layout_gravity="end"
        android:layout_marginBottom="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exo_fullscreen"/>
    <TextView
        android:text="duration"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:id="@+id/exo_duration"/>
    <com.google.android.exoplayer2.ui.DefaultTimeBar
        android:layout_width="0dp"
        android:layout_height="26dp"
        android:layout_weight="1"
        android:id="@+id/exo_progress"
        app:played_color="#FF0000"
        app:buffered_color="#FFFFFF"
        app:unplayed_color="#707070"/>
    <TextView
        android:text="position"
        android:layout_marginRight="4dp"
        android:layout_marginLeft="4dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:id="@+id/exo_position"/>
</LinearLayout>

课堂


最后用 URL

播放视频
SimpleExoPlayer simpleExoPlayer;
PlayerView playerView;

MediaItem mediaItem = MediaItem.fromUri(link);
playerView.setPlayer(simpleExoPlayer);
simpleExoPlayer.setMediaItem(mediaItem);
simpleExoPlayer.prepare();
simpleExoPlayer.pause();

simpleExoPlayer.addListener(new Player.Listener() {
        @Override
        public void onPlaybackStateChanged(int state) {
            if (state == simpleExoPlayer.STATE_BUFFERING){
                // hide controls
                // show progress
            }else {
                // hide progress and controls
            }
        }
    });

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...