Greetings. I am having trouble figuring out how to turn off/on all sound for the app via a switch. Right now I have around 30 items in a tableview. The last item in the tableview is the switch for sound control. All 30 tableview items click to a new window with sounds on them. How do I make the switch in the tableview turn on/off the sounds for all those windows. Would like the user to have a silent app experience if they so choose.
Tableview code:
var soundSwitch = Titanium.UI.createSwitch({ value:true, top:8, left:130 }); var soundControl = Ti.UI.createTableViewRow({ height:44, backgroundColor:'fff', backgroundImage:'img/rowup.png' }); soundControl.add(soundSwitch); soundSwitch.addEventListener('change', function() { if (soundSwitch.value == false) { Ti.App.fireEvent('volume_down'); } else if (soundSwitch.value == true) { Titanium.App.fireEvent('volume_up'); } }); tableview.addEventListener('click', function(e) { if (e.rowData.detail) { var newwin = Titanium.UI.createWindow({url:e.rowData.detail}); } newwin.open({fullscreen:true}); });> Tableview item clicked - new window opens. Code:
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'whoosh3.caf'); var sound = Titanium.Media.createSound({ sound:file, preload:true, volume:1.0 }); // VOLUME ON Ti.App.addEventListener('volume_up', function() { if (sound.volume < 1.0) { sound.volume = 1.0; } }); // VOLUME OFF Ti.App.addEventListener('volume_down', function() { if (sound.volume > 0) { sound.volume = 0.0; } }); sound.play();
1 Answer
Accepted Answer
I would use a global variable:
soundSwitch.addEventListener('change', function() { if (soundSwitch.value == false) { Ti.App.soundOn = false; } else if (soundSwitch.value == true) { Ti.App.soundOn = true; } });
if(Ti.App.soundOn) { var sound = Titanium.Media.createSound({ sound:file, preload:true, volume:1.0 }); sound.play(); }
Your Answer
Think you can help? Login to answer this question!