views:

20

answers:

1

Hello, I have some UIImageViews (Animals) and by touching them a specific soundloop starts to play. But if the user touches twice or more on the same View, the apropriate soundloop starts over and over again with an overlap. I wanna have that the touched sound is played once until its end and than the user can start the next time. The duration of a soundloop is approx. 3 sec.I tried something with "sleep(3); but it stops the prog before the sound is playing..

But if the user touches another View, two different sounds can overlap.

#pragma mark -

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
//Soundfile names in array arrAnimalde
arrAnimalde = [NSArray arrayWithObjects:@"Hund" ,@"Schwein" ,@"Ferkel", nil];

// here are some Views and their triggers
if([touch view] == img_hund){
    trigAnimal = 0;
    }

if([touch view] == img_schwein){
    trigAnimal = 1;
    }

if([touch view] == img_ferkel){
    trigAnimal = 2;
      }

    AudioServicesCreateSystemSoundID ((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: [arrAnimalde objectAtIndex: trigAnimal]
 ofType: @"wav"]], &soundFileObject);
    AudioServicesPlaySystemSound (self.soundFileObject);

//if the trigger is the same as before play the sound only once and completely
      }
}

Please write alternative solutions too - maybe with the possibilities to start and hold the sound in every if-statement.

Thanxx

+1  A: 

You could call [[UIApplication sharedApplication] beginIgnoringInteractionEvents] before calling AudioServicesPlaySystemSound. Once the sound has finished playing you may call [[UIApplication sharedApplication] endIgnoringInteractionEvents] to allow interaction again. This calls will block the interaction for everything in your application, what means that your user cant do anything as long as your sound plays. Consider only blocking your -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event message instead. You could achieve that by just checking a boolean value if your sound still plays. To find out when your sound has finished, register a appropriate callback with this function:

OSStatus AudioServicesAddSystemSoundCompletion (
   SystemSoundID                           inSystemSoundID,
   CFRunLoopRef                            inRunLoop,
   CFStringRef                             inRunLoopMode,
   AudioServicesSystemSoundCompletionProc  inCompletionRoutine,
   void                                    *inClientData
);
thatsdisgusting
Thank You, the fastest answer ever! That's what i'm looking for, hope I can implement it.
FredIce
Thanx too to Gouloises: I coded it with this solution:...[self performSelector:@selector(enable) withObject:yourImageView afterDelay:1.0];...-(void)enable{yourImageView.userInteractionEnabled = YES}
FredIce