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.
Related
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)
})
}
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 have an app that uses AVAudioPlayer to play sound effects, music, etc. It works fine for just about every user (100,000+ users), but I've had several reports of sound not playing at all even though sound effects are working on other apps on the same device. The bug has gone away for some users after an app restart/device reboot, but for others it continues to persist. AVSpeechSynthesizer isn't working for these users either.
I haven't been able to reproduce the bug to see if the AVAudioPlayer is throwing an error, so I'm not sure if it is - but I don't see why it would be working fine for 99.9% of users and not this tiny subset.
Has anyone else run into something like this?
Here's the code I'm using to set up the player:
[[AVAudioSession sharedInstance] setCategory:#"AVAudioSessionCategoryAmbient" error:nil];
NSString *file = [filename stringByDeletingPathExtension];
NSString *extension = [filename pathExtension];
NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:file withExtension:extension];
self.soundPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
[self.soundPlayer1 prepareToPlay];
Ok, here is the situation. I am writing an iOS application, and I have a text scrolling from bottom to top. At a certain point, let's say when the word "EXAMPLE" shows up, I would like to my app automatically play particular sound, let's say "JINGLE".
How do I do it?
import <AudioToolbox/AudioToolbox.h>
-(void) playSound {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"changeTrack" ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
//[soundPath release];
}
you call in every condition you want
I am trying to play a sound on button click using the AVFoundation framework. Previously, i used audio toolbox however when i use the audioplayer nothing comes out. What is weird is that when i test it on my phone and i press the button, the volume controls aren't the normal ringer volume controls. This suggests that it should be playing but i can't hear anything, on my phone or the simulator. I have used different files, created new project changed the code, nothing has helped.
Here's the code in .h:
#interface MainViewController : UIViewController <AVAudioPlayerDelegate>
- (IBAction)test;
Here's the code in .m:
- (IBAction)test {
NSString *path = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
can you do this before you play the sound i think you might have the wrong category:
AudioSessionInitialize (NULL,NULL,NULL,NULL);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,sizeof (sessionCategory),&sessionCategory);
AudioSessionSetActive(true);