Frage

For some reason, which I'm not sure why, my app keeps crashing on the fadeTo method on a OALAudioTrack object.

I have three different OALAudioTrack objects which are within an array and I only need to change a OALAudioTrack object if a given value is different. When the value is different I need to fade a OALAudioTrack object out and then fade the new OALAudioTrack object in and replace the OALAudioTrack object in the array but I can't do this as it keeps crashing when doing the first fade out (to 0.0f). When the app crashes it always crashing on the same line, which is within OALActionManager.m line #159 ( NSUInteger index = [targets indexOfObject:action.target]; ) and the error says "Thread 1 EXE_BAD_ACCESS". Is there something I must do before doing the fades, any help would be much appreciated as I've been looking at it for over a day and I can't seem to get past this error. The code I am using is below:

if ([[self.soundScapes objectAtIndex:i] isKindOfClass:[OALAudioTrack class]])
{
    [self.tmpSoundScapes removeAllObjects];
    [self.tmpSoundScapes addObject:[fnArr objectAtIndex:i]];
    [self.tmpSoundScapes addObject:[NSNumber numberWithInt:i]];

    OALAudioTrack *currentTrack = [self.soundScapes objectAtIndex:i];

    if ([currentTrack playing])
    {
      NSLog(@"is playing");
      [currentTrack stopFade];

      //This is where the app crashes
      [currentTrack fadeTo:0.0 duration:1.5 target:self selector:@selector(onFadeComplete:)];
    }
}




-(void)onFadeComplete:(id)sender
{
    NSLog(@"fade complete");

    NSString *fn = [self.tmpSoundScapes objectAtIndex:0];
    int i = [[self.tmpSoundScapes objectAtIndex:1] intValue];

    OALAudioTrack *currentTrack = [self.soundScapes objectAtIndex:i];
    [currentTrack stop];
    currentTrack = nil;

    OALAudioTrack* track = [OALAudioTrack track];
    [track preloadFile:fn];
    track.autoPreload = YES;
    track.numberOfLoops = -1;   // Loop forever when playing.            
    track.gain = 0.0f; // volume
    [track play];

    [self.soundScapes replaceObjectAtIndex:i withObject:track];
    [self.soundScapesFiles replaceObjectAtIndex:i withObject:fn];

    int timer;
    if (i==1) 
        timer = 10.0f;
    else if (i==2)
        timer = 25.0f;
    else
        timer = 0.5f;

//    [self performSelector:@selector(onPlayScoundScape:) withObject:[NSNumber numberWithInt:i] afterDelay:timer];
    [track fadeTo:1.0f duration:timer target:self selector:@selector(onPlayScoundScape:)];
}
War es hilfreich?

Lösung

The part that's crashing is "action.target". "target" is a weak reference, so if your track deallocates before the action finishes, it will crash.

I suspect that at some point your code is either removing the track from self.soundScapes in some other method, or another call to onFadeComplete is being made, generating the same index (thus evicting the currently fading track), before the track fade completes.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top