使用媒体对象在设备上录制和播放音频文件

使用媒体对象在设备上录制和播放音频文件

本文介绍了使用媒体对象在设备上录制和播放音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我尝试使用phoneGap Media对象来播放音频内容,但我得到这个错误
Uncaught ReferenceError:Media未定义我从phonegap获取这个代码示例文档
>

Hi i try to use the phoneGap Media object to play audio content but i'm getting this errorUncaught ReferenceError:Media is not defined i'm getting this example of code from phonegap Documentation http://docs.phonegap.com/en/1.0.0/phonegap_media_media.md.html

我使用的代码如下:

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Media Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //
    function onDeviceReady() {
        playAudio("http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3");
    }

    // Audio player
    //
    var my_media = null;
    var mediaTimer = null;

    // Play audio
    //
    function playAudio(src) {
        // Create Media object from src
        my_media = new Media(src, onSuccess, onError);

        // Play audio
        my_media.play();

        // Update my_media position every second
        if (mediaTimer == null) {
            mediaTimer = setInterval(function() {
                // get my_media position
                my_media.getCurrentPosition(
                    // success callback
                    function(position) {
                        if (position > -1) {
                            setAudioPosition((position) + " sec");
                        }
                    },
                    // error callback
                    function(e) {
                        console.log("Error getting pos=" + e);
                        setAudioPosition("Error: " + e);
                    }
                );
            }, 1000);
        }
    }

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

    // Stop audio
    //
    function stopAudio() {
        if (my_media) {
            my_media.stop();
        }
        clearInterval(mediaTimer);
        mediaTimer = null;
    }

    // onSuccess Callback
    //
    function onSuccess() {
        console.log("playAudio():Audio Success");
    }

    // onError Callback
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

    // Set audio position
    //
    function setAudioPosition(position) {
        document.getElementById('audio_position').innerHTML = position;
    }

    </script>
  </head>
  <body>
    <a href="#" class="btn large"
    onclick="playAudio('http://audio.ibeat.org/content/p1rj1s/p1rj1s_
    _rockGuitar.mp3');">Play Audio</a>
    <a href="#" class="btn large" onclick="pauseAudio();">Pause Playing Audio</a>
    <a href="#" class="btn large" onclick="stopAudio();">Stop Playing Audio</a>
    <p id="audio_position"></p>
  </body>
</html>

任何想法请多谢。

推荐答案

当我在浏览器中打开链接时,提供播放声音的链接无法正常工作。我不能播放声音。

The link u r providing for playing sound is not working properly when I am opening the link in a browser I m not able to play sound.

因此,请将ur rockGuitar.mp3存储在assets / www文件夹中,并在播放该文件时提供文件的src为

So kindly store ur rockGuitar.mp3 inside your assets/www folder and while playing that file provide the src of the file as

src= "/android_asset/www/rockGuitar.mp3";
newMedia= new Media(src,onSuccess1,onError);

或在声音src中提供工作链接。

Or provide working link in the src of sound.

这篇关于使用媒体对象在设备上录制和播放音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:48