Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No, you can't assume that. One of my methods looks at two keys, playable and duration, and I have found that playable is often available while duration isn't yet. I therefor have moved the loadValuesAsynchronouslyForKeys: code into a separate method shouldSave:. The shouldSave: method I call from a timer in a method called saveWithDuration:. Once saveWithDuration: receives a non-zero duration, it goes ahead and saves stuff. To avoid waiting too long, I use an attempt counter for now -- in the future, I'll refine this (you'll notice that the error instance isn't really used at the moment) </p> <pre><code>- (void)shouldSave:(NSTimer*)theTimer { NSString * const TBDuration = @"duration"; NSString * const TBPlayable = @"playable"; __block float theDuration = TBZeroDuration; __block NSError *error = nil; NSArray *assetKeys = [NSArray arrayWithObjects:TBDuration, TBPlayable, nil]; [_audioAsset loadValuesAsynchronouslyForKeys:assetKeys completionHandler:^() { AVKeyValueStatus playableStatus = [_audioAsset statusOfValueForKey:TBPlayable error:&amp;error]; switch (playableStatus) { case AVKeyValueStatusLoaded: //go on break; default: return; } AVKeyValueStatus durationStatus = [_audioAsset statusOfValueForKey:TBDuration error:&amp;error]; switch (durationStatus) { case AVKeyValueStatusLoaded: theDuration = CMTimeGetSeconds(_audioAsset.duration); break; default: return; } }]; NSUInteger attempt = [[[theTimer userInfo] objectForKey:TBAttemptKey] integerValue]; attempt++; [self saveWithDuration:theDuration attempt:attempt error:&amp;error]; </code></pre> <p>}</p>
 

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