目录
概述
JS调用方法
1、导入插件require(“cordova!cordova-plugin-media”);
2、本文对应的插件demo用了四个方法:
- play:播放音频文件
- stop:停止播放
- startRecord:开始录音
- stopRecord:结束录音
define(function(require) { var $ = require("jquery"); var justep = require("$UI/system/lib/justep"); require("$UI/system/lib/cordova/cordova"); require("css!$UI/demo/device/common/pub").load(); require("cordova!cordova-plugin-media"); var Model = function() { this.callParent(); this.mediaTimer; this.mediaRec; this.fileName; this.timeLen; this.operateType; this.STORE_ID = "com.justep.demo.advice.audiodata"; this.openByDialog = false; }; Model.prototype.modelLoad = function(event) { var me = this; document.addEventListener("deviceready", onDeviceReady, false); // 加载完成 function onDeviceReady() { me.comp("audioBtn").set({disabled: false}); me.comp("playBtn").set({disabled: false}); if(localStorage.getItem(me.STORE_ID) != "") me.comp("fileData").loadData(JSON.parse(localStorage.getItem(me.STORE_ID))); } }; // 关闭功能 Model.prototype.backBtnClick = function(event) { if(this.mediaRec) this.mediaRec.stop(); localStorage.setItem(this.STORE_ID, JSON.stringify(this.comp("fileData").toJson(false))); if(this.openByDialog) this.comp("windowReceiver").windowCancel(); else justep.Shell.closePage(); }; // 录音 Model.prototype.audioBtnClick = function(event) { this.comp("recordCover").show(); this.comp('titleOutput').set({value: "开始录音!"}); this.comp('fileOutput').set({value: "aaa"}); this.comp('recordTime').set({value: "00:00:00"}); var me = this; // 根据系统时间产生文件名 this.fileName = justep.Date.toString(new Date(), "yyyyMMddhhmmss") + ".wav"; this.mediaRec = new Media(this.fileName, function() { }, function(err) { me.comp('fileOutput').set({value: "失败:" + err}); } ); // 开始录音 this.mediaRec.startRecord(); this.operateType = "record"; var recTime = 0; this.mediaTimer = setInterval(function() { recTime = recTime + 1; me.setAudioPosition(recTime); me.comp('recordTime').set({value: me.timeLen}); }, 1000); }; //设置显示的时间 Model.prototype.setAudioPosition = function(position) { var hour = parseInt(position / 3600);// 小时数 var min = parseInt(position / 60);// 分钟数 if (min >= 60) { min = min % 60 } var lastsecs = position % 60; if(hour < 10) hour = "0" + hour; if(min < 10) min = "0" + min; if(lastsecs < 10) lastsecs = "0" + lastsecs; this.timeLen = hour + ':' + min + ':' + lastsecs; } //停止播放、停止录音 Model.prototype.pauseImageClick = function(event){ // 清空计时器 clearInterval(this.mediaTimer); if (this.operateType == "record") { // 停止录音 this.comp("recordCover").hide(); this.mediaRec.stopRecord(); var data = this.comp("fileData"); data.newData({index : 0}); data.setValue("fileName", this.fileName); data.setValue("createTime", justep.Date.toString(new Date(), justep.Date.DEFAULT_FORMAT))// 开始时间 data.setValue("timeLen", this.timeLen);// 录音时长 this.comp('fileOutput').set({value: "音频文件名"+this.fileName}); }else{ //停止播放 this.comp("playCover").hide(); this.mediaRec.stop(); } }; // 播放录音 Model.prototype.fileListClick = function(event) { this.comp("playCover").show(); this.comp('titleOutput').set({value: "播放录音!"}); this.comp('fileOutput').set({value: ""}); var me = this; var data = this.comp("fileData"); var url = data.getValue("fileName"); this.comp('playTime').set({value: "00:00:00||" + data.getValue("timeLen")}); //在播放前需要加载音频文件 this.mediaRec = new Media(url, function() { }, function(err) { me.comp('fileOutput').set({value: "失败:" + err}); } ); // 播放音频 this.mediaRec.play(); this.operateType = "play"; var recTime = 0; this.mediaTimer = setInterval(function() { recTime = recTime + 1; me.setAudioPosition(recTime); if(me.timeLen == data.getValue("timeLen")){ me.comp("playCover").hide(); clearInterval(me.mediaTimer); me.comp('fileOutput').set({value: "完毕"}); } me.comp('playTime').set({value: me.timeLen + "||" + data.getValue("timeLen")}); }, 1000); }; // 清空历史记录 Model.prototype.deleteBtnClick = function(event) { this.comp("fileData").clear(); this.comp('titleOutput').set({value: "清空历史记录"}); this.comp('fileOutput').set({value: ""}); localStorage.setItem(this.STORE_ID, ""); }; // 播放音乐 Model.prototype.playBtnClick = function(event) { this.comp('titleOutput').set({value: "播放音乐!"}); this.comp('fileOutput').set({value: ""}); var me = this; var src = window.location.origin + require.toUrl("$UI/demo/device/audio/music.mp3"); //在播放前需要加载mp3文件 this.mediaRec = new Media(src, function(optio) { me.comp('fileOutput').set({value: "完毕"}); }, function(err) { me.comp('fileOutput').set({value: "失败:" + err}); } ); this.mediaRec.play(); }; Model.prototype.windowReceiverReceive = function(event){ this.openByDialog = true; }; Model.prototype.modelUnLoad = function(event){ clearInterval(this.mediaTimer); }; return Model; });
注意事项
1、 在安卓系统如果当前app存储位置在MEDIA_MOUNTED(SD卡),音频文件会被存储到”外部存储路径”+”/”目录下,否则存在”/data/data/app包名”+”/”目录下,文件名需要自己定义。在IOS系统中,录音文件名只支持wav格式,写成其它格式会报错,文件名可以写成全路径也可以只写文件名,插件自动补全路径。全路径为该应用下的”documents/tmp”。也可用使用cordova的file插件将目录写成cordova.file.LocalFileSystem.TEMPORARY。需要注意的是指定的路径在开始录音之前必须存在,否则会引发异常报错。
2、 播放音频文件之前要先加载文件路径。
3、 本文实例所用的demo在\model\UI2\demo\native\audio目录下,可以自行打包查看效果。
评一波