首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Exoplayer中显示视频的剩余时长

在Exoplayer中显示视频的剩余时长
EN

Stack Overflow用户
提问于 2017-10-25 17:30:48
回答 1查看 3.9K关注 0票数 0

我使用SimpleExoPlayer播放raw文件夹中默认控件的视频。一切正常,然而,我想在控制栏的右下角显示剩余的持续时间,而不是总持续时间。播放器的getDuration方法可以得到总时长,player.getDuration() - player.getCurrentPosition()可以得到剩余时长。但我不知道如何每秒更新剩余的持续时间并在视图上显示。

我如何才能做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2017-11-05 21:24:35

这是一个简单的例子,如何实现每秒更新进度。

当player不在播放时(你可以从ExoPlayer.addListener(Player.EventListener);获取事件)和当你的Activity暂停/销毁时,你仍然应该使用Handler.removeCallbacks(updateProgressAction)

代码语言:javascript
运行
复制
   public class MainActivity extends AppCompatActivity {
        private final Runnable updateProgressAction = new Runnable() {
            @Override
            public void run() {
                updateProgress();
            }
        };
        private TextView mTextView;
        private Handler mHandler;
        private SimpleExoPlayer mSimpleExoPlayer;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mTextView = findViewById(R.id.my_text);
            SimpleExoPlayerView simpleExoPlayerView = findViewById(R.id.player_view);
            mSimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(new DefaultBandwidthMeter()));
            simpleExoPlayerView.setPlayer(mSimpleExoPlayer);
            Uri uri = Uri.parse("https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8");
            String userAgent = Util.getUserAgent(this, "appName");
            HlsMediaSource mediaSource = new HlsMediaSource(uri, new DefaultDataSourceFactory(this, userAgent), null, null);
            mSimpleExoPlayer.prepare(mediaSource);
            mSimpleExoPlayer.setPlayWhenReady(true);
            mHandler = new Handler();
            mHandler.post(updateProgressAction);
        }

        private void updateProgress() {
            mTextView.setText("calculate time and format");
            long delayMs = TimeUnit.SECONDS.toMillis(1);
            mHandler.postDelayed(updateProgressAction, delayMs);
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46928804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档