我正在尝试使用ExoPlayer
API和版本'com.google.android.exoplayer:exoplayer:2.8.1'
的exoplayer库播放视频。我想播放一个名为video.mp4
的视频,该视频位于一个名为folder1
的文件夹中,并且该文件夹位于assets
(res
)的一个文件夹res/assets/folder1/video.mp4
中。我无法获取播放视频的代码。请帮助我。
我的MainActivity.java
:
package com.example.amandeep.example2;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ConcatenatingMediaSource;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MergingMediaSource;
import com.google.android.exoplayer2.source.SingleSampleMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
SimpleExoPlayer video_player;
PlayerView player_screen;
DefaultTrackSelector track_selector;
DefaultBandwidthMeter band_width_meter = new DefaultBandwidthMeter();
MediaSource mediaSource;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
player_screen = findViewById (R.id.player_screen);
player_screen.requestFocus();
TrackSelection.Factory video_track_selection_factory = new AdaptiveTrackSelection.Factory(band_width_meter);
track_selector = new DefaultTrackSelector(video_track_selection_factory);
video_player = ExoPlayerFactory.newSimpleInstance(this, track_selector);
player_screen.setPlayer(video_player);
video_player.setPlayWhenReady(true);
DataSource.Factory data_source_factory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "Application Name"), new DefaultBandwidthMeter());
Uri url = Uri.parse("file:///android_asset/folder/video.mp4");
mediaSource = new ExtractorMediaSource.Factory(data_source_factory).createMediaSource(url);
video_player.prepare(mediaSource);
}
}
我的activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/player_screen"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:use_controller="true" />
</android.support.constraint.ConstraintLayout>
注意:我想使用Uri.parse
方法。
如果有人不能正确理解我的问题,请在下面评论。
如果URI.PARSE方法无法使用资产中的文件,请告诉我
答案 0 :(得分:3)
ExoPlayer 2.12 引入了 MediaItem
类,因此您可以:
val firstVideoUri = Uri.parse("asset:///localfile.mp4")
val firstItem = MediaItem.fromUri(firstVideoUri)
player.addMediaItem(firstItem)
注意 URI 应该以 asset:///
开头,而不是 assets:///
答案 1 :(得分:1)
我正在使用类似的代码通过ExoPlayer从资产文件夹中播放视频,效果很好。
但是我注意到您在描述和代码中提到了不同的文件夹。您说资产中的文件夹名为 folder1 ,但是在代码中您具有 folder 。
因此,请确保您拥有正确的视频路径。
答案 2 :(得分:0)
尝试以下格式的uri:
file:///android_asset/
对于您的示例,必须为:
Uri url = Uri.parse("file:///android_asset/folder1/video.mp4");
答案 3 :(得分:0)
这是我播放音频文件的代码,希望对您有帮助。
private void prepareExoPlayerFromAssetResourceFile(int current_file) {
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector((TrackSelection.Factory) null), new DefaultLoadControl());
exoPlayer.addListener(eventListener);
//DataSpec dataSpec = new DataSpec(uri);
//DataSpec dataSpec = new DataSpec(Uri.parse("asset:///001.mp3"));
DataSpec dataSpec = new DataSpec(Uri.parse("asset:///" + current_file +".mp3"));
final AssetDataSource assetDataSource = new AssetDataSource(this);
try {
assetDataSource.open(dataSpec);
} catch (AssetDataSource.AssetDataSourceException e) {
e.printStackTrace();
}
DataSource.Factory factory = new DataSource.Factory() {
@Override
public DataSource createDataSource() {
//return rawResourceDataSource;
return assetDataSource;
}
};
MediaSource audioSource = new ExtractorMediaSource(assetDataSource.getUri(),
factory, new DefaultExtractorsFactory(), null, null);
exoPlayer.prepare(audioSource);
initMediaControls();
}
答案 4 :(得分:0)
这可能有用:
import android.annotation.SuppressLint
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.upstream.*
import com.google.android.exoplayer2.upstream.DataSource.Factory
import com.google.android.exoplayer2.util.Util
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), Player.EventListener {
private var player: SimpleExoPlayer? = null
private var playWhenReady = true
private var currentWindow = 0
private var playbackPosition: Long = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
public override fun onStart() {
super.onStart()
if (Util.SDK_INT > 23) {
initializePlayer("assets:///your_file.your_extension") //"assets:///pillwheel_pills.mp4"
}
}
public override fun onResume() {
super.onResume()
hideSystemUi()
if (Util.SDK_INT <= 23 || player == null) {
initializePlayer("assets:///your_file.your_extension") //"assets:///pillwheel_pills.mp4"
}
}
public override fun onPause() {
super.onPause()
if (Util.SDK_INT <= 23) {
releasePlayer()
}
}
public override fun onStop() {
super.onStop()
if (Util.SDK_INT > 23) {
releasePlayer()
}
}
private fun initializePlayer(path: String) {
if (player == null) {
val trackSelector = DefaultTrackSelector(this)
trackSelector.setParameters(
trackSelector.buildUponParameters().setMaxVideoSizeSd())
player = SimpleExoPlayer.Builder(this)
.setTrackSelector(trackSelector)
.build()
}
video_view?.player = player
video_view?.requestFocus()
val dataSourceFactory = Factory { AssetDataSource(this@MainActivity) }
val videoSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(path))
player?.playWhenReady = playWhenReady
player?.seekTo(currentWindow, playbackPosition)
player?.addListener(this)
player?.prepare(videoSource)
}
private fun releasePlayer() {
if (player != null) {
playbackPosition = player?.currentPosition!!
currentWindow = player?.currentWindowIndex!!
playWhenReady = player?.playWhenReady!!
player?.removeListener(this)
player?.release()
player = null
}
}
/**
* set fullscreen
*/
@SuppressLint("InlinedApi")
private fun hideSystemUi() {
video_view?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
}
override fun onPlayerError(error: ExoPlaybackException) {
super.onPlayerError(error)
//handle the error
}
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
val stateString: String = when (playbackState) {
ExoPlayer.STATE_IDLE -> "ExoPlayer.STATE_IDLE -"
ExoPlayer.STATE_BUFFERING -> "ExoPlayer.STATE_BUFFERING -"
ExoPlayer.STATE_READY -> "ExoPlayer.STATE_READY -"
ExoPlayer.STATE_ENDED -> "ExoPlayer.STATE_ENDED -"
else -> "UNKNOWN_STATE -"
}
Log.d(TAG, "changed state to " + stateString
+ " playWhenReady: " + playWhenReady)
}
companion object {
private val TAG = MainActivity::class.java.name
}
}
您可以在 GitHub
上找到完整的源代码