我正在尝试用ExoPlayer播放url中的mp3文件。
一切都很好,但现在我想知道什么时候是安全的getDuration()的音频轨道。我只需要它一旦轨道加载。在google的示例项目中找不到它。
当我尝试在exoPlayer.prepare()之后立即获取它时,我得到了UNKNOWN_TIME。
发布于 2016-02-16 18:53:24
好的,看起来这是唯一的解决方案。
我们将listener添加到播放器中,并在状态更改为STATE_READY时获取持续时间。它显然会在每个播放/暂停动作上调用,所以我们需要用boolean durationSet标志if check包装它,以便在赛道开始时调用它一次。然后在audioPlayer.prepare()附近的某个地方使用durationSet = false
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.
}
});发布于 2020-08-09 21:52:44
private int getVideoDurationSeconds(SimpleExoPlayer player)
{
int timeMs=(int) player.getDuration();
int totalSeconds = timeMs / 1000;
return totalSeconds;
}只需使用exoplayer对象调用此方法,您将发现总秒数
完整方法
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();
}
}https://stackoverflow.com/questions/35298125
复制相似问题