DasBoot
DasBoot

Reputation: 707

objective c loop blocking user input while video recording with AvFoundation?

Hello every time i try to record the date with CFAbsoluteTimeGetCurrent(); my app ignores the rest of the buttons, it's as if it takes over all memory and blocks all user input. I was wondering what i am doing wrong? I thought of making a function showtime() but i dont know how to pass values between functions from toggleRecording to showtime so that my method would work if this would even solve the problem. Below is my code:

- (IBAction)toggleRecording:(id)sender
{
    // Start recording if there isn't a recording running. Stop recording if there is.
    [[self recordButton] setEnabled:NO];
    if (![[[self captureManager] recorder] isRecording]){
        [[self captureManager] startRecording];

        /* figure out a day to record for every half a second
         while([[[self captureManager]recorder] isRecording]){
         CFTimeInterval startTime = CFAbsoluteTimeGetCurrent();
         NSLog(@" time is %i", startTime);
         }

        */
    }
    else
        [[self captureManager] stopRecording];


}

-(void)showtime:(id)sender{
    while([[[self captureManager]recorder] isRecording]){
        CFTimeInterval startTime = CFAbsoluteTimeGetCurrent();
        NSLog(@" time is %f", startTime);
    }
}

Upvotes: 0

Views: 228

Answers (1)

Julien
Julien

Reputation: 3477

An application has to run the event loop to receive the events. While your own code is doing something else (here it's doing a "while" loop), events are queued and not delivered until your code returns.

Schematically, an application is doing something like:

while(1) {
    event = _UIReceiveNextQueuedEvent();
    _UIProcessEvent(event); // <- this calls -showTime:
}

If you want to record time while not blocking the loop, you will have to schedule an NSTimer every 0.5s and invalidate it as soon as recording is turned off.

Something like:

- (void)showTime:(id)sender
{
    if ([[[self captureManager]recorder] isRecording]) {
        [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
    }
}

- (void)timerFired:(NSTimer *)timer
{
    if ([[[self captureManager]recorder] isRecording]) {
        CFTimeInterval startTime = CFAbsoluteTimeGetCurrent();
        NSLog(@" time is %f", startTime);
    } else {
        [timer invalidate];
    }
}

Upvotes: 1

Related Questions