简体   繁体   中英

How to stop audio after finishing loop in iOS

I'm new to iPhone programming. Using below code I'm displaying image with audio. Images and audio I have stored in array.After audio completed in audioPlayerDidFinishPlaying method I'm using this code I'm displaying next image with audio.
Every thing is work fine. But problem is after audio and images are finish, again its coming from starting. May be some problem in below code can any one correct it plz..

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
  for (NSString* path in array000)
    {
        [blaukypath2 addObject:[UIImage imageWithContentsOfFile:path]];
        NSLog(@"%@",path);
    }
    currentImage++;
    NSError *error;
    if (currentImage >= blaukypath2.count) currentImage = 0;
    UIImage *blaukyyimag = [blaukypath2 objectAtIndex:currentImage];
    [img10 setImage:blaukyyimag];
    NSLog(@"%icurrentImage++ DIDF",currentImage);
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[audiopath2 objectAtIndex:currentImage] error:&error] ;
    audioPlayer.delegate = self;
    [audioPlayer prepareToPlay];
    [audioPlayer play];
}

If you dont want to repeat the image and audio than remove this line in the code..

if (currentImage >= blaukypath2.count) currentImage = 0;

Because this line will make your currentImage 0. So the loop will start again.

UPDATE
Do it like this

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
  for (NSString* path in array000)
    {
        [blaukypath2 addObject:[UIImage imageWithContentsOfFile:path]];
        NSLog(@"%@",path);
    }
    if (currentImage < blaukypath2.count)
    {
       currentImage++;
       NSError *error;
       UIImage *blaukyyimag = [blaukypath2 objectAtIndex:currentImage];
       [img10 setImage:blaukyyimag];
       NSLog(@"%icurrentImage++ DIDF",currentImage);
       audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[audiopath2 objectAtIndex:currentImage] error:&error] ;
       audioPlayer.delegate = self;
       [audioPlayer prepareToPlay];
       [audioPlayer play];
    }
}

befor plying next audio, stop previous one.

[audioPlayer stop];

Hopes it work for you.

Make a instance variable int arrayIndex; initialize with = 0

 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if([array000 count] > arrayIndex) {

      NSString* path = [arrayIndex objectAtIndex:arrayIndex];

      [blaukypath2 addObject:[UIImage imageWithContentsOfFile:path]];
      NSLog(@"%@",path);

      currentImage++;
     NSError *error;
     if (currentImage >= blaukypath2.count) currentImage = 0;
     UIImage *blaukyyimag = [blaukypath2 objectAtIndex:currentImage];
     [img10 setImage:blaukyyimag];
     NSLog(@"%icurrentImage++ DIDF",currentImage);
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[audiopath2      objectAtIndex:currentImage] error:&error] ;
     audioPlayer.delegate = self;
     [audioPlayer prepareToPlay];
     [audioPlayer play];

   }

    arrayIndex++;
} else {
 [audioPlayer stop];
}

try this approach and let me know if it work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM