0

I'm having a problem with AudioServicesPlaySystemSound in my iOS app.

I have a method defined to play a system sound. When I call the method from viewDidLoad, I can hear the sound play, but when I call it from a button handler, I do not hear the sound play.

Here's the code from my view controller:

SystemSoundID startSound = 0;

-(void)playStartSound
{        
    AudioServicesPlaySystemSound(startSound);
}

-(void)viewDidLoad 
{           
    NSURL *urlStart = [[NSBundle mainBundle] URLForResource:@"Beep" withExtension:@"wav"];
    AudioServicesCreateSystemSoundID((CFURLRef)urlStart, &startSound);

    [self playStartSound];    

    [super viewDidLoad];
}

- (IBAction)start:(id)sender
{
    [self playStartSound];
}

The IBAction method is associated with a button. When I select the button, the playStartSound method gets called, but the sound is not audible.

The system sound is not disposed of with AudioServicesDisposeSystemSoundID until the dealloc method is called.

I've tried calling AudioServicesCreateSystemSoundID from within playStartSound, but it didn't make any difference.

The app also uses AV Foundation, Media Player, Core Media, Core Video, and Core Location. I'm wondering if one of these might be interfering with AudioToolbox?

UPDATE: I just put together a simple app that just contains the above code and a button and the sound plays just fine when I select the button.

So then I removed all the video capture code from my controller and now the audio plays fine.

Avalanchis
  • 4,500
  • 3
  • 39
  • 48

2 Answers2

4

This was not working because I had an active video capture session in my application.

AudioServicesPlaySystemSound will not play when there is an active video capture session.

Avalanchis
  • 4,500
  • 3
  • 39
  • 48
0
   NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"7777" ofType:@"mp3"];
   SystemSoundID soundID;
   AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath: soundPath]), &soundID);
   AudioServicesPlaySystemSound (soundID);

Hey try like that it will play

Ahmed Z.
  • 2,329
  • 23
  • 52