How to suspend dispatch_queue_t and queues created by it

I am creating a program that, among other things, can disappear and exit music. The problem is that other streams / queues can pause music, which means that attenuation and output must also not only pause, but also be held on. I need to be able to pause the "timer" on dispatch_after (because it is called when the music starts playing, to tell her when to start fading, what would have to be postponed if it was paused) and pause the queue (to pause the fade in or out, when they disappear or disappear)

Here's the code (fadeIn and delayFadeOut called at the beginning of the program):

- (void) doFadeIn: (float) incriment to: (int) volume with: (AVAudioPlayer*) thisplayer on: (dispatch_queue_t) queue{
    dispatch_async(queue, ^{
        double delayInSeconds = .1;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, queue, ^(void){
            thisplayer.volume = (thisplayer.volume + incriment) < volume ? thisplayer.volume + incriment : volume;
            NSLog([[[NSNumber alloc] initWithFloat:thisplayer.volume] stringValue]);
            if (thisplayer.volume < volume) {
                [self doFadeIn:incriment to:volume with:thisplayer on:queue];
            }
        });
    });    
}

-(void) doDelayFadeOut: (float) incriment with: (AVAudioPlayer*) thisplayer on: (dispatch_queue_t) queue
{
    dispatch_async(queue, ^{
        double delayInSeconds = .1;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, queue, ^(void){
            thisplayer.volume = (thisplayer.volume - incriment) > 0 ? thisplayer.volume - incriment : 0;
            NSLog([[[NSNumber alloc] initWithFloat:thisplayer.volume] stringValue]);
            if (thisplayer.volume > 0.0) {
                [self doDelayFadeOut:incriment with:thisplayer on:queue];
            }
        });
    });
}

-(void) fadeIn: (AVAudioPlayer*) dFade {
    if (dFade == nil) {
        return;
    }
    dispatch_queue_t queue = dispatch_queue_create("com.cue.MainFade", NULL);
    dispatch_async( queue, ^(void){
        if(dFade !=nil){
            double incriment = ([self relativeVolume] / [self fadeIn]) / 10; //incriment per .1 seconds.
            [self doFadeIn: incriment to: [self relativeVolume] with:dFade on:dispatch_queue_create("com.cue.MainFade", 0)];
        }

    });
}

- (void) delayFadeOut: (AVAudioPlayer*) dFade { //d-fade should be independent of other threads
    if (dFade == nil) {
        return;
    }
    int timeRun = self.duration - self.fadeOut;
    dispatch_queue_t queue = dispatch_queue_create("com.cue.MainFade", NULL);
    dispatch_time_t mainPopTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeRun * NSEC_PER_SEC));
    printf("test");
    dispatch_after(mainPopTime, queue, ^(void){
        if(dFade !=nil){
            double incriment = ([dFade volume] / [self fadeOut])/10; //incriment per .1 seconds.
            [self doDelayFadeOut:incriment with:dFade on:dispatch_queue_create("com.cue.MainFade", 0)];
        }

    });
    if (self.cueType == 2) {
        [self callNext];
    }
}
+5
2

, dispatch_suspend() ( dispatch_resume()). . . , , .

, , , , "" " dispatch_after". , , - 1 , 1 . "" "". " ". , , . , , , , . , , " ". , , , , . , " ". , .

+10

/ NSOperation NSOperationQueue GCD. . .

+4

All Articles