我正在用ExoPlayer替换MediaPlayer。
我遵循了指南
https://google.github.io/ExoPlayer/guide.html
public class cl_ExoPlayer {
Map<String,SimpleExoPlayer> player = new HashMap<String,SimpleExoPlayer>();
Integer are_loading = 0 ;
// 1. Create a default TrackSelector
Handler mainHandler = new Handler();
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory audioTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(audioTrackSelectionFactory);
void snd_load( String file_name , String url ) {
try {
String file_name_with_ext = url.substring(url.lastIndexOf("/") + 1); // snd_title.ogg
String file_name_without_ext = file_name_with_ext.substring(0, file_name_with_ext.indexOf(".")); // snd_title
Uri uri ;
int resId = getApplicationContext().getResources().getIdentifier(file_name_without_ext, "raw", getApplicationContext().getPackageName());
if (resId != 0){ uri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/raw/" + file_name_without_ext); }
else{ uri = Uri.parse("http://diegotests.altervista.org/html5_games/8bit_pocket_wrestlers/" + url); }
// 2. Create the player
player.put( file_name, ExoPlayerFactory.newSimpleInstance(getApplicationContext(), trackSelector) );
// Measures bandwidth during playback. Can be null if not required.
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(),
Util.getUserAgent(getApplicationContext(), "yourApplicationName"), bandwidthMeter);
// This is the MediaSource representing the media to be played.
MediaSource audioSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
// Prepare the player with the source.
are_loading++;
player.get(file_name).prepare(audioSource);
现在,我想知道何时完成文件加载,解码并准备播放。我不使用.setPlayWhenReady(),因为我不希望它在准备好后立即开始播放。我在互联网上读到的唯一方法始终是:
player.get(file_name).addListener( new Player.EventListener() {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == Player.STATE_READY) {
}
}
});
但这行不通。
我得到错误
error: <anonymous didi.a8bitpocketwrestlers.cl_ExoPlayer$1> is not abstract and does not override abstract method onSeekProcessed() in EventListener
我阅读了许多有关此错误的页面,但每次与我在此处尝试的操作都如此不同时,我仍然不明白。
如果我无法添加此事件侦听器,是否还有其他方法可以知道ExoPlayer何时可以播放?
最佳答案
使用Player.DefaultEventListener
-这将使您可以选择性地仅覆盖您感兴趣的方法。
关于android - 如何知道ExoPlayer何时可以播放?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50045884/