我找到了一个使用Media Plugin播放音频的Cordova Android App示例,它运行正常。它随机播放不同的音频。
这是代码:
//Global media file var
var my_media;
function getSong(){
listDirectory();
}
function listDirectory() {
window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + "www/audio/", function (dirEntry) {
var directoryReader = dirEntry.createReader();
directoryReader.readEntries(dirSuccess,dirFail);
});
}
function dirSuccess(entries) {
console.log("INFO: Listing entries");
// Bellow Code lists all the files
/*
var i;
for (i=0; i<entries.length; i++) {
console.log(entries[i].name);
}
*/
// Bellow Code Gets single entry by day.
// PS you will need 365 sound files
// or you will receive an error that the file does not exist
/*
console.log(entry[dayOfYear()].name);
*/
// Bellow code gets a random file
var randomIndex = Math.floor( Math.random() * entries.length );
var theFile = entries[randomIndex];
document.getElementById( 'quote' ).innerHTML = theFile.name;
//console.log(theFile);
//console.log("tx object " + JSON.stringify(theFile));
playSong(theFile.nativeURL);
}
function dirFail(error) {
console.log("Failed to list directory contents: " + error.code);
}
function dayOfYear(){
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
return day;
}
function playSong(src){
// Audio player
if(my_media){
my_media.stop();
my_media.release();
}
my_media = new Media(src, onSuccess, onError);
// Record audio
my_media.play();
}
function onSuccess(){}
function onError(error) {
console.log("Failed to play file: " + error.code);
}
我需要的是使用不同的按钮播放不同的声音: 按钮1播放声音1 按钮2播放声音2 .....
如果有人可以帮助修改代码,我将非常感激。
由于