admin管理员组

文章数量:1334826

I'm getting the audio/video duration of a file without appending it to the screen. "Using the same code", when I try to get the video duration on both sides it works as expected. But when using audio files it says that the duration is 0 on Android, but it works on a desktop puter.

// Only working on Desktop
var audio = new Audio(url);

// Hide audio player
// player.appendChild(audio);

audio.addEventListener('loadedmetadata', function() {
  alert(audio.duration);
});

And the below code is working:

// Working on Desktop and Android
var video = document.createElement('video');
video.src = url;

// Hide video
// player.appendChild(video);

video.addEventListener('loadedmetadata', function() {
  alert(video.duration);
});

I'm getting the audio/video duration of a file without appending it to the screen. "Using the same code", when I try to get the video duration on both sides it works as expected. But when using audio files it says that the duration is 0 on Android, but it works on a desktop puter.

// Only working on Desktop
var audio = new Audio(url);

// Hide audio player
// player.appendChild(audio);

audio.addEventListener('loadedmetadata', function() {
  alert(audio.duration);
});

And the below code is working:

// Working on Desktop and Android
var video = document.createElement('video');
video.src = url;

// Hide video
// player.appendChild(video);

video.addEventListener('loadedmetadata', function() {
  alert(video.duration);
});
Share edited Mar 28, 2014 at 17:33 Suman Bogati 6,3511 gold badge24 silver badges36 bronze badges asked Mar 28, 2014 at 17:29 KennyKenny 1,1524 gold badges14 silver badges42 bronze badges 2
  • do you try to play the audio/video, or is that it? phones don't preload like desktops do, but a preload=all attrib might help. after you tap "play" on an audio with controls, you should be able to get the duration once the loadedmetadata fires. – dandavis Commented Jun 2, 2014 at 21:53
  • @dandavis It's working. It'd be a bug as the same code did not work two months ago. Thank you. – Kenny Commented Jun 3, 2014 at 17:22
Add a ment  | 

2 Answers 2

Reset to default 5 +50

There is a different approach you can try but, if duration doesn't work with your device (which IMO is a bug) then it's likely this doesn't either; worth a shot though:

audio.seekable.end(audio.seekable.length-1);

or even

audio.buffered.end(audio.buffered.length-1);

though the latter is dependent on content being loaded which in this case probably then won't help.

EDIT: Using the durationchange event is much easier. First the 0 is being output, but as soon as the file is loaded (that's where loadedmetadata fails I guess) the updated and real duration will be output.

audio.addEventListener('durationchange', function(e) {
    console.log(e.target.duration); //FIRST 0, THEN REAL DURATION
});



OLD WAY (ABOVE IS MUCH FASTER)

Looks like this "bug" (if this is actually a real bug) is still around. Chrome (40) for Android still outputs 0 as the audio files duration. Researching the web didn't get me a solution but I found out the bug also occurs on iOS. I figured I should post my fix here for you guys.

While audio.duration outputs 0, logging audio outputs the object and you can see that the duration is displayed just right there. All this is happening in the loadedmetadata event.

audio.addEventListener('loadedmetadata', function(e) {
    console.log(e.target.duration); //0
});

If you log audio.duration in the timeupdate event though, the real duration is being output. To only output it once you could do something like:

var fix = true;
audio.addEventListener('timeupdate', function(e) {
    if(fix === true) {
        console.log(e.target.duration); //REAL DURATION
        fix = false;
    }
    console.log(e.target.currentTime); //UPDATED TIME POSITION
});

I'm not sure why all this is happening. But let's be happy it's nothing serious.

本文标签: javascriptGet audio duration on Chrome for AndroidStack Overflow