📜  Android 中的 ExoPlayer 示例

📅  最后修改于: 2022-05-13 01:55:44.130000             🧑  作者: Mango

Android 中的 ExoPlayer 示例

ExoPlayerView是许多应用程序中最常用的 UI 组件之一,例如YouTube、Netflix和许多视频流平台。 ExoPlayerView 用于 Android 应用程序中的音频和视频流。许多 Google 应用程序使用 ExoPlayerView 来流式传输音频和视频。 ExoPlayer 是一个媒体播放器库,它提供了一种播放音频和视频的方法,其中包含大量自定义。它是一种用于在 Android 中与 MediaPlayer 一起播放视频和音频的替代方法。 ExoPlayer 是一个库,是在 Android 上播放音频和视频的最佳替代来源。该库还将帮助您根据我们的要求自定义您的媒体播放器。

使用 ExoPlayer 的优势

  • ExoPlayer 提供对播放列表的支持,因此您可以剪辑或合并您的媒体。
  • 在 ExoPlayer 的帮助下,您可以直接从互联网上直接获取音频和视频等媒体文件并在 ExoPlayer 中播放。
  • 它提供了视频和音频文件的流畅加密和流传输。
  • ExoPlayer 使您能够根据自己的要求自定义媒体播放器。

ExoPlayer 与 MediaPlayer

ExoPlayer

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

MediaPlayer

ExoPlayer supports dynamic streaming over HTTP.MediaPlayer does not support dynamic support over HTTP. 
It provides smooth streaming and encryption for the played video. MediaPlayer does not provide smooth streaming and encryption for the video.
ExoPlayer provides support to clip or merge your media files. MediaPlayer does not provide any support for clipping and merging of media files. 
ExoPlayer can be customized according to our requirements. MediaPlayer cannot be customized according to our requirements. 
ExoPlayer is able to stream audio and video files directly from the server without downloading. Media Player is not able to play audio and video files directly from the server. 
ExoPlayer is released in API level 16 and it will not work on the device below API level 16.Media Player was released in API level 1 and it works on all devices. 
ExoPlayer easily handles buffering of audio and video files. Media Player is not able to handle buffering of audio and videos.

Android中ExoPlayer的分步实现

我们将创建一个简单的视频播放器应用程序,在其中我们将从 URL 获取视频并在我们的 ExoPlayer 中播放该视频。请注意,我们使用Java在 Android 中实现 ExoPlayer。

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。

第 2 步:在 build.gradle(Module:app) 中添加依赖项

导航到Gradle Scripts > build.gradle(Module:app)并在依赖项部分添加以下依赖项。

添加此依赖项后同步项目。

第 3 步:在清单文件中添加 Internet 权限

导航到app > manifest 文件夹并记下它的以下权限。

第 4 步:使用 activity_main.xml

现在我们将开始在我们的 XML 文件中实现我们的 ExoPlayerView。导航到app > res > layout > activity_main.xml 。在该文件中添加以下代码。

XML


  
    
    
      


Java
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
  
public class MainActivity extends AppCompatActivity {
  
    // creating a variable for exoplayerview.
    SimpleExoPlayerView exoPlayerView;
      
    // creating a variable for exoplayer
    SimpleExoPlayer exoPlayer;
      
    // url of video which we are loading.
    String videoURL = "https://media.geeksforgeeks.org/wp-content/uploads/20201217163353/Screenrecorder-2020-12-17-16-32-03-350.mp4";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        exoPlayerView = findViewById(R.id.idExoPlayerVIew);
        try {
              
            // bandwisthmeter is used for 
            // getting default bandwidth
            BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
              
            // track selector is used to navigate between 
            // video using a default seekbar.
            TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
              
            // we are adding our track selector to exoplayer.
            exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
              
            // we are parsing a video url
            // and parsing its video uri.
            Uri videouri = Uri.parse(videoURL);
              
            // we are creating a variable for datasource factory
            // and setting its user agent as 'exoplayer_view'
            DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
              
            // we are creating a variable for extractor factory
            // and setting it to default extractor factory.
            ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
              
            // we are creating a media source with above variables 
            // and passing our event handler as null,
            MediaSource mediaSource = new ExtractorMediaSource(videouri, dataSourceFactory, extractorsFactory, null, null);
              
            // inside our exoplayer view 
            // we are setting our player
            exoPlayerView.setPlayer(exoPlayer);
              
            // we are preparing our exoplayer
            // with media source.
            exoPlayer.prepare(mediaSource);
              
            // we are setting our exoplayer
            // when it is ready.
            exoPlayer.setPlayWhenReady(true);
            
        } catch (Exception e) {
            // below line is used for 
            // handling our errors.
            Log.e("TAG", "Error : " + e.toString());
        }
    }
}


第 5 步:使用 MainActivity。 Java文件

导航到应用程序 > Java > 您的应用程序包名称 > MainActivity。 Java文件。在该文件中添加以下代码。代码中添加了注释以更详细地理解代码。

Java

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
  
public class MainActivity extends AppCompatActivity {
  
    // creating a variable for exoplayerview.
    SimpleExoPlayerView exoPlayerView;
      
    // creating a variable for exoplayer
    SimpleExoPlayer exoPlayer;
      
    // url of video which we are loading.
    String videoURL = "https://media.geeksforgeeks.org/wp-content/uploads/20201217163353/Screenrecorder-2020-12-17-16-32-03-350.mp4";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        exoPlayerView = findViewById(R.id.idExoPlayerVIew);
        try {
              
            // bandwisthmeter is used for 
            // getting default bandwidth
            BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
              
            // track selector is used to navigate between 
            // video using a default seekbar.
            TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
              
            // we are adding our track selector to exoplayer.
            exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
              
            // we are parsing a video url
            // and parsing its video uri.
            Uri videouri = Uri.parse(videoURL);
              
            // we are creating a variable for datasource factory
            // and setting its user agent as 'exoplayer_view'
            DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
              
            // we are creating a variable for extractor factory
            // and setting it to default extractor factory.
            ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
              
            // we are creating a media source with above variables 
            // and passing our event handler as null,
            MediaSource mediaSource = new ExtractorMediaSource(videouri, dataSourceFactory, extractorsFactory, null, null);
              
            // inside our exoplayer view 
            // we are setting our player
            exoPlayerView.setPlayer(exoPlayer);
              
            // we are preparing our exoplayer
            // with media source.
            exoPlayer.prepare(mediaSource);
              
            // we are setting our exoplayer
            // when it is ready.
            exoPlayer.setPlayWhenReady(true);
            
        } catch (Exception e) {
            // below line is used for 
            // handling our errors.
            Log.e("TAG", "Error : " + e.toString());
        }
    }
}

输出:

查看项目: https://github.com/ChaitanyaMunje/QRCodeGenerator/tree/ExoPlayer