PhoneGap unable to getDuration() out of Media API, but other methods work

前端 未结 4 2197
[愿得一人]
[愿得一人] 2021-02-15 14:29

I\'m building out an audio media recorder/player with PhoneGap. It\'s all working beautifully, but I\'ve hit a wrinkle I can\'t seem to iron.

my_media.play();

相关标签:
4条回答
  • 2021-02-15 15:03

    This solution works for me. Basically, play and immediately stop. It doesn't seem to take any time, seems like a decent workaround.

    media.play();
    media.stop();
    var length = media.getDuration();
    
    0 讨论(0)
  • 2021-02-15 15:15

    This question is too old. But it is still relevant because many might have been facing this same problem. Whenever nothing works I just do one thing, upgrade or downgrade the version. In this case I solved my problem by installing following version.

    cordova plugin add cordova-plugin-media@1.0.1
    
    0 讨论(0)
  • 2021-02-15 15:16

    I also faced similar problem in cordova for iOS. I was successfully able to record, play and stop audio but unable to get current position and total duration for audio file. So I added my_media.release() just after I was done finishing recording audio i.e. after my_media.stopRecord() and it worked like a charm. Earlier I was getting -1 for getDuration() and 0 for getCurrentPosition().

    Hope it helps someone.

    0 讨论(0)
  • The metadata for the media in question has not been loaded when you call my_media.getDuration(). In the documentation you referenced in your question the example code puts the getDuration call into an interval:

    var timerDur = setInterval(function() {
        counter = counter + 100;
        if (counter > 2000) {
            clearInterval(timerDur);
        }
        var dur = my_media.getDuration();
        if (dur > 0) {
            clearInterval(timerDur);
            document.getElementById('audio_duration').innerHTML = (dur) + " sec";
        }
    }, 100);
    

    I would recommend doing something similar.

    0 讨论(0)
提交回复
热议问题