开发者

How to avoid userinput while playing a soundloop complete

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 alt开发者_开发技巧ernative solutions too - maybe with the possibilities to start and hold the sound in every if-statement.

Thanxx


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
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜