【问题标题】:ExoPlayer sometimes returns negative duration for an mp3 file from urlExoPlayer 有时会从 url 返回 mp3 文件的负持续时间
【发布时间】:2020-08-13 20:29:22
【问题描述】:

我正在制作一个应用程序,我可以在其中播放来自 URL 的 .mp3 文件。我正在使用最新版本的 ExoPlayer 2.11.4。

我需要做的是从 url 获取音频的总持续时间,以便我可以在我的自定义音频播放器中使用它。

我使用的网址属于这种类型:https://myserver.net/.../audio/439688e0c3626d39d3ef3.mp3?token=6oz-22-2sa-9yh-7e-2iak

问题是有时我的代码大部分时间都能正常工作并返回正确的持续时间。但有时我得到的是一个负数:-9223372036854775807

这不允许我的代码正常工作。我得到持续时间的代码是这样的:

fun getDuration(url: String, context: Context) {
        exoPlayer?.release()

        exoPlayer = SimpleExoPlayer.Builder(context).build()
        val dataSourceFactory = DefaultDataSourceFactory(context, Util.getUserAgent(context, "ExoPlayer"))
        val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(url))

        exoPlayer?.prepare(mediaSource)

        exoPlayer?.addListener(object : Player.EventListener {
            override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
                if (playbackState == ExoPlayer.STATE_READY) {
                    val realDurationMillis: Long? = exoPlayer?.getDuration()
                    currentDuration = realDurationMillis
                    if (currentDuration != null) {
                        if (currentDuration!! > 0) {
                            exoPlayer?.release()
                        }
                    }

                }
            }
        })
    }

【问题讨论】:

  • 你找到解决办法了吗?
  • @iamkdblue 很遗憾没有,我不再从事那个项目

标签: android kotlin exoplayer exoplayer2.x


【解决方案1】:

C.TIME_UNSET 定义为 Long.MIN_VALUE + 1,这是您的 -9223372036854775807 的来源。

  /**
   * Special constant representing an unset or unknown time or duration. Suitable for use in any
   * time base.
   */
  public static final long TIME_UNSET = Long.MIN_VALUE + 1;

【讨论】:

    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多