Quantcast
Channel: Appcelerator Developer Center Q&A Tag Feed (complete)
Viewing all articles
Browse latest Browse all 20

sound.release() problem with sound complete event listener for sound in an array

$
0
0

In trying to solve the issue http://developer.appcelerator.com/question/140242/audioflinger-34-no-more-track-names-available

I tried to add free the sound resource each and every time it's used and complete. There are so many sounds that if you try to hit every sound in the array, android runs out of memory, so freeing the sound should do the trick, no?

The problem arises in that there are many sounds, so I need to effectively take mysound[i], attache a 'complete' listener, then, when the sound is played, release the resource, then assign the same sound to it.

The code below shows the fundamental issue where I have tried to pass a variable into my anonymous function, and I suspect some expert here can resolve this in a few seconds.

I leveraged the code from another question as it represented half of my problem and doesn't include all my debug codes (I can't get the markdown to work for my links) http://developer.appcelerator.com/question/84241/dynamically-assign-listener-to-button

var buttons = new Array();
var sounds = new Array();
var snd = new Array();
 
sounds[0] = '../sounds/first.mp3';
sounds[1] = '../sounds/second.mp3';
sounds[2] = '../sounds/third.mp3';
sounds[3] = '../sounds/fourth.mp3';
sounds[4] = '../sounds/fifth.mp3';
sounds[5] = '../sounds/sixth.mp3';
sounds[6] = '../sounds/seventh.mp3';
 
var itemCount = sounds.length;
var i = 0;
var pos = 1;
while(i<(sounds.length)) {
    var position = pos * 50;
    var btn_title = 'Button ' + i;
    buttons[i] = Ti.UI.createButton({ title: btn_title, my_id:i, top: position, left:25 }); // used my_id for tracking sound in array
    win1.add(buttons[i]);
    buttons[i].addEventListener('click',function(e){ var i = e.source.my_id;playSound(i); // this function just plays the sound
    });
    snd[i] = Ti.Media.createSound({ url: sounds[i]}); // tried to also add an 'id' type property here, but not accepted
    snd[i].addEventListener('complete',function(){ 
        Titanium.API.info('sound complete for URL: '+snd[i].url); // variable 'i' in the anonymous function is not pass from loop to the fnction & throws an arror
        sndcomplete2(i)} // 'i' is still undefined
    );
    i++;
    pos++;
};

Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>