Question: I am testing my app on my 3GS iPhone with iOS 4.2

I am using the following code which plays a sound in my IBAction. It works perfectly in the simulator (both iPad and iPhone) - I hear the sound. 

```
NSString *pathToMusicFile1 = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"mp3"];
NSError *error;
alarmSound = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile1] error:&error]; 
NSLog(@"Song1 Loaded");
if (alarmSound == nil) {
    NSLog(@"Error playing sound");
} else {
    alarmSound.numberOfLoops = 20;
    alarmSound.volume = 1.0;
    [alarmSound play];
}

```

I have everything declared properly (headers and frameworks etc) as I can hear the sound in the simulator. 

I have also checked that my phone is NOT in silent mode!! I hear other sounds from the generic iphone GUI. (example from a datepicker).

I have also cleaned all targets and deleted the app to reinstall. 

Any ideas what's wrong?!

Answer: 
It is **extremely important** to have the "audioPlayer" variable as a property. Otherwise with ARC, it gets released before it gets a chance to play!

It took me half an hour and some googling for this A-ha moment.

My sound works perfectly now!
My problem was that iOS was autoreleasing my AVAudioPlayer instance before the file got a chance to play. Strangely this only happened with MP3s and not AU files. Solution was to not set it as autorelease and then release it in the delegate method...

```
- (void)playSound:(NSString *)sound
{
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:sound ofType:@""]] error:NULL];
    audioPlayer.delegate = self;
    [audioPlayer play];
}

```

...then in the delegate method...

```
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [player release];
}

```

...works great now.