ExoPlayer sometimes returns negative duration for an mp3 file from url
Asked Answered
C

2

5

I'm making an app where I play .mp3 files from the URL. I'm using the latest version of ExoPlayer 2.11.4.

What I need to do is get the total duration of the audio from the url so I can use it in my custom audio player.

The urls I'm using are of this type: https://myserver.net/.../audio/439688e0c3626d39d3ef3.mp3?token=6oz-22-2sa-9yh-7e-2iak

The problem is that sometimes my code works correctly most of the time and returns the correct duration. But sometimes what I get is a negative number: -9223372036854775807

And that doesn't allow my code to work properly. My code where I get the duration is this:

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()
                        }
                    }

                }
            }
        })
    }
Casual answered 29/4, 2020 at 16:47 Comment(2)
did you find any solution?Imputable
@Imputable unfortunately not, and I am no longer working on that projectCasual
A
5

C.TIME_UNSET is defined as Long.MIN_VALUE + 1 which is where your -9223372036854775807 comes from.

  /**
   * 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;
Avocation answered 22/1, 2021 at 23:5 Comment(0)
A
2

I spent hours looking for a solution for this problem. I took help from the automatic notification feature of media3. Once you set up the controller, from controller.get(). You can use that to get the total duration.

val sessionToken =
        SessionToken(context, ComponentName(context, ExoplayerService::class.java))
    controller = MediaController.Builder(context, sessionToken).buildAsync()
    controller.addListener({
        val mediaController = controller.get()
        mediaController.addListener(object : Player.Listener {
            //to sync with inside app player media play/pause button
            override fun onIsPlayingChanged(isPlaying: Boolean) {
                super.onIsPlayingChanged(isPlaying)
                val duration = mediaController.duration
                Log.d(LOG, "Duration Controller: $duration")
            }
        })
    }, MoreExecutors.directExecutor())

This seems to be working in my case. If I am doing something wrong, let me know.

Answerable answered 26/5, 2023 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.