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

标签 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 何时可以播放?

最佳答案

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

关于android - 如何知道 ExoPlayer 何时可以播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50045884/

相关文章:

android - 将项目导入工作区时遇到问题

javascript - 如何在 body 的输入元素上委托(delegate) 'change' 事件?

android - 如何使用 Exoplayer 使视频全屏而不拉伸(stretch)

android - 偶尔出现 "Too much time to handle tune request"错误

android - 获取 listView 中的值微调器(android)

android - 保护 Android 应用 Activity

c++ - Poco C++ 事件是如何处理的?

java - 如何防止 'Packet Capture' 在 Android 应用程序中使用 exoplayer 库获取视频播放的 url

java - 在 API 8 中调用 PreferenceFragment 方法

c# - 如何通过委托(delegate)使用 void 函数?