Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exoplayer2 duration is -9223372036854775807, how to solve this?

I record my audio and send this to server. when I get the file from server, I can hear my audio. But the duration of that file is -9223372036854775807. I don't understand this weird... short recorded audio is fine. But when I record audio close to 1 min or over, I got this problem. The current position is logged well but not the total duration. how to solve this?

private fun initializePlayer() {
        if (player == null) {
            val trackSelector = DefaultTrackSelector()
            trackSelector.setParameters(
                trackSelector.buildUponParameters().setMaxVideoSizeSd()
            )
            player = ExoPlayerFactory.newSimpleInstance(this,trackSelector)
            binding.musicPlayer.player = player
            binding.musicPlayer.useController=false
            defaultDataSourceFactory = DefaultDataSourceFactory(this, this.getString(R.string.app_name))
            mediaSourceFactory = ProgressiveMediaSource.Factory(defaultDataSourceFactory)
            player!!.playWhenReady = false
            player!!.addListener(object : Player.EventListener {
                override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
                    when (playbackState) {
                        Player.STATE_IDLE -> {

                        }
                        Player.STATE_BUFFERING -> {

                        }
                        Player.STATE_READY -> {
                            showLog("totalDuration : "+player!!.duration)
                        }
                        Player.STATE_ENDED -> {
                            player!!.playWhenReady = false
                            activeHolder?.run {
                                binding.progressBar.progress = 0
                                showAudioStartButton(activeHolder!!)
                                activeHolder = null
                            }
                            handlerChangeProgress.removeCallbacks(updateChangeProgressTask)
                            currentAudioPosition = 0
                            maxAudioPosition = 0
                            player!!.seekTo(0)
                        }
                    }
                }
            })
        }
    }



Log

D/TAG: totalDuration : -9223372036854775807
like image 303
Charles Avatar asked Oct 20 '25 04:10

Charles


1 Answers

You have to add listener until it is prepared and only then you can get the duration:

exoPlayer.addListener(new Player.Listener() {
                @Override
                public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                    if (playbackState == ExoPlayer.STATE_READY) {
                        long realDurationMillis = exoPlayer.getDuration();
                        seekBar.setMax((int)realDurationMillis/1000);
                        dialog.dismiss();
                    }

                    if (playbackState == ExoPlayer.STATE_ENDED){
                        performEndExoPlayer();
                    }
                }
            });
like image 109
Olsi Hoxha Avatar answered Oct 22 '25 17:10

Olsi Hoxha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!