2015-01-03 33 views
0

我在Android(Cordova 4.1)上播放了几个声音,但现在我想动态地改变每个视频的音量。Cordova - 在Android上控制几个声音

我用于播放声音的代码如下:

function onDeviceReady() { 
    document.querySelector("#play").addEventListener("touchend", playMP3(), false); 

    function playMP3() { 
     var mp3_1 = getMediaURL("audio/sound1.mp3"); 
     var mp3_2 = getMediaURL("audio/sound2.mp3"); 
     media1 = new Media(mp3_1, null, mediaError); 
     media2 = new Media(mp3_2, null, mediaError); 
     media1.play(); 
     media2.play(); 
    } 

    function getMediaURL(s) { 
     if(device.platform.toLowerCase() === "android") return "/android_asset/www/" + s; 
     return s; 
    } 
    function mediaError(e) { 
     alert('Media Error'); 
     alert(JSON.stringify(e)); 
    } 
} 

然后我想控制从onDeviceReady功能之外的东西像media1.volume = 0.5;卷,但我不能让它工作...

任何想法,我失踪?

回答

0

最后,用Cordova 4.1和Android 4.4为我播放和控制声音的整个包。要设置音量调用功能类似onclick=setVolume(media1, "0.5") 希望它可以帮助!

var media1 = null; 
var media2 = null; 

//set volume of one sound 
function setVolume(media, volume) { 
    if (media) { 
     media.setVolume(volume); 
    } 
} 

//play the 2 sounds at the same time 
function playMP3() { 
    var mp3_1 = getMediaURL("audio/media1.mp3"); 
    var mp3_2 = getMediaURL("audio/media2.mp3"); 
    media1 = new Media(mp3gauche, null, mediaError); 
    media2 = new Media(mp3droite, null, mediaError); 
    media1.play(); 
    media2.play(); 
} 

//for android devices (need the Device Cordova plugin) 
function getMediaURL(s) { 
    if(device.platform.toLowerCase() === "android") return "/android_asset/www/" + s; 
    return s; 
} 
//help to get log when errors occur 
function mediaError(e) { 
    alert('Media Error'); 
    alert(JSON.stringify(e)); 
} 

//launch the 2 sounds when the device is ready 
function onDeviceReady() { 
    document.querySelector("#playMp3").addEventListener("touchend", playMP3(), false); 

}