如何知道ExoPlayer何时可以玩?

时间:2018-04-26 14:42:12

标签: android event-handling exoplayer

我正在用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何时可以播放?

1 个答案:

答案 0 :(得分:1)

使用{{1}} - 这将允许您有选择地覆盖您感兴趣的方法。