views:

45

answers:

1

I'll keep it short and sweet - I'm building an application just for practice before I buy myself the iPhone Developer Program.

I'm experimenting with the AVFoundation.framework and I keep running into a bug which will only let me play the first sound I initialize in my code. Do you think any of you could help me out? Thanks in advance!! :)

view controller

-(IBAction)play {
    if (segments.selectedSegmentIndex == 0) {
        NSLog(@"Segment = 0");
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path = [bundle pathForResource:@"meow" ofType:@"wav"];

        if (path != nil) {
            NSURL *url = [NSURL fileURLWithPath:path];
            AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
            [player prepareToPlay];
            [player play];
        }
    }

    else if (segments.selectedSegmentIndex == 1) {
        NSLog(@"Segment = 1");
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path2 = [bundle pathForResource:@"meowloud" ofType:@"wav"];

        if (path2 != nil) {
            NSURL *url2 = [NSURL fileURLWithPath:path2];
            AVAudioPlayer *player2 = [[AVAudioPlayer alloc]initWithContentsOfURL:url2 error:NULL];
            [player2 prepareToPlay];
            [player2 play];
//          [player2 release];
        }
    }

    text1.textColor = [UIColor clearColor];
    text2.textColor = [UIColor clearColor];
    text3.textColor = [UIColor clearColor];
    text4.textColor = [UIColor clearColor];

}

When this code gets executed, only the meow.wav is executed, no matter which segment is selected.

+2  A: 

Your code has a couple of memory leaks. You allocate instances of AVAudioPlayer in your play method, but you never release those instances. See Cocoa's memory management qguidelines for details. Since the AVAudioPlayer instances need to remain in memory to play, the easiest solution is to add a member variable to your viewController class and set it up as a retain property.

For the sake of Don't Repeat Yourself (DRY), I would also suggest adding a method which encapsulates all of the code needed to play a single wav file.

Here is how I would write this view controller:

MyViewController.h

@interface MyViewController : UIViewController
{
    AVAudioPlayer *player;
}
@property(nonatomic, retain) AVAudioPlayer *player;
- (void)playWavFile:(NSString *)fileName;
- (IBAction)play;
@end

MyViewController.m

@synthesize player;

- (void)dealloc {
    [player release];
}

- (void)playWavFile:(NSString *)fileName {
    NSBundle *bundle = [NSBundle mainBundle];   
    NSString *path = [bundle pathForResource:fileName ofType:@"wav"];

    if (path != nil) {
        AVAudioPlayer *newPlayer;
        NSURL *url = [NSURL fileURLWithPath:path];
        newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
                                                           error:NULL];
        [self setPlayer:newPlayer];
        [newPlayer release];

        [newPlayer prepareToPlay];
        [newPlayer play];
    }
}

- (IBAction)play {

    if (segments.selectedSegmentIndex == 0) {
        [self playWavFile:@"meow"];
    }
    else if (segments.selectedSegmentIndex == 1) {
        [self playWavFile:@"meowloud"];
    }
}
e.James
Thank you so much!!! I'll try it and report back. In the meantime, how can you [newPlayer release]; before you call prepareToPlay and play?
esqew
You can do that because of the line above it. `[self setPlayer:newPlayer]` makes use of the property accessor for the `player` member variable, which means that it is automatically retained (because the property was defined as a retain property). After that call is made, your viewController "owns" the player, so you can safely call `release` to balance out the call to `alloc`.
e.James
Crap... it doesn't work. I get:**Program received signal: “SIGABRT”.**2010-06-30 19:48:41.334 Merrrw![4713:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AVAudioPlayer initWithContentsOfURL:]: unrecognized selector sent to instance 0x6302f80'Any ideas?Thanks again for all the help. :)
esqew
Make sure the `filename` that gets passed into `initWithContentsOfURL` exists in XCode (i.e. you added it to the resources folder if it's a sound file). You can also check the code at various steps with an NSLog to see if it all is working... also, shouldn't `[newPlayer prepareToPlay];` and `[newPlayer play];` actually be `[player prepareToPlay];` and `[player play];` ? Or am I not counting my retains correctly? Just curious
iWasRobbed
@seanny94: Sorry, my fault. I forgot to include the error argument in the call to initWithContentsOfURL: I'll fix it in my code.
e.James
@IWasRobbed: Either one will do at that point, since the call to `[self setPlayer:newPlayer]` retains the variable, and `newPlayer` still contains a valid reference to it. If you were to display the value of each of those pointers, they would still point to the same thing (a single instance of `AVAudioPlayer`)
e.James
Thanks! :D I should have realized that myself, though. :/
esqew
No trouble. I'm glad to hear it worked `:)`
e.James