【问题标题】:PhoneGap unable to getDuration() out of Media API, but other methods workPhoneGap 无法从 Media API 中获取 Duration(),但其他方法有效
【发布时间】:2012-11-13 19:43:06
【问题描述】:

我正在使用 PhoneGap 构建音频媒体记录器/播放器。这一切都很好,但我遇到了我似乎无法熨烫的皱纹。

my_media.play(); 确实在我的 Eclipse 或 XCode 控制台中播放没有错误的媒体,这就是为什么显示 -1 的警报令人费解的原因。我希望 my_media.getDuration(); 返回我正在尝试播放的文件的持续时间。

我的 try/catch 块没有抛出错误,我对此感到很困惑。 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');
    });
}

【问题讨论】:

    标签: javascript jquery cordova jquery-mobile


    【解决方案1】:

    当您调用 my_media.getDuration() 时,尚未加载相关媒体的元数据。在您在问题中引用的文档中,示例代码将 getDuration 调用置于一个间隔中:

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

    我建议做类似的事情。

    【讨论】:

    • 我已经像你一样进入 setInterval 但仍然无法正常工作。它总是返回-1。可能是什么原因?因为它执行得很好。文件类型可能是问题?我正在使用 mp3
    【解决方案2】:

    这个解决方案对我有用。基本上,播放并立即停止。这似乎不需要任何时间,似乎是一个不错的解决方法。

    media.play();
    media.stop();
    var length = media.getDuration();
    

    【讨论】:

      【解决方案3】:

      这个问题太老了。但这仍然是相关的,因为许多人可能面临同样的问题。 每当没有任何效果时,我只做一件事,升级或降级版本。在这种情况下,我通过安装以下版本解决了我的问题。

      cordova plugin add cordova-plugin-media@1.0.1
      

      【讨论】:

        【解决方案4】:

        我在 iOS 的 cordova 中也遇到了类似的问题。我能够成功录制、播放和停止音频,但无法获取音频文件的当前位置和总持续时间。所以我在完成录制音频之后添加了my_media.release(),即在my_media.stopRecord()之后,它就像一个魅力。早些时候我得到-1getDuration()0getCurrentPosition()

        希望对某人有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-10
          • 1970-01-01
          • 2020-02-29
          • 1970-01-01
          • 2021-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多