我使用ionic / Cordova框架从html5和JavaScript创建了一个Android应用程序。在那里,我创建并播放音频对象:
var myAudio = new Audio("myfile.mp3") ;
myAudio.play();
如果我在Android API 21上安装生成的apk,则音频播放正常。如果我在API 19上安装它就不会播放。
对此的任何帮助都非常感谢。
答案 0 :(得分:2)
您是否安装了cordova媒体插件?
我过去曾用cordova 4.0.0做过类似的事情(肯定是在android 19(4.4.2准确)上工作):
AckSound:function(){
try{
var pathMedia;
if(device.platform ==='iOS'){
pathMedia ='';
} else {
pathMedia = app.getCordovaPath();
}
var snd =new Media(pathMedia+'mySound.mp3');
// Update media position every second
var mediaTimer = null;
mediaDuration = snd.getDuration();
console.log("mediaDuration is " + mediaDuration);
snd.play();
if(mediaTimer==null){
mediaTimer = setInterval(function () {
// get media position
snd.getCurrentPosition(
// success callback
function (position) {
if (position > 0) {
console.log((position) + " sec");
} else {
//if position isn t > 0, the file is over
snd.stop();
clearInterval(mediaTimer);
mediaTimer = null;
snd.release();
delete snd;
}
},
// error callback
function (e) {
console.log("Error getting pos=" + e);
}
);
}, 100);
};
delete snd;
} catch(err){
console.log("Error: in AudioManagement.AckSound(), Error is " + err);
}
console.log("AudioManagement.AckSound() stops");
}
},