最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PhoneGap unable to getDuration() out of Media API, but other methods work - Stack Overflow

programmeradmin2浏览0评论

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(); does indeed play the media w/o error in my Eclipse or XCode consoles which is why the alert that is showing a -1 is puzzling. I expect my_media.getDuration(); to return the duration of the file I'm attempting to play.

My try/catch block isn't throwing an error, I'm quite puzzled on this one. Here's the PhoneGap documentation on Media.getDuration().

function playAudio() {

    $('#btnStopRecording').removeClass('ui-disabled');
    $('#btnPlayMessage, #btnStartStopRecording, #btnDeleteMessage, #btnAcceptUpload').addClass('ui-disabled');

    my_media = new Media(fullRecordPath,

        // success callback
        function () {
            $('#btnPlayMessage, #btnStartStopRecording, #btnDeleteMessage, #btnAcceptUpload').removeClass('ui-disabled');
            $('#btnStopRecording').addClass('ui-disabled');
        },

        // error callback
        function (err) {
            console.log("attempting to play fullRecordPath = "+fullRecordPath);
            console.log("playAudio():Audio Error: " + err.code);
        }
    );

    var thisDuration;

    try{
        thisDuration = my_media.getDuration();
    } catch (err) {
        console.log("attempting to get duration error code "+err.code);
        console.log("attempting to get duration error message "+err.message);
    }

    alert("we're about play a file of this duration "+thisDuration);

    my_media.play();

    // stop playback when the stop button is tapped
    $('#btnStopRecording').off('tap').on('tap',function()
    {
        my_media.stop();
        $('#btnPlayMessage, #btnStartStopRecording, #btnDeleteMessage, #btnAcceptUpload').removeClass('ui-disabled');
        $('#btnStopRecording').addClass('ui-disabled');
    });

    // if the user leaves the page, stop playback
    $('#pageRecordMessage').live('pagehide', function()
    {
        my_media.stop();
        $('#btnPlayMessage, #btnStartStopRecording, #btnDeleteMessage, #btnAcceptUpload').removeClass('ui-disabled');
        $('#btnStopRecording').addClass('ui-disabled');
    });
}

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(); does indeed play the media w/o error in my Eclipse or XCode consoles which is why the alert that is showing a -1 is puzzling. I expect my_media.getDuration(); to return the duration of the file I'm attempting to play.

My try/catch block isn't throwing an error, I'm quite puzzled on this one. Here's the PhoneGap documentation on Media.getDuration().

function playAudio() {

    $('#btnStopRecording').removeClass('ui-disabled');
    $('#btnPlayMessage, #btnStartStopRecording, #btnDeleteMessage, #btnAcceptUpload').addClass('ui-disabled');

    my_media = new Media(fullRecordPath,

        // success callback
        function () {
            $('#btnPlayMessage, #btnStartStopRecording, #btnDeleteMessage, #btnAcceptUpload').removeClass('ui-disabled');
            $('#btnStopRecording').addClass('ui-disabled');
        },

        // error callback
        function (err) {
            console.log("attempting to play fullRecordPath = "+fullRecordPath);
            console.log("playAudio():Audio Error: " + err.code);
        }
    );

    var thisDuration;

    try{
        thisDuration = my_media.getDuration();
    } catch (err) {
        console.log("attempting to get duration error code "+err.code);
        console.log("attempting to get duration error message "+err.message);
    }

    alert("we're about play a file of this duration "+thisDuration);

    my_media.play();

    // stop playback when the stop button is tapped
    $('#btnStopRecording').off('tap').on('tap',function()
    {
        my_media.stop();
        $('#btnPlayMessage, #btnStartStopRecording, #btnDeleteMessage, #btnAcceptUpload').removeClass('ui-disabled');
        $('#btnStopRecording').addClass('ui-disabled');
    });

    // if the user leaves the page, stop playback
    $('#pageRecordMessage').live('pagehide', function()
    {
        my_media.stop();
        $('#btnPlayMessage, #btnStartStopRecording, #btnDeleteMessage, #btnAcceptUpload').removeClass('ui-disabled');
        $('#btnStopRecording').addClass('ui-disabled');
    });
}
Share Improve this question edited Nov 13, 2012 at 19:54 fusion27 asked Nov 13, 2012 at 19:43 fusion27fusion27 2,6561 gold badge28 silver badges25 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

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 remend doing something similar.

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

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 [email protected]

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)

  1. 暂无评论