l***@qq.com
l***@qq.com
  • 发布:2019-03-08 17:54
  • 更新:2019-03-08 17:54
  • 阅读:1959

一个简单5+录音和播放音频分享

分类:HTML5+

少废话,直接上代码

    <body>  
        <button id="start" type="button">录音</button>  

        <button type="button" id="stop">停止</button>  

        <div id="data-info">  
            <button type="button" id="play">开始播放</button>  

            <button type="button" id="pause-audio">暂停播放</button>  

            <button type="button" id="continue">继续播放</button>  
        </div>  
    </body>  
    <script type="text/javascript">  
        var audio_src = null;  
        var p = null;  
        mui.plusReady(function(){  
            var start = document.getElementById('start');  
            var stop = document.getElementById('stop');  
            var play = document.getElementById('play');           
            var pause = document.getElementById('pause-audio');     //暂停播放  
            var cont_inue = document.getElementById('continue');    //继续播放  
            play.addEventListener('tap',function(){  
                p = plus.audio.createPlayer(audio_src);  
                p.play( function () {  
                    alert( "Audio play success!" );  
                }, function ( e ) {  
                    alert( "Audio play failed: " + e.message );  
                } );  
            });  
            //暂停播放  
            pause.addEventListener('tap',function(){  
                p.pause();  
            });  
            //继续播放  
            cont_inue.addEventListener('tap',function(){  
                p.resume();  
            })  
            var r = plus.audio.getRecorder();   
            start.addEventListener('tap',function(){  
                // 录音操作  
                if ( r == null ) {  
                    alert( "Device not ready!" );  
                        return;   
                }    
                r.record({filename:'_doc/audio/',format:'mp3',}, function(p){    
                    console.log('录音完成:'+p);    

                    audio_src = p;  
                }, function(e){    
                    console.log('录音失败:'+e.message);    
                });    
            });  
            stop.addEventListener('tap',function(){  
                r.stop();  
            })  
        });  
    </script>

希望对新手不懂的有帮助,交流Q群:228206998

1 关注 分享
老哥教教我

要回复文章请先登录注册