19

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?!

Matt Facer
  • 3,103
  • 11
  • 49
  • 91

4 Answers4

48

So, I had this issue too - well I'm pretty sure it's the same one...

We've had an app in the app store for a year or so now and recently we needed to change a bit of content although nothing functional.

Suddenly, the sound stopped working - Both the simulator in the latest sdk version (version 4.0) AND on device too (again running iOS 4).

The code that always worked for us was this...

NSString *sound_file;
if ((sound_file = [[NSBundle mainBundle] pathForResource:@"track1" ofType:@"mp3"])){

NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.delegate = self;
[url release];

[audioPlayer prepareToPlay];
[audioPlayer play];

}

Finally, I found out that you now need to set the type of AVAudioSession playback just to get sound to play through the speaker as it already did! Put following line of code in your app delegate applicationDidFinishLaunching event handler...

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

Not forgetting to add the include in your app delegate .h file (obviously you need to import AVFoundation framework too if not already done so)...

#import <AVFoundation/AVAudioSession.h>

Hopefully this'll now make your sound play on device.

Don't get cuaght out by what I think might be a separate issue, and that's the sound still doesn't play in the simulator. I've seen other posts suggesting this is the case, but I'm not sure how widespread this is. I found out that if I selected iPad 3.2 as my simulator, it at least worked on that. The joy!

What seems crazy to me is that, surely this must be effecting loads of people and yet it's quite hard to find info or suggestions about something that should be quite a well known issue - after all, I've seen loads of posts on forums that don't seem to have been answered.

Anywayz, hope this helps somebody else.

Jon Andrews
  • 481
  • 4
  • 3
  • wow this 1 worked for me , i was facing same problem, thanx @Jon Andrews! – Dhaval Panchal Sep 01 '11 at 11:37
  • 4
    The code `[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];` simply overrides the silent switch and lets you play sounds when the phone is locked AFAIK. – epetousis Mar 11 '12 at 08:09
  • Finally! It took an hour of googling, converting, importing, trying, and this finally worked. Thanks a lot! – karpathy Apr 16 '12 at 04:14
  • I wish i could +100. Thanks a lot @Jon. – Yama Jun 25 '12 at 04:58
  • terrific! I can fix a similar trouble. In my case, it did not work on ios5 device but simulator was OK. Thanks – zono Nov 11 '12 at 08:19
  • Thanks for that tip! Code worked perfectly fine on an iPhone and nothing at all happened on an iPad 4. Never realised it had a silent switch set (the volume was also low but turning it up made no difference). – gnasher729 May 09 '15 at 20:18
26

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!

Anjaan
  • 660
  • 6
  • 10
17

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.

rob5408
  • 2,972
  • 2
  • 40
  • 53
1

have you tried [NSURL URLWithString:pathToMusicFile1]; instead of fileURLWithPath:?