1

Ive been beating my head up against the all trying to figure out a weird sound issue in my program. I will post the code below. As a brief description I have a function that gets passed a variable and it takes that and decides which category of sound it should play, within that function I have multiple sounds of reach category so I use an arc4random statement then run that through a switch statement. What keeps happening is that it will play two sounds from the case statement instead of just one, most times if I call it twice with my button push the second time it will play the sound from the first time and a new sound, other times it plays the same same over the top of each other but with a slight delay. I put in a breakpoint in the switch and when it double plays it only goes through the switch once which is really confusing. One thing to note is right before this I do play another sound but it uses a separate AVAudioplayer and path variable so that shouldn't be an issue as it never plays the second sound as the other sound I'm playing. I'm only calling the function once when I press the button so I'm not sure why it does this. I have tried putting the *path and *avaudioplayer variables inside the function but it won't play at all. Searching here it seems as though arc deallocs it before it gets a chance to play it. I ended up trying to put it at the top of my .m file as a global variable then just set the actual path and play the sound within the switch. The sound will play but it plays twice.. Hopefully someone can help me out. I tried putting the avaudioplayer definition as a property as well and it does the same thing. Here is my code snippet. And thanks in advance ...

in the .m file

// Just below my synthesize statements
NSString *path;
AVAudioPlayer *theSound

// My code that I call when the button is pressed
[self playVoice:@"buyVoice"];

// The playVoice function
- (void)playVoice:(NSString*)voiceNum;

    if ([voiceNum isEqualToString:@"buyVoice"])  // Bought an Item Time for a voice
    {
        // play coin sound 
        [self coinSound];

        // play random buy phrase and coin sound
        int phraseNumber = 0;
        phraseNumber = arc4random() %(3);

        switch (phraseNumber) 
       {
            case 0:
            {
                path = [[NSBundle mainBundle] pathForResource:@"sndBuy1" ofType:@"m4a"];
                    theSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
                    [theSound setNumberOfLoops:0];
                    [theSound play]; 
                    break;
            }

            case 1:
            {
                path = [[NSBundle mainBundle] pathForResource:@"sndBuy2" ofType:@"m4a"];
                    theSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
                    [theSound setNumberOfLoops:0];
                    [theSound play]; 
                    break;
            }

            case 2:
            {
                path = [[NSBundle mainBundle] pathForResource:@"sndBuy3" ofType:@"m4a"];
                    theSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
                    [theSound setNumberOfLoops:0];
                    [theSound play]; 
                    break;
            }

            case 3:
            {
                path = [[NSBundle mainBundle] pathForResource:@"sndBuy4" ofType:@"m4a"];
                    theSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
                    [theSound setNumberOfLoops:0];
                    [theSound play]; 
                    break;
            }
    }
}
2
  • Hi Tommy... I fixed your code formatting a little bit, but I hope the semicolon after your - (void)playVoice:(NSString*)voiceNum; is a mistake in your original copy & pasting. It should be - (void) playVoice: (NSString *) voiceNum { (i.e. { instead of ;). Jan 1, 2012 at 17:10
  • Thanks Michael it was not a typo but I have fixed it since then. In pouring over the code some more I found out where the issue was. within my coin sound function I was using the same path as the sound. I can't believe I didn't see that yesterday. I guess thats what you get when you stay up half the night coding.
    – TommyT39
    Jan 1, 2012 at 17:32

1 Answer 1

8

set this code before u play the Audio sound

This will reset the player to the beginning:

[theSound setCurrentTime:0.0];
[theSound play];

try this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.