11

This is how I played a beep sound effect in iOS 4:

SystemSoundId beepOnSoundId;

CFURLRef soundUrl = CFBundleCopyResourceURL(
    CFBundleGetMainBundle(), 
    CFSTR("beep"), 
    CFSTR("wav"), 
    NULL
);

// soundUrl logged:
// file://localhost/var/mobile/Applications/ ... beep.wav

AudioServicesCreateSystemSoundID(soundUrl, &beepOnSoundId);

// beepOnSoundId logged: 4097

AudioServicesPlaySystemSound(beepOnSoundId);

It stopped working, when I upgraded my device to iOS 5. I hear nothing. I logged out all the variables and none were nil or 0.

How has the API changed in iOS 5 that breaks my sound playing code?

Update

The problem may be because I have an AVCaptureSession running while I play the sound. iOS 5 somehow doesn't let you do this anymore. I need to play a beep sound while I am recording something from the camera.

Community
  • 1
  • 1
JoJo
  • 19,587
  • 34
  • 106
  • 162
  • I don't know if you have seen my answer but the problem must be something else, because I do also have an AVCaptureSession running and the System Sounds play smoothly and perfectly with iOS5. So reform your question or try to improve your code... – Carles Estevadeordal Oct 30 '11 at 18:35

4 Answers4

13

This bug report could be related to your question, if you have an AVCaptureSession running.

Under iOS 5, when using an AudioServicesPlaySystemSound call, it will not work when there is an active AVCaptureSession with an audio device active.

jbat100
  • 16,757
  • 4
  • 45
  • 70
  • Yes, I have a camera running, which uses AVCaptureSession. When the user taps the record button, I want a beep sound to play. So what's the workaround? – JoJo Oct 25 '11 at 19:35
10

One problem could be that in ios 5 when you call AudioServicesPlaySystemSound, it stop working if there is an active AVCaptureSession with an audio device active.

Also check the names of imports in your files, make sure the audiotoolbox import is in all your viewcontrollers. I also went to a guide and got this for you :) I'll leave the website's name at the bottom

Step 1: Import the AudioToolbox Framework Begin by importing the Audio Toolbox framework into your application in order to make the System Sound Services available to your code. To do so, select the “PhoneAppSkin” project in the Project Navigator pane in Xcode, then select “PhoneAppSkin” under “TARGETS”, and finally select the “Build Phases” tab. After doing so, your screen should look something like this:

enter image description here

Next, click the “Link Binary With Libraries” drop down, and click the “+” symbol to add a new framework to our project’s linking phase.

enter image description here

Finally, find the “AudioToolbox” framework in the popup window, and then click “Add”.

enter image description here

Next, open up the PhoneViewController.h file and add the line necessary to actually import the Audio Toolbox framework into your class:

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

The Audio Toolbox functions should now work

Website I got the images from: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_playing-systemsoundid/

Gabriel
  • 3,039
  • 6
  • 34
  • 44
  • I wonder how this answer got a `-1`!. Even though is could not be the solution, it is so explicative and well formatted! It worths at least +1 – nacho4d Oct 31 '11 at 14:14
  • I upped you back to 0 for the effort, however just posting a link to the website would have been enough, also linking the audio toolbox does not seem to be the issue here. – jbat100 Nov 02 '11 at 09:22
6

I have almost the same code and it works for me in iOS5 with a video and an audio media sessions on. If you have all the code in a function to play the sound the problem may be that the SystemSoundID object gets deallocated before the sound can be played, or the problem may be with the URL. But I guess it is the first one.

See, the following code works:

The SystemSoundID declaration is in the header of the object and the object lives long enough to play the sound.

    SystemSoundID soundID;

The AudioServicesCreateSystemSoundID is in the object initialization.

    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                                                   pathForResource: @"sound" ofType:@"wav"]], &soundID);

Then you can use AudioServicesPlaySystemSound wherever you want inside the object.

    AudioServicesPlaySystemSound(soundID);

Try putting this pieces codes in the appropriate places and you should hear the sound.

1

In my Constructor:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
NSURL* musicFile  = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                   pathForResource:@"video-sent"
                                                   ofType:@"caf"]];
self.sentSound = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[self.sentSound prepareToPlay];

The function playSentSound

- (void)playSentSound
{
    DLog(@"PLay Sound %@", self.sentSound);
    [self.sentSound play];
}

then later in the code i just call it

[self playSentSound];

The key here are the first 4 lines ... ;-)

Kordi
  • 2,405
  • 1
  • 14
  • 13