Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tackled this problem using an NSOperation subclass so fading the volume doesn't block the main thread. It also allows fades to be queued and and forgotten about. This is especially useful for playing one shot sounds with fade-in and fade-out effects as they are dealloced after the last fade is completed.</p> <pre><code>// Example of MXAudioPlayerFadeOperation in NSOperationQueue NSOperationQueue *audioFaderQueue = [[NSOperationQueue alloc] init]; [audioFaderQueue setMaxConcurrentOperationCount:1]; // Execute fades serially. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bg" ofType:@"mp3"]; // path to bg.mp3 AVAudioPlayer *player = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:NULL] autorelease]; [player setNumberOfLoops:-1]; [player setVolume:0.0]; // Note that delay is delay after last fade due to the Operation Queue working serially. MXAudioPlayerFadeOperation *fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:player toVolume:1.0 overDuration:3.0]; [fadeIn setDelay:2.0]; MXAudioPlayerFadeOperation *fadeDown = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:player toVolume:0.1 overDuration:3.0]; [fadeDown setDelay:0.0]; MXAudioPlayerFadeOperation *fadeUp = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:player toVolume:1.0 overDuration:4.0]; [fadeUp setDelay:0.0]; MXAudioPlayerFadeOperation *fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:player toVolume:0.0 overDuration:3.0]; [fadeOut setDelay:2.0]; [audioFaderQueue addOperation:fadeIn]; // 2.0s - 5.0s [audioFaderQueue addOperation:fadeDown]; // 5.0s - 8.0s [audioFaderQueue addOperation:fadeUp]; // 8.0s - 12.0s [audioFaderQueue addOperation:fadeOut]; // 14.0s - 17.0s [fadeIn release]; [fadeDown release]; [fadeUp release]; [fadeOut release]; </code></pre> <p>For MXAudioPlayerFadeOperation class code see <a href="http://www.mackross.net/2010/12/fading-audio-with-avaudioplayer/" rel="noreferrer" title="my blog post">this post</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload