Fading in and out Volume

Discussion in 'iOS Programming' started by MobileAppCenter, Oct 23, 2008.

  1. MobileAppCenter macrumors newbie

    Joined:
    Oct 23, 2008
    #1
    I could use some direction regarding methods to use volume control to fade in sounds internal to an app on start, and fade out on stop.

    Thanks.
     
  2. SqueegyX macrumors regular

    Joined:
    Mar 24, 2008
    #2
    There's like 3 different ways audio is handled on the iPhone that greatly affect how this question would be answered. How are you play the audio files you want to fade?
     
  3. MobileAppCenter thread starter macrumors newbie

    Joined:
    Oct 23, 2008
    #3
    AudioPlayer

    We are using the AudioPlayer class. Of course, we are adding sounds to the queue. We set the gain to 0.0 and after initiating the Play method we want to gradually have the gain increased to some predefined gain (maybe 1.0, or maybe less depending on user input).

    I hope this is what you are looking for. Thanks for your help.
     
  4. SqueegyX macrumors regular

    Joined:
    Mar 24, 2008
    #4
    I only have experience with Apple's SoundEngine form the Crash Landing example. But the same idea should apply.

    Setup a timer to call a method every few miliseconds to adjust the volume or gain. I don't think there is any sort of core animation like API for doing this stuff.
     
  5. Niiro13 macrumors 68000

    Joined:
    Feb 12, 2008
    Location:
    Illinois
    #5
    I'm also trying to fade in and out from the SoundEngine but want to keep performance. Is there a difference in using a timer versus a for loop? Timers are new to me and for loops aren't, so I'd be nice to know if Timers have a significant performance boost from for loops.
     
  6. SqueegyX macrumors regular

    Joined:
    Mar 24, 2008
    #6
    A loop would not work like you expect. Take this psuedo code.

    Code:
    for (int i =0; i < 100; i++) {
      [mySound setVolume:i];
    }
    
    This will execute in an almost imperceptible amount of time, making the fade barely noticeable. You could insert some sort of sleep in there, but then you are just tieing up your main application thread doing nothing.

    You can tell the timer to fire 20 times per second and step the volume down, or up, a little bit each time. A loop doesn't give you that same level of control over time.
     
  7. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #7
    Cocoa will not function well if loops are used with delays, even if that were not a bad programming practice. Use Timers instead, and let Cocoa call your code at the time desired.

    If ramping the volume down a few percent every few milliseconds (until it gets to zero) doesn't work, you could try using uncompressed raw audio files and ramping down the amplitude of the samples in software at run time before putting them into the audio queue buffers. The latter is what I tried, and it works great. Allows me to fade in/out or cross fade without a single audible glitch, and with sample accurate looping.


    .
     

Share This Page