UI Locking & Delay While Sound Object Loads
Does anything know a better way to play a sound file without locking the UI while playing?
For an app I've got a view which a user touches and I want to play a sound. Like of it as a door that you need to knock. On the click event I call mySound.play(), this locks the UI for a second before playing, then for the duration of the 1 second clip.
This wouldn't be bad if the user only did it once, but the user will do this frequently.
I've tried a few things, but the below seems to work the best, even though it still locks the UI thread for 1-2 seconds.
Even using the Simulator you can see some delay while the 1 second MP3 plays. On an iPhone 3G the delay can be 1-4 seconds.
Any ideas on how I can fix this?
var myFile = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'my.mp3');
// load from file object
var mySound = Titanium.Media.createSound({sound:myFile});
10 Answers
-
Had the same problem. Bought the Zero Latency Module from the Marketplace to play simple button sounds. Solved the problem.
-
You can use
preload
to load the sound immediately. Does that help? -
Thanks Jeff that helped alittle on the initial load but if I call mySound.play() each time I tap a view or button it seems to delay in loading each time. Is there anyway to avoid loading the sound each time? It feels like each time I call mySound.play() it is loading the sound file into memory. I would have though re-playing would be less memory intensive.
-
like this ?…
var mySound = Titanium.Media.createSound({sound:myFile, preload:true});
that's right ? :)
-
I have exactly the same problem.
Even using:
var sndTickTock = Ti.Media.createSound({sound:Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'sounds/ticktock.wav'), preload:true});
It locks up for a second each time I call sndTickTock.play()…
-
Can anyone else confirm this? Is it a bug?
Thanks.
-
I have that problem too. If I'm loading the soundfile from the internet and use the preload property, the rest of the UI is locked. The only thing I can use is the back button in the navBar. But even if release the resources on window close and select another sound file, I have to wait until the sound file is loaded.
Maybe it's possible to send an interrupt when the window is closed?
-
You could try the "fool-preload"-technique:
var mySound = Titanium.Media.createSound({sound:myFile}); mySound.play(); mySound.pause();
This should preload the audio file, but will probably increase initial load time for the entire application.
(Only tested on iPhone emulator) -
Very curious behaviour: I have a similar problem as described above, but I didn't noticed it for a long time, because I have a short intro movie. So if the movie completes, the menu sounds later will play directly. If the movie is interrupted or doesn't start at all, the sounds will play with the mentioned delay. I tried to set the useApplicationAudioSession parameter of the video player from true (the default) to false as a counter check: This results in menu sounds that are played delayed in any case! So it looks like a problem, that could be solved, if the sounds can be played in a certain audio session.
-
Hello, one particular reason for the "Delay" in audio is the audio files compression. Try using uncompressed audio (Linear PCM) and it might speed things up a bit.