我在res/raw
文件夹中有一个dog.mp4视频文件,我想用ExoPlayer播放。我正试图弄清楚如何从ExoPlayer开发人员指南(https://google.github.io/ExoPlayer/guide.html)获取这行代码的视频Uri:
MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri,
dataSourceFactory, extractorsFactory, null, null);
为了得到它,我使用这一行:
Uri mp4VideoUri = Uri.parse("android.resources://"+getPackageName()+"/"+R.raw.dog);
也尝试了这种语法:android.resource://[package]/[res type]/[res name]
但SimpleExoPlayerView
保持黑色,我得到以下错误:
com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect to android.resources://lt.wilkas.deleteexoplayer/2131099648
我究竟做错了什么?
我发现res/raw
文件夹无法用于存储ExoPlayer的本地视频。它们应该放在assets
文件夹中。
不要将视频从原始文件移动到任何其他文件夹
使用此代码播放视频:
PlayerView playerView = findViewById(R.id.player_view);
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this);
// Bind the player to the view.
playerView.setPlayer(player);
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "yourApplicationName"));
// This is the MediaSource representing the media to be played.
MediaSource firstSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(RawResourceDataSource.buildRawResourceUri(R.raw.dog));
// Prepare the player with the source.
player.prepare(firstSource);