2010-08-09 38 views
0

我有一些UIImageViews(動物),並通過觸摸他們一個特定的soundloop開始播放。 但是,如果用戶在同一個視圖上觸摸兩次或更多次,則合適的聲音循環將重複地重複開始。我想讓觸摸的聲音播放一次直到它結束,並且用戶可以下次開始播放。 soundloop的持續時間約爲。 3 sec.I試圖與「睡眠(3)的東西,但它的聲音響起之前停止PROG ..如何避免userinput播放soundloop完成

但是,如果用戶觸摸另一種觀點,兩種不同的聲音可以重疊

#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 
     } 
} 

。與可能性也許開始並保持聲音在每個if語句

Thanxx

回答

1

你可以調用AudioServicesPlaySystemSound之前調用[[UIApplication sharedApplication] beginIgnoringInteractionEvents]一旦聲音播放完畢喲 - 請填寫備選解決方案太。你可以撥打[[UIApplication sharedApplication] endIgnoringInteractionEvents]再次進行互動。這種調用會阻止應用程序中所有內容的交互,這意味着只要聲音播放,用戶就無法做任何事情。考慮只能阻止你的消息-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event。如果您的聲音仍在播放,您可以通過檢查布爾值 來實現。要了解您的聲音何時完成,請使用此功能註冊適當的回叫:

OSStatus AudioServicesAddSystemSoundCompletion (
    SystemSoundID       inSystemSoundID, 
    CFRunLoopRef       inRunLoop, 
    CFStringRef        inRunLoopMode, 
    AudioServicesSystemSoundCompletionProc inCompletionRoutine, 
    void         *inClientData 
); 
+0

謝謝你,有史以來最快的答案!這就是我正在尋找的東西,希望我能夠實現它。 – FredIce 2010-08-09 14:24:33

+0

Thanou太Gouloises:我使用此解決方案編碼: ... [self performSelector:@selector(enable)withObject:yourImageView afterDelay:1.0]; ... - (void)enable { yourImageView.userInteractionEnabled = YES } – FredIce 2010-08-09 18:13:18

相關問題