当前位置:首页 / 网页制作 / html5教程 / phonegap播放音频介绍
phonegap播放音频介绍
芯晴素材特效 分类:html5教程 发布日期:2018-05-11

这次给大家带来phonegap播放音频详解,phonegap播放音频详解的注意事项有哪些,下面就是实战案例,一起来看一下。

实例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!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>

版权信息:本站所有资源仅供学习与参考,请勿用于商业用途,如有侵犯您的版权,请及时联系821794221#qq.com(#换@),我们将尽快处理。

您可能在找这些

热门素材

html5手机端图片上传裁剪网页下载

2017-03-13   浏览:219

文字切换插件Adjector.js

2017-03-13   浏览:195

JS+CSS打造的鼠标点击触发的导航菜单

2013-01-04   浏览:9863

用CSS控制滚动条样式

2012-12-31   浏览:8114

JS实现的螺旋效果

2012-12-27   浏览:7912

推荐一款jQuery版淡出淡入的图片幻灯切

2013-01-15   浏览:8809

jQuery标签插件flyLabel

2017-03-16   浏览:368

又一款纯CSS实现的滑动门,代码非常简

2013-01-04   浏览:9039

前端模拟微信app支付页面效果

2017-03-09   浏览:265

Canvas粒子线条连接动态背景特效

2017-05-04   浏览:859

一款CSS+JS打造的个性菜单

2013-01-04   浏览:7845

全屏游动的图片,可以飘到浏览器外边

2012-12-30   浏览:7987