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>