<! doctype html> <html> <head> <meta charset = utf-8> <title> ตัวอย่างเข็มทิศ </title> <script type = text/javascript charset = utf-8 src = cordova.js> </script> <script type = text/javascript charset = utf-8> document.addeventListener (Deviceready, ondeviceready, false); ฟังก์ชั่น ondeviceready () {} var my_media = null; var mediatimer = null; ฟังก์ชัน playaudio (src) {my_media = สื่อใหม่ (src, onsuccess, onerror); my_media.play (); if (mediatimer == null) {mediatimer = setInterval (function () {my_media.getCurrentPosition (// 成功回调ฟังก์ชัน (ตำแหน่ง) {ถ้า (ตำแหน่ง> -1) {setaudioposition ((ตำแหน่ง/1000)+วินาที);}}} , // 错误回调 function (e) {console.log (ข้อผิดพลาดได้รับ pos = +e); }} ฟังก์ชั่น pauseaudio () {ถ้า (my_media) {my_media.pause (); }} ฟังก์ชั่น stopaudio () {ถ้า (my_media) {my_media.stopaudio (); } ClearInterval (Mediatimer); mediatimer = null; } function onsuccess () {console.log (playaudio (): ความสำเร็จด้านเสียง); } ฟังก์ชั่น setaudioposition (ตำแหน่ง) {document.getElementById ('audio_position'). innerhtml = ตำแหน่ง; } // 错误的回调ฟังก์ชั่น onerror (ข้อผิดพลาด) {แจ้งเตือน ('รหัส:'+error.code+'/n'+'ข้อความ:'+Error.message+'/n'); } </script> </head> <body> <a href =# onclick = playaudio ('http://example.com/audio.mp3');> เล่นเสียง </a> <a href =# onclick = pauseaudio ();> หยุดเล่นเสียง </a> <a href =# onclick = stopaudio ();> หยุดเล่นเสียง </a> <p id = audio_position> </p> </body> </html>
以上这篇使用 phonegap 播放音频的实现方法就是小编分享给大家的全部内容了, 希望能给大家一个参考, 也希望大家多多支持 vevb 武林网。