Java ExoPlayer - 在 SD 卡中播放本地 mp4 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35675105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:59:46  来源:igfitidea点击:

ExoPlayer - play local mp4 file in SD card

javaandroidandroid-sdcardexoplayer

提问by Meera Anand

I am using the Exoplayer Demo app and want to preload a MP4 video from SD card. I have tried out the implementation from this post, but it does not work. There is no such class called DemoUtil.java in my exoplayer Demo. Instead used:

我正在使用 Exoplayer 演示应用程序并希望从 SD 卡预加载 MP4 视频。我已经尝试了这篇文章中的实现,但它不起作用。在我的 exoplayer 演示中没有名为 DemoUtil.java 的类。而是使用:

public static final Sample[] LOCAL_VIDEOS = new Sample[] {
new Sample("Some User friendly name of video 1",
"/mnt/sdcard/video1.mp4", Util.TYPE_OTHER),
};

I also could not use the their snippet of code mentioned for SampleChooserActivity.java. (Kept giving me errors)

我也无法使用他们为 SampleChooserActivity.java 提到的代码片段。(一直给我错误)

I instead used :

我改为使用:

group = new SampleGroup("Local Videos");
group.addAll(Samples.LOCAL_VIDEOS);
sampleGroups.add(group);

What am I doing wrong? Does the path of the file change for every device?

我究竟做错了什么?每个设备的文件路径都会改变吗?

回答by Srikanth Peddibhotla

In some devices you could directly used this path '/sdcard/nameoffile.mp4".

在某些设备中,您可以直接使用此路径“/sdcard/namefile.mp4”。

回答by Christo

Video playback from sd card worked with following code. My test file is in Videos directory in sdcard.

从 SD 卡播放视频使用以下代码。我的测试文件在 sdcard 的 Videos 目录中。

public static final Sample[] LOCAL_VIDEOS = new Sample[] {
        new Sample("test",
            Environment.getExternalStorageDirectory()+"/Videos/test.mp4", Util.TYPE_OTHER),
};

回答by ak93

Haven't tried the demo app, but I have managed to create my own example of playing local audio files and have posted it here: https://github.com/nzkozar/ExoplayerExample

还没有尝试过演示应用程序,但我已经设法创建了自己的播放本地音频文件的示例并将其发布在此处:https: //github.com/nzkozar/ExoplayerExample

Here is the main part that does all the work of preparing the player from a file Uri:

这是完成从文件 Uri 准备播放器的所有工作的主要部分:

private void prepareExoPlayerFromFileUri(Uri uri){
        exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(null), new DefaultLoadControl());
        exoPlayer.addListener(eventListener);

        DataSpec dataSpec = new DataSpec(uri);
        final FileDataSource fileDataSource = new FileDataSource();
        try {
            fileDataSource.open(dataSpec);
        } catch (FileDataSource.FileDataSourceException e) {
            e.printStackTrace();
        }

        DataSource.Factory factory = new DataSource.Factory() {
            @Override
            public DataSource createDataSource() {
                return fileDataSource;
            }
        };
        MediaSource audioSource = new ExtractorMediaSource(fileDataSource.getUri(),
                factory, new DefaultExtractorsFactory(), null, null);

        exoPlayer.prepare(audioSource);
    }

You can get the Uri like this: Uri.fromFile(file)

您可以像这样获取 Uri: Uri.fromFile(file)

After you have prepared your file for playback as shown above, you only need to call exoPlayer.setPlayWhenReady(true);to start playback.

如上所示准备好要播放的文件后,您只需要调用exoPlayer.setPlayWhenReady(true);即可开始播放。

For a video file you'd probably only need to attach a surface view to your exoPlayer object, but I haven't really done this with ExoPlayer2 yet.

对于视频文件,您可能只需要将表面视图附加到 exoPlayer 对象,但我还没有真正使用 ExoPlayer2 完成此操作。

回答by Sasha Shpota

For those who want to play a video from assetsusing ExoPlayer 2here is the way:

对于那些想要使用ExoPlayer 2从资产播放视频的人来说,这里是方法:

String playerInfo = Util.getUserAgent(context, "ExoPlayerInfo");
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(
        context, playerInfo
);
MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory)
    .setExtractorsFactory(new DefaultExtractorsFactory())
    .createMediaSource(Uri.parse("asset:///your_video.mov"));
player.prepare(mediaSource);

回答by Aashis Shrestha

This worked for me.Try these steps:

这对我有用。试试这些步骤:

Get path of the file and start the player

获取文件路径并启动播放器

File myFile = new File(extStore.getAbsolutePath() + "/folder/videos/" + video_name);  
videoUrl= String.valueOf(Uri.fromFile(myFile));  
initializePlayer(videoUrl);

Initializing player

初始化播放器

private void initializePlayer(String videoUrl) {
    player = ExoPlayerFactory.newSimpleInstance(
            new DefaultRenderersFactory(getActivity()),
            new DefaultTrackSelector(), new DefaultLoadControl());

    playerView.setPlayer(player);

    player.setPlayWhenReady(playWhenReady);
    player.seekTo(currentWindow, playbackPosition);

    Uri uri = Uri.parse(videoUrl);
    MediaSource mediaSource = buildMediaSource(uri);
    player.prepare(mediaSource, resetPositionBoolean, false);
}   

Building media source

建立媒体来源

  private MediaSource buildMediaSource(Uri uri) {
    return new ExtractorMediaSource.Factory(
            new DefaultDataSourceFactory(getActivity(),"Exoplayer-local")).
            createMediaSource(uri);
}

回答by petrumo

For those looking to load from the assets folder:

对于那些希望从资产文件夹加载的人:

assets/xyz.mp4

资产/xyz.mp4

it works by loading the file with:

它通过加载文件来工作:

"file:/android_asset/xyz.mp4"

“文件:/android_asset/xyz.mp4”

and using the DefaultDataSourceFactory. Checked on exoplayer 2.4.4.

并使用 DefaultDataSourceFactory。在 exoplayer 2.4.4 上检查。

回答by Usama Mehmood

For those who trying to play a video filefrom res/raw* folder, here is the solution. Keep in mind that i used the **2.8.0version of the ExoPlayer.

对于那些试图从res/raw* 文件夹播放视频文件的人,这里是解决方案。请记住,我使用ExoPlayer的 **2.8.0版本。

public class MainActivity extends AppCompatActivity {

PlayerView playerView;
SimpleExoPlayer simpleExoPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    playerView=findViewById(R.id.playerView);
}

@Override
protected void onStart() {
    simpleExoPlayer= ExoPlayerFactory.newSimpleInstance(this,new DefaultTrackSelector());
    DefaultDataSourceFactory defaultDataSourceFactory=new DefaultDataSourceFactory(this, Util.getUserAgent(this,"YourApplicationName"));
    simpleExoPlayer.setPlayWhenReady(true);
    ExtractorMediaSource extractorMediaSource=new ExtractorMediaSource.Factory(defaultDataSourceFactory).createMediaSource(RawResourceDataSource.buildRawResourceUri(R.raw.video));
    simpleExoPlayer.prepare(extractorMediaSource);
    playerView.setPlayer(simpleExoPlayer);

    super.onStart();
}

@Override
protected void onStop() {
    playerView.setPlayer(null);
    simpleExoPlayer.release();
    simpleExoPlayer=null;
    super.onStop();
}

}

}