Cordova Media Capture - 查找音频文件的持续时间

时间:2016-07-08 12:28:53

标签: cordova audio telerik

我正在编写一个应用程序,它将接收一个音频文件,然后将其上传到Teleriks everlive服务。我想阻止用户上传任何超过20秒的音频文件。

但经过测试后似乎无论音频文件的持续时间如何,“data.duration”似乎总是返回0,所以无论文件上传的是什么。

这是我正在使用的插件:https://github.com/apache/cordova-plugin-media-capture

这是我的代码:

    var el = new Everlive('app-id-here'); //Taken away ID for stackoverflow

    var options = {
        fileName: 'testaudio.mp3',
        mimeType: 'audio/mpeg3'
    };

    function formatSuccess(data) {
        if (data.duration > 20) {
            alert("The audio file you have used is longer than 20 seconds.");
        } else {
            alert(data.duration);
            el.files.upload(capturedFiles[0].fullPath, options)
                .then(function () {
                    alert('success');
                }, function (err) {
                    alert(JSON.stringify(err));
                });
        }

    }

    function formatFail() {
        alert("Error accessing file data");

    }

    capturedFiles[0].getFormatData(formatSuccess, formatFail);

1 个答案:

答案 0 :(得分:0)

上次我玩这个时,getDuration仅在播放媒体文件时有效。来自我的Apache Cordova API Cookbook(www.cordovacookbook.com):

应用程序可以使用getDuration确定媒体文件的长度,如以下示例所示:

console.log('Duration: ' + theMedia.getDuration() + ' seconds');
如果音频片段当前没有播放,

getDuration将报告-1。与getCurrentPosition不同,即使暂停播放,getDuration方法也会返回剪辑长度。

相关问题