首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ExoPlayer getDuration

ExoPlayer getDuration
EN

Stack Overflow用户
提问于 2016-02-10 01:16:13
回答 2查看 18.6K关注 0票数 7

我正在尝试用ExoPlayer播放url中的mp3文件。

一切都很好,但现在我想知道什么时候是安全的getDuration()的音频轨道。我只需要它一旦轨道加载。在google的示例项目中找不到它。

当我尝试在exoPlayer.prepare()之后立即获取它时,我得到了UNKNOWN_TIME

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-16 18:53:24

好的,看起来这是唯一的解决方案。

我们将listener添加到播放器中,并在状态更改为STATE_READY时获取持续时间。它显然会在每个播放/暂停动作上调用,所以我们需要用boolean durationSet标志if check包装它,以便在赛道开始时调用它一次。然后在audioPlayer.prepare()附近的某个地方使用durationSet = false

代码语言:javascript
复制
audioPlayer.addListener(new ExoPlayer.Listener() {
        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            if (playbackState == ExoPlayer.STATE_READY && !durationSet) {
                long realDurationMillis = audioPlayer.getDuration();
                durationSet = true;
            }

        }

        @Override
        public void onPlayWhenReadyCommitted() {
            // No op.
        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {
            // No op.
        }
    });
票数 22
EN

Stack Overflow用户

发布于 2020-08-09 21:52:44

代码语言:javascript
复制
private int getVideoDurationSeconds(SimpleExoPlayer player)
    {
        int timeMs=(int) player.getDuration();
        int totalSeconds = timeMs / 1000;
        return totalSeconds;
    }

只需使用exoplayer对象调用此方法,您将发现总秒数

完整方法

代码语言:javascript
复制
private String stringForTime(int timeMs) {
    StringBuilder mFormatBuilder = new StringBuilder();
    Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
    int totalSeconds = timeMs / 1000;
  //  videoDurationInSeconds = totalSeconds % 60;
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;

    mFormatBuilder.setLength(0);
    if (hours > 0) {
        return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
    } else {
        return mFormatter.format("%02d:%02d", minutes, seconds).toString();
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35298125

复制
相关文章

相似问题

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