我正在使用Exoplayer演示应用,并希望从SD卡预加载MP4视频。 我已经尝试了this post的实现,但它不起作用。在我的exoplayer Demo中没有名为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),
};
我也无法使用他们为SampleChooserActivity.java提到的代码片段。 (保持给我错误)
我改为使用:
group = new SampleGroup("Local Videos");
group.addAll(Samples.LOCAL_VIDEOS);
sampleGroups.add(group);
我做错了什么?每个设备的文件路径是否都会更改?
答案 0 :(得分:19)
Haven没有尝试过演示应用程序,但我设法创建了自己的播放本地音频文件的示例并在此处发布: https://github.com/nzkozar/ExoplayerExample
以下是从文件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);
}
你可以像这样得到Uri:Uri.fromFile(file)
如上所示准备好文件进行播放后,只需拨打exoPlayer.setPlayWhenReady(true);
即可开始播放。
对于视频文件,您可能只需要将表面视图附加到exoPlayer对象,但我还没有真正使用ExoPlayer2完成此操作。
答案 1 :(得分:2)
对于那些想使用 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);
答案 2 :(得分:1)
来自SD卡的视频播放使用以下代码。我的测试文件位于sdcard的视频目录中。
public static final Sample[] LOCAL_VIDEOS = new Sample[] {
new Sample("test",
Environment.getExternalStorageDirectory()+"/Videos/test.mp4", Util.TYPE_OTHER),
};
答案 3 :(得分:1)
如果您需要从assets
字段中获取数据,则可以使用。这不会从SD卡中获取数据
package com.studio.mattiaferigutti.exoplayertest
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")
}
}
public override fun onResume() {
super.onResume()
hideSystemUi()
if (Util.SDK_INT <= 23 || player == null) {
initializePlayer("assets:///your_file.your_extension")
}
}
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
}
}
答案 4 :(得分:0)
在某些设备中,您可以直接使用此路径&#39; /sdcard/nameoffile.mp4"。
答案 5 :(得分:0)
这对我有用。请尝试以下步骤:
获取文件的路径并启动播放器
File myFile = new File(extStore.getAbsolutePath() + "/folder/videos/" + video_name);
videoUrl= String.valueOf(Uri.fromFile(myFile));
initializePlayer(videoUrl);
初始化玩家
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);
}
构建媒体源
private MediaSource buildMediaSource(Uri uri) {
return new ExtractorMediaSource.Factory(
new DefaultDataSourceFactory(getActivity(),"Exoplayer-local")).
createMediaSource(uri);
}
答案 6 :(得分:0)
对于希望从资产文件夹加载的用户:
assets / xyz.mp4
通过将文件加载为:
“文件:/android_asset/xyz.mp4”
并使用DefaultDataSourceFactory。在exoplayer 2.4.4上检查。
答案 7 :(得分:0)
对于Exo player 2.13.3:
implementation 'com.google.android.exoplayer:exoplayer:2.13.3'
将您的视频放在资产文件夹中:
const val VIDEO_URL = "asset:///sample.mkv"
初始化播放器:
val mediaItem: MediaItem = MediaItem.fromUri(VIDEO_URL)
player.setMediaItem(mediaItem)
答案 8 :(得分:-1)
对于那些尝试播放 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();
}
}