2011-06-16 3 views
0

Est-il possible de forcer la lecture d'un fichier audio avant de passer au code? Dans l'exemple ci-dessous, j'aimerais qu'un fichier audio soit lu autant de fois que j'ai d'éléments dans le tableau. courant il finit la boucle en jouant seulement une fois. J'utilise AVAudioPlayer dans le framework AVFoundation.iphone avaudiofoundation Achèvement du son en boucle

for (int i = 0; i<[Array count]; i++) { 
    if ([Array objectAtIndex:i] == @"Red") { 
     NSLog(@"red"); 
     [self.player play]; 
    } 
    if ([Array objectAtIndex:i] == @"Blue") { 
     NSLog(@"blue"); 
     [self.player play]; 
    } 
    if ([Array objectAtIndex:i] == @"Green") { 
     NSLog(@"green"); 
     [self.player play]; 
    } 
    if ([Array objectAtIndex:i] == @"Yellow") { 
     NSLog(@"yellow"); 
     [self.player play]; 
    } 
} 

J'utilise aussi la méthode audioPlayerDidFinishPlaying pour tester si elle est terminé cinq fois, mais il atteint seulement cette méthode une fois.

-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL) completed{ 
    if ((completed) == YES){ 
     NSLog(@"audio finish"); 
     return; 
    } 
} 

Est-il possible de maintenir la boucle for en place pendant la lecture de l'audio?

en théorie la console doit lire: couleur-> audio Terminer-> couleur-> audio Terminer-> REPE

Répondre

1

Peut-être que vous pouvez dire AudioPlayer à jouer lorsque vous recevez la notification finishedPlaying comme:

int arrayIndex = 0 
NSArray* array; //initialized somewhere.. 


-(void)myFunction 
{ 
    if(arrayIndex < [array length]) 
    { 
    NSArray* colors = [NSArray arrayWithObjects:@"Red",@"Blue",@"Green",@"Yellow",nil]; 
    if([colors indexOf:[array objectAtIndex:arrayIndex]] != NSNotFound) 
    { 
     NSLog([array objectAtIndex:arrayIndex]); 
     [self.player play]; 
    } 
    arrayIndex++; 
    }else{ 
    NSLog(@"audio finish"); 
    } 
} 

-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)  completed{ 
    if ((completed) == YES){ 
     [self myFunction]; 
    } 
} 

-(void)start_it_up 
{ 
    arrayIndex = 0; 
    [self myFunction]; 
} 
+0

J'aime cette idée. Je vais essayer. –

+0

cela m'a mis sur la bonne voie! Merci de votre aide. –