使用phonegap播放音频的实现方法

建站知识 2021-07-02 23:00www.168986.cn长沙网站建设

实例如下

<!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,onSuess,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 sAudio(){ 
                if(my_media){ 
                    my_media.sAudio(); 
                } 
                clearInterval(mediaTimer); 
                mediaTimer=null; 
            } 
              
            function onSuess(){ 
                console.log("playAudio():Audio Suess"); 
            } 
              
            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./audio.mp3');">Play Audio</a> 
        <a href="#" onclick="pauseAudio();">Pause Playing Audio</a> 
        <a href="#" onclick="sAudio();">S Playing Audio</a> 
        <p id="audio_position"></p> 
    </body> 
  
</html>

以上这篇使用phonegap播放音频的实现方法就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by