PHP前端开发

使用phonegap实现播放音频的方法示例分享

百变鹏仔 2个月前 (10-18) #H5教程
文章标签 示例

下面小编就为大家带来一篇使用phonegap播放音频的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

实例如下:

<!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>