Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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});
— asked March 10th 2010 by Ben Bahrenburg
  • iPhone
  • sound
0 Comments

10 Answers

  • Had the same problem. Bought the Zero Latency Module from the Marketplace to play simple button sounds. Solved the problem.

    — answered February 15th 2012 by Jonas Funk Johannessen
    permalink
    0 Comments
  • You can use preload to load the sound immediately. Does that help?

    — answered March 15th 2010 by Jeff Haynie
    permalink
    0 Comments
  • 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.

    — answered March 16th 2010 by Ben Bahrenburg
    permalink
    0 Comments
  • like this ?…

    var mySound = Titanium.Media.createSound({sound:myFile, preload:true});
    

    that's right ? :)

    — answered March 25th 2010 by David Dupuis
    permalink
    0 Comments
  • 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()…

    — answered April 6th 2010 by salo corgan
    permalink
    0 Comments
  • Can anyone else confirm this? Is it a bug?

    Thanks.

    — answered April 9th 2010 by salo corgan
    permalink
    0 Comments
  • 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?

    — answered May 17th 2010 by Robert Ölei
    permalink
    0 Comments
  • 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)

    — answered February 8th 2011 by Oscar Karlsson
    permalink
    0 Comments
  • 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.

    — answered November 20th 2011 by Klaus Heyne
    permalink
    0 Comments
  • 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.

    — answered January 27th 2012 by Zack Carlson
    permalink
    1 Comment
    • Zack Carlson @ can you please provide some code to perform Linear PCM on wav files ?

      — commented September 7th 2013 by Suresh Sharma
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.