实例如下:
<!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('audio_position').innerhtml=position; } //错误的回调 function onerror(error) { alert('code:'+error.code+'\n'+'message:'+error.message+'\n'); } </script> </head> <body> <a href="#" onclick="playaudio('http://example.com/audio.mp3');">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>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
pushstate、popstate操作url的方法
不刷新的前提下怎样改变当前url的代码
以上就是phonegap播放音频详解的详细内容。
