Problem with multiple sound buttons.

Discussion in 'iOS Programming' started by timbojill, May 21, 2011.

  1. macrumors member

    Joined:
    May 21, 2011
    #1
    The problem with how the app is now is the first time you press any of the buttons there is a 5 second delay. After that no problem
    Code:
    .h File for View
    
    #import <UIKit/UIKit.h>
    #import<AVFoundation/AVAudioPlayer.h>
    
    @interface buttonSoundViewController : UIViewController <AVAudioPlayerDelegate> {
        }
    -(IBAction) button1:(id)sender;
    -(IBAction) button2:(id)sender;
    -(IBAction) button3:(id)sender;
    -(IBAction) button4:(id)sender;
    -(IBAction) button5:(id)sender;
    -(IBAction) button6:(id)sender;
    -(IBAction) button7:(id)sender;
    
    @end
    
    Code:
    .M File
    
    -(IBAction) button1:(id)sender{
       
        NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"];
        AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
       
        theAudio.delegate=self;
        [theAudio play];
           
        }
    
    -(IBAction) button2:(id)sender{
       
        NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"wav"];
        AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
       
        theAudio.delegate=self;
        [theAudio play];
       
    }
    
    -(IBAction) button3:(id)sender{
       
        NSString *path = [[NSBundle mainBundle] pathForResource:@"sound3" ofType:@"wav"];
        AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
       
        theAudio.delegate=self;
        [theAudio play];
       
    }
    
    -(IBAction) button4:(id)sender{
       
        NSString *path = [[NSBundle mainBundle] pathForResource:@"sound4" ofType:@"wav"];
        AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
       
        theAudio.delegate=self;
        [theAudio play];
       
    }
    
    -(IBAction) button5:(id)sender{
       
        NSString *path = [[NSBundle mainBundle] pathForResource:@"sound5" ofType:@"wav"];
        AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
       
        theAudio.delegate=self;
        [theAudio play];
       
    }
    
    -(IBAction) button6:(id)sender{
       
        NSString *path = [[NSBundle mainBundle] pathForResource:@"sound6" ofType:@"wav"];
        AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
       
        theAudio.delegate=self;
        [theAudio play];
       
    }
    
    -(IBAction) button7:(id)sender{
       
        NSString *path = [[NSBundle mainBundle] pathForResource:@"sound7" ofType:@"wav"];
        AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
       
        theAudio.delegate=self;
        [theAudio play];
       
    }
    
    
     
  2. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #2
    I remember reading somewhere that when you play an audio sound for the first time, there is a delay. After that it all runs fine. I think the author loaded one of his sounds in the viewDidLoad method with the volume at 0 and then when he played another sound there was no delay. I'll try and see if I can find exactly where I read it and post it for you.
     

Share This Page