1

here is my code

 NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"95" ofType:@"wav"];

NSError *activationError = nil;
NSError *audioPlayerInitError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];

NSURL *newURL = [NSURL fileURLWithPath:soundFilePath];
musicPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error:&audioPlayerInitError];

if (musicPlayer1) {
    [musicPlayer1 stop];
    [musicPlayer1 release];
    musicPlayer1 = nil;
}
else {

    [musicPlayer1 prepareToPlay];
    [musicPlayer1 setVolume:.8];
    [musicPlayer1 setNumberOfLoops:-1]; // -1 means play indefintely
    [musicPlayer1 setDelegate: self];
    [musicPlayer1 play];
}

}

I am rying to start and stop the AVAudioPlay with the same button (musicPlayer1 is in the header). Now when i touch the button it does not play anything. Help please?

luca590
  • 460
  • 2
  • 5
  • 25

1 Answers1

3

You're creating a new instance of AVAudioPlayer each time. You need to hold onto the reference to the object somewhere, and refer back to that. Add a field to your view controller class:

@private AVAudioPlayer *currentAudio;

And then in this method, make the following changes:

if (currentAudio) {
    [currentAudio stop];
    [currentAudio release];
    currentAudio = nil;
} else {
    // what you already have goes here
}
Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • im assuming u r declaring @private in the header – luca590 Apr 01 '11 at 15:02
  • also, i have 8 more of these so should i implement 8 more AVAudioPlayers or would it be more efficient to use the same one for all 9 – luca590 Apr 01 '11 at 17:10
  • The amount of memory used by 9 pointers is trivial, so it doesn't matter how many fields in a class you have. However, if you are attempting to run 9 `AVAudioPlayer` instances at a time, each one should be managed separately. – Jonathan Grynspan Apr 01 '11 at 17:42
  • When i put this in my header i get "expected identifier or "(" before 'private' – luca590 Apr 01 '11 at 19:31
  • 1
    You have to put it in an Objective-C class' member section. In between `{` and `}` after `@interface SomethingOrOther`. ;) – Jonathan Grynspan Apr 01 '11 at 19:35
  • ah yes wow my fault haha. however the only problem when i do this is that now wen i click a button it doesnt play anything – luca590 Apr 01 '11 at 20:05
  • Amend your question to show the entire method. I can't psychically debug your problems. – Jonathan Grynspan Apr 01 '11 at 20:14
  • i understand that but this didn't fix the problem. The only difference bwtween my current code and my previous is i put in your if statement, added the field in my header, and took out the pointer (AVAudioPlayer *musicPlayer, now it just says musicPlayer) – luca590 Apr 01 '11 at 20:23
  • 1
    You have to put the `[[AVAudioPlayer alloc] init...` line in the second branch. Otherwise it gets executed every time, and then you're back to square one. – Jonathan Grynspan Apr 04 '11 at 15:06