I have AVFoundation and AudioToolbox frameworks added to my project. In the class from where I want to play a system sound, I #include <AudioToolbox/AudioToolbox.h> and I call AudioServicesPlaySystemSound(1007);. I'm testing in a device running iOS 8, sounds are on and volume is high enough, but I don't hear any system sound when I run the app and AudioServicesPlaySystemSound(1007); is called... what could I be missing?
With iOS10 playing audio like this doesn't work:
SystemSoundID audioID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &mySSID);
AudioServicesPlaySystemSound(audioID);
Use this instead:
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &audioID);
AudioServicesPlaySystemSoundWithCompletion(audioID, ^{
AudioServicesDisposeSystemSoundID(audioID);
});
According to the documentation:
This function (AudioServicesPlaySystemSound()) will be deprecated in
a future release. Use AudioServicesPlaySystemSoundWithCompletion
instead.
Use the following code snippet to play sounds:
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; //filename can include extension e.g. #"bang.wav"
if (fileURL)
{
SystemSoundID theSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
if (error == kAudioServicesNoError)
{
AudioServicesPlaySystemSoundWithCompletion(theSoundID, ^{
AudioServicesDisposeSystemSoundID(theSoundID);
});
}
}
Also, the completion block ensures that the sound play was completed before it is disposed of.
If this doesn't fix the issue, maybe your problem isn't code related but rather setting related (device on silent / simulator sounds muted from MAC System Preferences, make sure "Play user interface sound effects" is checked)
This will play system sound.
But remember system sound will not play longer sound.
NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:#"engine" ofType:#"mp3"];
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_engineSound);
AudioServicesPlaySystemSound(_engineSound);
I have just tested the code on an iPad and iPhone running iOS 8, and it is working on the real devices.
For some very strange reason, it isn't working on iOS 8 simulator for any device, even though it works on iOS 7 and 7.1 simulators.
Otherwise below code works fine in all real devices.
NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:#"engine" ofType:#"mp3"];
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_engineSound);
AudioServicesPlaySystemSound(_engineSound);
for swift 3.x and xcode 8:
var theSoundID : SystemSoundID = 0
let bundleURL = Bundle.main.bundleURL
let url = bundleURL.appendingPathComponent("Invitation.aiff")
let urlRef = url as CFURL
let err = AudioServicesCreateSystemSoundID(urlRef, &theSoundID)
if err == kAudioServicesNoError{
AudioServicesPlaySystemSoundWithCompletion(theSoundID, {
AudioServicesDisposeSystemSoundID(theSoundID)
})
}
Related
I'm trying to play a sound when a button is clicked. First, I imported the AudioToolbox in the header file, then added the code into the IBAction that fires when the button is pressed, like so:
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)button1:(id)sender {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"mySong" ofType:#"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
}
I even threw an NSLog in the method to make sure it's firing. It is. I also but in a foo path in the string to see if it would crash, it did. I made sure that I dragged and dropped the short sound file into the app as well. Tested my speakers, tested audio on the device simulator by going to youtube on the simulator, everything. I can't figure out what I'm doing wrong.
When I changed the code to this:
- (IBAction)button1:(id)sender {
AVAudioPlayer *testAudioPlayer;
// *** Implementation... ***
// Load the audio data
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;
// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
if(audioError != nil) {
NSLog(#"An audio error occurred: \"%#\"", audioError);
}
else {
[testAudioPlayer setNumberOfLoops: -1];
[testAudioPlayer play];
}}
I get the error: WARNING: 137: Error '!dat' trying to set the (null) audio devices' sample rate
Hi as per you question can you please check whether you audio file is present in you project folder.It might happen that u have forgotten to checkmark "copy item if needed" so it might not be there in your project file.
You can also play sounds provided in device. Visit the below link it will be helpful.
https://github.com/TUNER88/iOSSystemSoundsLibrary
I've tested your code and it working fine and not getting crashed. Here's what I did.
As said in #matt's comment, sound playback fails in the Simulator. I had this problem when trying to play back a video.
The funny part is that sound plays just fine for the same video inside the Photos app.
I've been working on a custom keyboard for iOS 8 for some time and everything went fine so far, but I still couldn't get my head around this tapping sound stuff.
I searched high and low for this issue and tried several approaches including
Using AudioToolbox
Using AVFoundation
Put the tock.caf inside my bundle and just play it
Some of them works, in the simulators but none of them works in my devices.
Could anyone who has successfully played sound when tapping on custom keyboard buttons care to share some working code? And it is the best if the code could honor the sound settings.
As Farzad Nazifi mentioned above Allow openAcesess after a fresh install , solved my issue .
And I recommend a simpler solution for playing system keyboard tap sound
Swift and Objective-C All the same `we don't need to import any custom sound file just play the system tap sound.
Import AudioToolbox framework:
#import <AudioToolbox/AudioToolbox.h>
AudioServicesPlaySystemSound(1104)
This code is tested in iOS 8 beta 5 , I hope apple would fix the bug (if it is a bug) in the GM version .
Finally I got an answer from other SO thread.
- (void)playSound
{
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"Tock" ofType:#"caf"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
}
I have implemented and verified this method works on both simulators and devices on iOS8 Beta 2.
Import the framework:
#import <AudioToolbox/AudioToolbox.h>
And use:
AudioServicesPlaySystemSound(1104);
The key here is that iOS can only play the file types described here. iOS cannot play the file type .caf. The following code should work fine on iOS. You can use this website to convert your .caf file to any file they have available on the site and that are compatible. I've tested it out already and .caf conversions work even though it's not specified anywhere.
-(void)buttonPressed:(UIButton *)theButton {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
AVAudioPlayer *av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"Tock" ofType:#"mp3"]] error:nil];
[av setVolume:1.0];
[av setNumberOfLoops:0];
[av prepareToPlay];
[av play];
}
Playing sounds requires OpenAccess, I don't know why Apple is doing it like this but it is what it is for now. This will also fix the lag and issues with it not working after trying to play the sound.
You can get OpenAccess by setting YES for the value of RequestsOpenAccess Key in the info.plist of the keyboard.
Next you should import AudioToolbox to your project, then use this code:
- (void)playSound{
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"Tock" ofType:#"caf"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
Now install your keyboard and give the Openaccess while adding it to your keyboards and it should work.
You can use playInputClick() method.
Swift:
UIDevice.currentDevice().playInputClick()
Objective-C:
[[UIDevice currentDevice] playInputClick];
See documentation for details.
I have the following code that used to work to play a sound in my project however after iv done some playing around over the past few weeks on other aspects of the project it dose not seem to play.
The audio file is in the main directory and works in preview.
Any ideas?
//playsound
AVAudioPlayer *showsound;
NSString *audiopath = [[NSBundle mainBundle] pathForResource:#"mouse1" ofType:#"wav"];
NSURL *audiourl = [NSURL fileURLWithPath:audiopath];
showsound = [[AVAudioPlayer alloc]initWithContentsOfURL:audiourl error:Nil];
[showsound play];
It's because after the line showsound play your method comes to an end and showsound (your AVAudioPlayer) is released. Hence there is nothing to do any playing any more. Solution: retain it! (For example, set an instance variable to hold on to it.)
There is a sound file called lock.aiff in side SpringBoard.app if the iOS 7 SDK. The path for the sound file is as follows.
iPhoneSimulator7.0.sdk/System/Library/CoreServices/SpringBoard.app/lock.aiffs
This is my code.
NSString *path = [[NSBundle bundleWithIdentifier:#"com.apple.UIKit"] bundlePath];
NSString *frameworkPath = [path stringByDeletingLastPathComponent];
NSString *libraryPath = [frameworkPath stringByDeletingLastPathComponent];
NSString *finalePath = [libraryPath stringByAppendingString:#"/CoreServices/SpringBoard.app/lock.aiff"];
NSURL *fileURL = [NSURL fileURLWithPath:finalePath];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)fileURL,&soundID);
AudioServicesPlaySystemSound(soundID);
Value of the variable finalePath = "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/System/Library/CoreServices/SpringBoard.app/lock.aiff"
But I am unable to play this sound file on the simulator. Also so I can't play any of the sound file inside SpringBoard.app. But I can play any other file successfully. For example the sound file at the following location is playing well.
iPhoneSimulator7.0.sdk/System/Library/PrivateFrameworks/AOSNotification.framework/findme_alarm_2.aiff
My guess is that since the lock.aiffs is inside the SpringBoard.app, it causes some sort of a problem.So how can I play the lock.aiff file.
You cannot access the resources in another app bundle because of the sandboxed nature of iOS apps.
However, you can play the system sounds, such as the lock sound, like this:
NSURL *fileURL = [NSURL URLWithString:#"/System/Library/Audio/UISounds/lock.caf"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)fileURL,&soundID);
AudioServicesPlaySystemSound(soundID);
Here's a full list of system sounds from iOS 7.0.2 that you can access:
/System/Library/Audio/UISounds/Modern/airdrop_invite.caf
/System/Library/Audio/UISounds/Modern/calendar_alert_chord.caf
/System/Library/Audio/UISounds/Modern/camera_shutter_burst.caf
/System/Library/Audio/UISounds/Modern/camera_shutter_burst_begin.caf
/System/Library/Audio/UISounds/Modern/camera_shutter_burst_end.caf
/System/Library/Audio/UISounds/Modern/sms_alert_aurora.caf
/System/Library/Audio/UISounds/Modern/sms_alert_bamboo.caf
/System/Library/Audio/UISounds/Modern/sms_alert_circles.caf
/System/Library/Audio/UISounds/Modern/sms_alert_complete.caf
/System/Library/Audio/UISounds/Modern/sms_alert_hello.caf
/System/Library/Audio/UISounds/Modern/sms_alert_input.caf
/System/Library/Audio/UISounds/Modern/sms_alert_keys.caf
/System/Library/Audio/UISounds/Modern/sms_alert_note.caf
/System/Library/Audio/UISounds/Modern/sms_alert_popcorn.caf
/System/Library/Audio/UISounds/Modern/sms_alert_synth.caf
/System/Library/Audio/UISounds/New/Anticipate.caf
/System/Library/Audio/UISounds/New/Bloom.caf
/System/Library/Audio/UISounds/New/Calypso.caf
/System/Library/Audio/UISounds/New/Choo_Choo.caf
/System/Library/Audio/UISounds/New/Descent.caf
/System/Library/Audio/UISounds/New/Fanfare.caf
/System/Library/Audio/UISounds/New/Ladder.caf
/System/Library/Audio/UISounds/New/Minuet.caf
/System/Library/Audio/UISounds/New/News_Flash.caf
/System/Library/Audio/UISounds/New/Noir.caf
/System/Library/Audio/UISounds/New/Sherwood_Forest.caf
/System/Library/Audio/UISounds/New/Spell.caf
/System/Library/Audio/UISounds/New/Suspense.caf
/System/Library/Audio/UISounds/New/Telegraph.caf
/System/Library/Audio/UISounds/New/Tiptoes.caf
/System/Library/Audio/UISounds/New/Typewriters.caf
/System/Library/Audio/UISounds/New/Update.caf
/System/Library/Audio/UISounds/ReceivedMessage.caf
/System/Library/Audio/UISounds/RingerChanged.caf
/System/Library/Audio/UISounds/SIMToolkitCallDropped.caf
/System/Library/Audio/UISounds/SIMToolkitGeneralBeep.caf
/System/Library/Audio/UISounds/SIMToolkitNegativeACK.caf
/System/Library/Audio/UISounds/SIMToolkitPositiveACK.caf
/System/Library/Audio/UISounds/SIMToolkitSMS.caf
/System/Library/Audio/UISounds/SentMessage.caf
/System/Library/Audio/UISounds/Swish.caf
/System/Library/Audio/UISounds/Tink.caf
/System/Library/Audio/UISounds/Tock.caf
/System/Library/Audio/UISounds/Voicemail.caf
/System/Library/Audio/UISounds/alarm.caf
/System/Library/Audio/UISounds/beep-beep.caf
/System/Library/Audio/UISounds/begin_record.caf
/System/Library/Audio/UISounds/begin_video_record.caf
/System/Library/Audio/UISounds/ct-busy.caf
/System/Library/Audio/UISounds/ct-call-waiting.caf
/System/Library/Audio/UISounds/ct-congestion.caf
/System/Library/Audio/UISounds/ct-error.caf
/System/Library/Audio/UISounds/ct-keytone2.caf
/System/Library/Audio/UISounds/ct-path-ack.caf
/System/Library/Audio/UISounds/dtmf-0.caf
/System/Library/Audio/UISounds/dtmf-1.caf
/System/Library/Audio/UISounds/dtmf-2.caf
/System/Library/Audio/UISounds/dtmf-3.caf
/System/Library/Audio/UISounds/dtmf-4.caf
/System/Library/Audio/UISounds/dtmf-5.caf
/System/Library/Audio/UISounds/dtmf-6.caf
/System/Library/Audio/UISounds/dtmf-7.caf
/System/Library/Audio/UISounds/dtmf-8.caf
/System/Library/Audio/UISounds/dtmf-9.caf
/System/Library/Audio/UISounds/dtmf-pound.caf
/System/Library/Audio/UISounds/dtmf-star.caf
/System/Library/Audio/UISounds/end_record.caf
/System/Library/Audio/UISounds/end_video_record.caf
/System/Library/Audio/UISounds/jbl_ambiguous.caf
/System/Library/Audio/UISounds/jbl_begin.caf
/System/Library/Audio/UISounds/jbl_cancel.caf
/System/Library/Audio/UISounds/jbl_confirm.caf
/System/Library/Audio/UISounds/jbl_no_match.caf
/System/Library/Audio/UISounds/lock.caf
/System/Library/Audio/UISounds/long_low_short_high.caf
/System/Library/Audio/UISounds/low_power.caf
/System/Library/Audio/UISounds/mail-sent.caf
/System/Library/Audio/UISounds/middle_9_short_double_low.caf
/System/Library/Audio/UISounds/new-mail.caf
/System/Library/Audio/UISounds/photoShutter.caf
/System/Library/Audio/UISounds/shake.caf
/System/Library/Audio/UISounds/short_double_high.caf
/System/Library/Audio/UISounds/short_double_low.caf
/System/Library/Audio/UISounds/short_low_high.caf
/System/Library/Audio/UISounds/sms-received1.caf
/System/Library/Audio/UISounds/sms-received2.caf
/System/Library/Audio/UISounds/sms-received3.caf
/System/Library/Audio/UISounds/sms-received4.caf
/System/Library/Audio/UISounds/sms-received5.caf
/System/Library/Audio/UISounds/sms-received6.caf
/System/Library/Audio/UISounds/sq_alarm.caf
/System/Library/Audio/UISounds/sq_beep-beep.caf
/System/Library/Audio/UISounds/sq_lock.caf
/System/Library/Audio/UISounds/sq_tock.caf
/System/Library/Audio/UISounds/tweet_sent.caf
/System/Library/Audio/UISounds/unlock.caf
/System/Library/Audio/UISounds/ussd.caf
/System/Library/Audio/UISounds/vc~ended.caf
/System/Library/Audio/UISounds/vc~invitation-accepted.caf
/System/Library/Audio/UISounds/vc~ringing.caf
I am trying to play my audio file, which is 1.9 seconds, in my iOS App with these lines of code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"bell" ofType:#"wav"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
SystemSoundID audioEffect;
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
AudioServicesDisposeSystemSoundID(audioEffect);
The audio file never plays but the code gets called.
EDIT: file bell.wav
bell.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 48000 Hz
The problem is that you are disposing the sound before it has a chance to start playing.
My book teaches you how to do this. Here is the code I provide in my book:
void SoundFinished (SystemSoundID snd, void* context) {
// NSLog(#"finished!");
AudioServicesRemoveSystemSoundCompletion(snd);
AudioServicesDisposeSystemSoundID(snd);
}
- (IBAction)doButton:(id)sender {
NSURL* sndurl = [[NSBundle mainBundle] URLForResource:#"test" withExtension:#"aif"];
SystemSoundID snd;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)sndurl, &snd);
AudioServicesAddSystemSoundCompletion(snd, nil, nil, SoundFinished, nil);
AudioServicesPlaySystemSound(snd);
}
You can adapt that to use your sound and to trigger the playing in the way you need.
However, do note that you should not be using a 1.9 second sound as a system sound. It is much better to use an AVAudioPlayer (which my book also teaches you to do).