0

大家好,我的phonegap应用程序有一个小问题,一切正常,但除非我点击播放,否则我无法让我的应用程序自动播放或流式传输音频文件!如何使以下代码自动播放音频文件?

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

<script type="text/javascript" charset="utf-8">

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

        // PhoneGap is ready
        //
        function onDeviceReady() {
            playAudio("http://myradiostation.com:8000/live.mp3");
        }

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

        // Play audio
        //
        function playAudio(src) {
            if (my_media == null) {
                // Create Media object from src
                my_media = new Media(src, onSuccess, onError);
            } // else play current audio
            // 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>

<style type="text/css">
#player {
    position: absolute;
    width: 200px;
    height: 33px;
    z-index: 1;
    left: 7px;
    top: 123px;
}
</style>
</head>
      <body>
      <div id="player"> <a href="#" class="btn large" onclick="playAudio('http://myradiostation.com:8000/live.mp3');"><img src="images/play.png"></a>
          <a href="#" class="btn large" onclick="pauseAudio();"><img src="images/pause.png"></a>
          <a href="#" class="btn large" onclick="stopAudio();"><img src="images/stop.png"></a></div>
</body>
4

0 回答 0