Home  >  Article  >  Web Front-end  >  Introduction to how to use phonegap (7) Implementation method of playing audio

Introduction to how to use phonegap (7) Implementation method of playing audio

零下一度
零下一度Original
2017-05-03 10:28:191181browse

The following editor will bring you an implementation method of using phonegap to play audio. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

The examples are as follows:

<!DOCTYPE html> 
<html> 
  
    <head> 
        <meta charset="UTF-8"> 
        <title>Compass Example</title> 
  
        <script type="text/javascript" charset="UTF-8" src="cordova.js"></script> 
        <script type="text/javascript" charset="UTF-8"> 
            document.addEventListener("deviceready", onDeviceReady, false); 
  
            function onDeviceReady() { 
            } 
              
            var my_media=null; 
            var mediaTimer=null; 
              
            function playAudio(src){ 
                my_media=new Media(src,onSuccess,onError); 
                my_media.play(); 
                  
                if(mediaTimer==null){ 
                    mediaTimer=setInterval(function(){ 
                        my_media.getCurrentPosition( 
                            //成功回调 
                            function(position){ 
                            if(position>-1){ 
                                setAudioPosition((position/1000)+"sec"); 
                            } 
                        }, 
                        //错误回调 
                        function (e){ 
                            console.log("Error getting pos="+e); 
                            setAudioPosition("Error: "+e); 
                        } 
                        ); 
                    },1000); 
                } 
            } 
              
            function pauseAudio(){ 
                if(my_media){ 
                    my_media.pause(); 
                } 
            } 
              
            function stopAudio(){ 
                if(my_media){ 
                    my_media.stopAudio(); 
                } 
                clearInterval(mediaTimer); 
                mediaTimer=null; 
            } 
              
            function onSuccess(){ 
                console.log("playAudio():Audio Success"); 
            } 
              
            function setAudioPosition(position){ 
                document.getElementById(&#39;audio_position&#39;).innerHTML=position; 
            } 
  
            //错误的回调  
            function onError(error) { 
                alert(&#39;code:&#39;+error.code+&#39;\n&#39;+&#39;message:&#39;+error.message+&#39;\n&#39;); 
            } 
              
        </script> 
    </head> 
  
    <body> 
        <a href="#" onclick="playAudio(&#39;http://example.com/audio.mp3&#39;);">Play Audio</a> 
        <a href="#" onclick="pauseAudio();">Pause Playing Audio</a> 
        <a href="#" onclick="stopAudio();">Stop Playing Audio</a> 
        <p id="audio_position"></p> 
    </body> 
  
</html>

The above is the detailed content of Introduction to how to use phonegap (7) Implementation method of playing audio. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn