AVAudioPlayer Help

Discussion in 'iPhone/iPad Programming' started by PanzyCat, Jul 16, 2010.

  1. macrumors newbie

    Joined:
    Jul 7, 2010
    #1
    Hey guys

    I was wondering if you could help me with my issue below.

    Code:
    
    - (IBAction)playsound1 {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
    [theAudio play];
    
    }
    
    When I press the buttons often, after 200-250 times pressing the buttons, the buttons don't sound anymore. I can continue pressing the buttons, but no sound appears. But the application doesn't break down, i can continue using my application fine, but only the sound doesn't sound anymore, so I have to restart the application and after the restart everything works fine again, till I pressed the sound-buttons 200-250 times and the game begins again. I can't figure out the problem.

    If you could help, that would be great.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Do you ever release theAudio? Because you should as as per the Cocoa Memory Management Rules you "own" that object. I assume you are familiar with the entirety of the document I linked to?
     
  3. thread starter macrumors newbie

    Joined:
    Jul 7, 2010
    #3
    No, sorry. I actually downloaded xcode yesterday, and watched a tutorial on how to make sounds. I wanted to make a little soundboard.

    I know that I would release it in the .h file, but I don't know how I would do that?
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    No, you absolutely wouldn't do it in the .h file. I suggest you read the document I linked to.
     
  5. thread starter macrumors newbie

    Joined:
    Jul 7, 2010
    #5
    From what I read in the link you just sent me, this is what I believe it should look like.

    Code:
    - (IBAction)playsound1 {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
    [theAudio play];
    [theAudio release];
    
    }
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    I suggest that that will probably not do what you want. You want to release the theAudio when the sound finishes playing. Look at the delegate methods.
     
  7. thread starter macrumors newbie

    Joined:
    Jul 7, 2010
    #7

    Is this in the link you sent me?

    I get that I have to release theAudio after 1 count. I don't know how to tell it to do that.
     
  8. thread starter macrumors newbie

    Joined:
    Jul 7, 2010
    #8
    Code:
    	NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
    	AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    	theAudio.delegate = self;
    	[theAudio play];
    	
    	if([theAudio initWithData:audioData error:NULL]) {
            [theAudio autorelease];
        } else {
            [theAudio release];
            theAudio = nil;
        }
        return theAudio;
    }

    is that right?
     

Share This Page