在phonegap中播放捕获的音频

时间:2013-03-05 11:30:08

标签: ios cordova jquery-mobile

我能够捕获音频并且我拥有捕获文件的完整路径,但是我无法使用它来播放最新捕获的文件。

我在iOS 6.1上使用Phongap 2.5和Jquery Mobile。

代码:

        <script type="text/javascript" charset="utf-8">
            function captureSuccess(mediaFiles) {
                var i, len;
                var formatSuccess = function (mediaFile) {
                    document.getElementById('capture-data').innerHTML =
                    "Duration: <strong>" + mediaFile.duration + "</strong><br/>";
                };

                for (i = 0, len = mediaFiles.length; i < len; i += 1) {
                    // uploadFile(mediaFiles[i]);
                    document.getElementById('capture-result').innerHTML = "<strong>" + (i+1) + " file(s), Path: " + mediaFiles[i].fullPath + "</strong><br/>";
                    mediaFiles[i].getFormatData(formatSuccess, formatError);
                    readDataUrl = mediaFiles[i].fullPath;
                };

                console.log("captureImageSuccess");
            }


            function captureError(error) {
                var msg = 'An error occurred during capture: ' + error.code;
                navigator.notification.alert(msg, null, 'Uh oh!');
            }

            function captureAudio() {
                // Launch device audio recording application,
                // allowing user to capture up to 2 audio clips
                navigator.device.capture.captureAudio(captureSuccess, captureError, {limit: 2});
            }

            $("#audio-result").click(function() {
                function playAudio(mediaFiles) {
                var play = document.getElementById('audio-result').src =
                mediaFiles[i].fullPath;
            }
                                     });

            </script>
    </head>
    <body>
        <button onclick="captureAudio();">Capture Audio</button> <br>

        <div class="result-block">
            Capture Result: <span id="capture-result"></span><br/>
            <span id="capture-data"></span><br/>
            <button id="audio-result">Play audio</button>
            </div>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以按以下方式播放媒体

var my_media = null;

// here src is the uri of your media
// Create Media object from src
my_media = new Media(src, onSuccess, onError);

// Play audio
my_media.play()

// Pause audio
function pauseAudio() {
    if (my_media) {
        my_media.pause();
    }
}

// Stop audio
function stopAudio() {
    if (my_media) {
        my_media.stop();
    }
}