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

Create a simple exoplayer to stream video from url

我是 android 工作室的初学者。

我正在开发一个简单的应用程序,我必须在其中从 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>

在你的*.javaclass中添加这个

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)

  • ... 代表空格 - 或者你的 data/content

它适合我

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>

在-class


最后用 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
            }
        }
    });