Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 1    Views: 59

iZSV
I hate code!
Profile
Posts: 2
Reg: May 09, 2013
Kemerovo
20
05/19/13 08:46 AM (12 years ago)

iPad Storybook page plugin (stop playing sound)

Please HELP me! What should I do to realize such thing as "stop playing sound fx when I left the screen (by pressing NEXT/PREVIOUS/MENU and INTERACTION button or swiping the pages)? This sound fx are playing when you press the ITERACTION button, but if the fx is not short you have to listen to it till the end, even if you are already on the next screen. In perfect: I also need to start playing this sound FXs automatically when the screen's opened. Thank you VERY MUCH for helping me with this problem!
 
iZSV
I hate code!
Profile
Posts: 2
Reg: May 09, 2013
Kemerovo
20
like
05/20/13 02:29 AM (12 years ago)
here is the code from the plugin: //Interaction Button Touched -(IBAction)pageInteraction:(id)sender{ [BT_debugger showIt:self theMessage:@"pageInteraction"]; thepatchbo_appDelegate *appDelegate = (thepatchbo_appDelegate *)[[UIApplication sharedApplication] delegate]; //Plays MP3 sound effect - The MP3 file must be added to the sound array in the appDelegate (around line 900) [appDelegate playSoundEffect:[BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"sbInteractionSound" defaultValue:@""]];; } And in the appDelegate file was this code: /////////////////////////////////////////////////////////////////////// //sound effect methods /* loadSoundEffects... the sound effects you want to use must be added to your Xcode project then added to this soundEffectNames array. See example on line 1110, 1111.. */ -(void)loadSoundEffects{ //this runs in it's own thread NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ]; [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"loadSoundEffects SEE appDelegete.m, line 924 %@", @""]]; //fill an array of sound effect file names... self.soundEffectNames = [[NSMutableArray alloc] init]; [self.soundEffectNames addObject:@"Water.mp3"]; [self.soundEffectNames addObject:@"Flower.mp3"]; [self.soundEffectNames addObject:@"pop.mp3"]; [self.soundEffectNames addObject:@"tink.mp3"]; //setup audio session for sound effects... [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; //fill an array of sound effect player objects to pre-load with each audio track... self.soundEffectPlayers = [[NSMutableArray alloc] init]; for(int x = 0; x < [self.soundEffectNames count]; x++){ NSString *theFileName = [self.soundEffectNames objectAtIndex:x]; if([BT_fileManager doesFileExistInBundle:theFileName]){ NSURL *soundFileUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], theFileName]]; NSError *error; AVAudioPlayer *tmpPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileUrl error:&error]; [tmpPlayer setNumberOfLoops:0]; [tmpPlayer prepareToPlay]; [tmpPlayer setDelegate:self]; [self.soundEffectPlayers addObject:tmpPlayer]; [tmpPlayer release]; } } //release pool [pool release]; } //playSoundEffect... -(void)playSoundEffect:(NSString *)theFileName{ [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"playSoundEffect %@", theFileName]]; if([theFileName length] > 3){ /* play sound effect logic a) Check the soundEffectNames array for the file name b) if it exists, we already instantiated an audio-player object in the soundEffectPlayers array c) Find the index of that player in the array then play it... */ if([self.soundEffectNames containsObject:theFileName]){ int playerIndex = [self.soundEffectNames indexOfObject:theFileName]; //we already initialized a player for this sound. Find it, play it. AVAudioPlayer *tmpPlayer = (AVAudioPlayer *)[self.soundEffectPlayers objectAtIndex:playerIndex]; if(tmpPlayer){ [tmpPlayer play]; } }else{ [BT_debugger showIt:self theMessage:[NSString stringWithFormat:@"playSoundInBundle:ERROR. This sound effect is not included in the list of available sounds: %@", theFileName]]; } } }
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.