wil*_*kas 1 android android-video-player exoplayer
我的文件res/raw夹中有一个dog.mp4视频文件,我想与ExoPlayer一起播放。我正在尝试从ExoPlayer开发人员指南(https://google.github.io/ExoPlayer/guide.html)中找出如何获取此代码行的视频Uri :
MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri,
dataSourceFactory, extractorsFactory, null, null);
Run Code Online (Sandbox Code Playgroud)
要获得它,我使用以下行:
Uri mp4VideoUri = Uri.parse("android.resources://"+getPackageName()+"/"+R.raw.dog);
Run Code Online (Sandbox Code Playgroud)
还尝试了以下语法: 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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
小智 13
根据 ExoPlayer 2.12,它更容易使用MediaItem。为了创建它,我们需要Uri可以从RawResourceDataSource.buildRawResourceUri. 就是这样。只需设置此MediaItemdoprepare()并在准备好后即可播放:
SimpleExoPlayer.Builder(view.context).build().apply {
val uri = RawResourceDataSource.buildRawResourceUri(R.raw.video)
setMediaItem(MediaItem.fromUri(uri))
prepare()
playWhenReady = true
}
Run Code Online (Sandbox Code Playgroud)
不要将您的视频从 raw 移动到任何其他文件夹
使用此代码播放视频:
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4071 次 |
| 最近记录: |