04模块-audio【音频的录制和播放】

Audio模块用于提供音频的录制和播放功能,可调用系统的麦克风设备进行录音操作,也可调用系统的扬声器设备播放音频文件。

该模块有两大该功能:录音和播放音频 

若发现录音设备无法工作则 需要 点击  顶部菜单栏-》运行-》真机运行-》设置自定义调试基座-》参数配置-》模块权限配置  将语音输入模块 选择起来



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>心得</title>
<script type="text/javascript">
var xhrs_r =null;
document.addEventListener("plusready",xhrs);//监听plusready事件  
function xhrs(){ 
xhrs_r = plus.audio.getRecorder(); //获取当前设备的录音对象
document.getElementById("aa").innerHTML = xhrs_r;
}
function startRecord() {
if (xhrs_r == null) {
alert( "Device not ready!" );
return; 
xhrs_r.record( {filename:"_doc/audio/"}, function () {
alert( "录音操作成功回调函数!" );
},function (xhrs_e) {
alert( "录音操作错误回调函数: " + xhrs_e.message );
});
}
function stopRecord() {
xhrs_r.stop(); 
alert('停止成功');
}
</script>
</head>
<body>
<div id="aa">123123</div>613123
<input type="button" value="Start Record" onclick="startRecord();" style="width:100px;height:50px;" /> 
<br/>
<br/>
<br/>
<br/>
<input type="button" value="Stop Record" onclick="stopRecord();" style="width:100px;height:50px;" />
</body>
</html>

 


 

 接下来是音频播放模块的代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>心得</title>
<script type="text/javascript">
//播放音频不需要  监听plusready事件  ,因为在用户点击播放按钮之前 该载入的 plus对象早已载入
var bofan = null;
function start_video() {
if (plus.audio == undefined) {
document.getElementById('aa').innerHTML = '播放器未就绪';
}
if(bofan!=null){
document.getElementById('aa').innerHTML = '已有一个音频已载入';
return '';
}
bofan = plus.audio.createPlayer("http://win.web.nf03.sycdn.kuwo.cn/9b65c1a3785440285cf35e9b1ae56885/5b4c5b92/resource/a1/72/42/1539721118.aac");//返回音频播放对象
document.getElementById('aa').innerHTML = '成功载入音频歌曲 9420';
}
function open_video(){
if(bofan==null){
document.getElementById('aa').innerHTML = '当前没有音频在播放';
return '';
}else{
document.getElementById('aa').innerHTML = "歌曲播放中……";
}
bofan.play(function(){//无返回值
document.getElementById('aa').innerHTML = '音频已播放完毕';//当音频文件播放完成时才会回调。
},function(xrhs_e){
document.getElementById('aa').innerHTML = "音频播放失败:"+xrhs_e.message;//音频播放失败时回调
});
}
function pause_video(){
if(bofan==null){
document.getElementById('aa').innerHTML = '当前没有音频在播放';
return '';
}
bofan.pause();//无返回值
document.getElementById('aa').innerHTML = "歌曲已暂停播放";
}
function goon_video(){
if(bofan==null){
document.getElementById('aa').innerHTML = '当前没有音频在播放';
return '';
}
bofan.resume();//无返回值
document.getElementById('aa').innerHTML = "歌曲播放中……";
}
function getDuration_video(){
if(bofan==null){
document.getElementById('aa').innerHTML = '当前没有音频在播放';
return '';
}
var cd =bofan.getDuration();//返回 整数类型播放音频长度
//获取音频流的总长度,单位为秒,若长度未知则返回-1。如果还未获取到音频流信息则返回NaN,此时需要延迟获取此信息。
if(isNaN(cd)){
document.getElementById('aa').innerHTML = "当前未播放任何音频";
return '';
}
document.getElementById('bb').innerHTML = "当前歌曲时长:"+parseInt(cd)+'秒';
document.getElementById('getDuration').style.display="none";
}
//
function getPosition_video(){
if(bofan==null){
document.getElementById('aa').innerHTML = '当前没有音频在播放';
return '';
}
//获取音频流当前播放的位置(已播放的长度),单位为s。
var cd = bofan.getPosition();
document.getElementById('cc').innerHTML = "当前歌曲已播放时长:"+parseInt(cd)+'秒';
document.getElementById('getPosition').style.display="none";
setTimeout(getPosition_video,1000);
}
function SPEAKER_video(){
document.getElementById('dd').innerHTML = plus.audio.ROUTE_EARPIECE+'|'+plus.audio.ROUTE_SPEAKER;
bofan.setRoute(plus.audio.ROUTE_SPEAKER);//切换到扬声器播放
}
function EARPIECE_video(){
document.getElementById('dd').innerHTML = plus.audio.ROUTE_EARPIECE+'|'+plus.audio.ROUTE_SPEAKER;
bofan.setRoute(plus.audio.ROUTE_EARPIECE);//切换到听筒器播放
}
</script>
</head>
<body>
<h1 id="aa">音频未载入</h1>
<h1 id="bb"></h1>
<h1 id="cc"></h1>
<h1 id="dd"></h1>
<div style="width:300px;height:300px;background:red;">
<div style="color: #fff;height:35px; line-height: 35px;border-bottom: #fff dashed 1px;;" onclick="start_video();">摘入音频</div>
<div style="color: #fff;height:35px; line-height: 35px;border-bottom: #fff dashed 1px;;" onclick="open_video();">播放/重载音频</div>
<div style="color: #fff;height:35px; line-height: 35px;border-bottom: #fff dashed 1px;;" onclick="pause_video();">暂停播放</div>
<div style="color: #fff;height:35px; line-height: 35px;border-bottom: #fff dashed 1px;;" onclick="goon_video();">继续播放</div>
<div id="getDuration" style="color: #fff;height:35px; line-height: 35px;border-bottom: #fff dashed 1px;" onclick="getDuration_video();">查看音频时长</div>
<div id="getPosition" style="color: #fff;height:35px; line-height: 35px;border-bottom: #fff dashed 1px;" onclick="getPosition_video();">获取当前播放位置</div>
<div style="color: #fff;height:35px; line-height: 35px;border-bottom: #fff dashed 1px;;" onclick="SPEAKER_video();">扬声器播放</div>
<div style="color: #fff;height:35px; line-height: 35px;border-bottom: #fff dashed 1px;;" onclick="EARPIECE_video();">听筒播放</div>
</div>
</body>
</html>

 

posted @ 2018-07-16 12:05  学画人生  阅读(3238)  评论(0编辑  收藏  举报