Start New Discussion within our Software Development Community

Hello everybody,

I have an application for the iphone.

When I go into a view, it is supposed to play a sound on repeat until I exit the view.

This works fine the first time. The second (and subsequent) times the sound is played numerous times overlapping each other (two sounds on the second, 3 on the 3rd ... etc).

When I exit the view, it only stops one of the sounds playing

It is being played in the ViewDidLoad method (which is only being called once each time).

I rekon everytime I load the view it adds another of the sounds to the avaudioplayer but never gets rid of the old one.

I have tried setting it to nil before the view closes and have tried [player stop] and [player pause]. All of which have the same effect.

This is in the viewDidLoad method:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Sound Effect - Chicken" ofType:@"wav"];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
    player.numberOfLoops = -1; //infinite

    [player play];

And this is where I load the view (in the appDelegate):

self.animalTestViewController = [[AnimalTestViewController alloc] initWithNibName:@"AnimalTestViewController" bundle:nil];
        self.window.rootViewController = self.animalTestViewController;
        self.animalTestViewController.ringingAlarm = [alarms nextAlarm:dayOfTheWeek];
        if(!allowSnooze_)
            self.animalTestViewController.snoozeButton.hidden = YES;
        [self.window makeKeyAndVisible];

The player is a property (retain, nonatomic) if that helps

Thanks,

This article has been dead for over six months. Start a new discussion instead.