2

I know this question has been asked 999 times. All the answers say to go to System Preferences > Sound > Sound Effects and to make sure "Play user interface sound effects" is checked, or to uncheck and recheck it.

This does not work for me. I have it checked but sound effects are still not playing.

Some more information:

  1. I am trying to use SystemSoundID to play sound effects.
  2. I previously used AVAudioPlayer for sound effects and it worked.
  3. Went on YouTube from Safari in the simulator and video sound did play.
  4. I have linked AudioToolbox.framework to my project.
  5. I downloaded Apple sample code from here.

This did not play either, so I am quite sure there is nothing wrong with my code. 6. I am running my Mac on Yosemite, OSx 10.10.1 with XCode 6.1.1.

I have not yet tested it on an actual device as I currently do not have a Developer's account. I do not wish to pay my life savings of $99 just to find out there was a box somewhere that I had left unchecked.

Is there anything else I should try?

My code:

SoundEffect.h

#import <AudioToolbox/AudioServices.h>

@interface SoundEffect : NSObject
{
    SystemSoundID soundID;
}

- (id)initWithSoundNamed:(NSString *)filename withExtension:(NSString*) extension;

- (void)play;

@end 

SoundEffect.m

#import "SoundEffect.h"

@implementation SoundEffect

- (id)initWithSoundNamed:(NSString *)filename withExtension:(NSString *)extension
{
    if ((self = [super init]))
    {
        NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:extension];
        if (fileURL != nil)
        {
            NSLog(@"fileURL found");
            SystemSoundID theSoundID;
            OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
            if (error == kAudioServicesNoError)
                soundID = theSoundID;
        }
    }
    return self;
}

- (void)dealloc
{
    AudioServicesDisposeSystemSoundID(soundID);
}

- (void)play
{
    if([[NSUserDefaults standardUserDefaults] boolForKey:soundEffectsKey]) {
        NSLog(@"Play sound effect");
        AudioServicesPlaySystemSound(soundID);
    }else {
        NSLog(@"Sound effects are disabled");
    }
}

Note: I create this object and play it wherever I need to in my project. When I try to play my sounds, "fileURL found" and "Play sound effect" are both logged.

Source: Play a short sound in iOS

Community
  • 1
  • 1
aanrv
  • 2,159
  • 5
  • 25
  • 37

1 Answers1

0
    SystemSoundID customSound;

NSString *soundFile = [[NSBundle mainBundle]pathForResource:<#customSoundName#> ofType:<#soundFormat#>];
    NSURL *soundURL = [NSURL fileURLWithPath:openMenu];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, & customSound);
        AudioServicesPlaySystemSound(customSound);

This is works on mine

RyanTCB
  • 7,400
  • 5
  • 42
  • 62