开发者

Bug in AVAudioPlayer class?

开发者 https://www.devze.com 2023-01-05 12:25 出处:网络
I\'ll keep it short and sweet - I\'m building an application just for practice before I buy myself the iPhone Developer Program.

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 = [开发者_C百科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.


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"];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号