如何知道 ExoPlayer 何时可以播放?

how to know when ExoPlayer is ready to play?

我正在用 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> is not abstract and does not override abstract method onSeekProcessed() in EventListener

我读了很多关于这个错误的页面,但我还是不明白,每次它都与我在这里尝试做的完全不同。

如果我不能添加这个事件监听器,有没有其他方法可以知道 ExoPlayer 何时可以播放?

使用 Player.DefaultEventListener - 这将允许您有选择地覆盖您感兴趣的方法。