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.
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.
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:
Next, click the “Link Binary With Libraries” drop down, and click the “+” symbol to add a new framework to our project’s linking phase.
Finally, find the “AudioToolbox” framework in the popup window, and then click “Add”.
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/
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.
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 ... ;-)
Related
I am trying to play my music within my game app and it crashes. But when I copy and paste the code within a different xcode project it works?
I'm using a 2d game engine called SpriteKit within Xcode. I play an mp3 file using the AVFoundation framework.
NSURL *url = [[NSBundle mainBundle] URLForResource:#"backgroundMusic" withExtension:#"mp3"];
self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.backgroundMusic.numberOfLoops = 1;
[self.backgroundMusic prepareToPlay];
[self.backgroundMusic play];
AVAudioPlayer throws breakpoint in debug mode
I've had this problem before, this answer helped me:
"Add your exception breakpoint and edit the exception type from "All" to "Objective-C exceptions"
Some classes in AudioToolbox throw regular C++ exceptions. You can filter them off this way."
I have tried several options (many of them referenced from this post on SO and elsewhere) to get music to play on the iPhone simulator, but so far none of it has worked. I have tried -
Unchecking, and then checking the box that says "Player user interface sound effects" in the Preferences > Sound option
Going to Audio Midi Set Up, and changing the sample rate to 44100
Ensuring that the output/input is both using the internal speakers
Make sure that I imported <AVFoundation/AVFoundation.h> and added the AVFoundation.framework to my linked frameworks.
Resetting iOS simulator using Reset Settings/Contents
Restarting XCode, as well as as the computer
I have the following code in viewDidLoad that is play the file gundam.mp3
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"gundam" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = 1;
player.volume = 0.9;
NSLog(#"The sound URL is %#", soundFileURL);
[player play];
The NSLog seems to be able to capture the file correctly: The sound URL is file://localhost/Users/zallanx/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/[...]/Missile%20Defender.app/gundam.mp3
I am unsure what I am doing incorrectly based on all the options I have tried, and would appreciate your help. Thanks!
Im using the AVFoundation framework to play sound files. The problem im having is that its stopping music from playing when the audio file gets used, im not saying play both files continuously, but play the sound file, then pick up the ipod music right where it left off. Is there any way i can use AVFoundation is this kind of way? or is there a better framework for it?
Here is what my code looks like:
click = [NSURL fileURLWithPath:[NSString stringWithFormat:#"#/Click.WAV", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:click error:nil];
audioPlayer.numberOfLoops = 1;
[click release];
[audioPlayer play];
This code works completely fine, i had to type it out so ignore any problems that there might be with it.
Thanks,
Jacob
You can use the AVAudioSession class to change the audio "category" of your app: thus you can allow it to play on top of the iPod music. Use the -setCategory:error: method, and you will probably want to use AVAudioSessionCategoryAmbient. More info can be found in the Audio Session Programming Guide.
I'm using the following to play an m4a file:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: fileName];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
It works fine on the simulator but I hear nothing on the device. Sounds files I'm using all stay in the bundle. Here is what filePath looks like from the device:
file://localhost/var/mobile/Applications/418945F3-3711-4B4D-BC65-0D78993C77FB/African%20Adventure.app/Switch%201.m4a
Is there an issue with the file path or any thing different I need to do for the device?
Just as a sidenote - I was having the exact same problem and spent probably close to an hour on converting files to the correct format, etc.. Yet the problem was the "mute" switch on the iPad. So even though the volume was up, and I could hear other sounds on the iPad, because the mute switch was turned on, it wasn't playing system sounds.
To add to the confusion, this app uses text-to-speech and the volume coming from the dictation was perfectly fine, it was only the sounds coming from AudioServicesPlaySystemSound() that weren't being played.
I had trouble with this too. Finally I realised it was because AudioServices can only play audio with the following constratints.
Sound files that you play using this
function must be:
- No longer than 30 seconds in duration
- In linear PCM or IMA4 (IMA/ADPCM) format
- Packaged in a .caf, .aif, or .wav file
From Apple docs: http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html
You might want to use the AVAudioPlayer instead of AudioServices.
The following code will take an audio file (.m4a) and play the audio file 1 time. Don't forget to release "audioPlayer" when you're done with it.
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"m4a"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
{
NSLog([error description]);
}
else
{
[audioPlayer play];
}
Hope this example helps you with playing audio on the actual device. It might also be a good idea to increase the device audio when the file is playing.
Note: You will need to add the AVFoundation framework to your project if you have not already done so. As well as import the header file.
#import <AVFoundation/AVFoundation.h>
Update:
From Apple's Core Audio Overview Document
Audio Session Services
Audio Session Services lets you manage audio sessions in your application—coordinating the audio behavior in your application with background applications on an iPhone or iPod touch. Audio Session Services consists of a subset of the functions, data types, and constants declared in the AudioServices.h header file in AudioToolbox.framework.
The AVAudioPlayer Class
The AVAudioPlayer class provides a simple Objective-C interface for playing sounds. If your application does not require stereo positioning or precise synchronization, and if you are not playing audio captured from a network stream, Apple recommends that you use this class for playback. This class is declared in the AVAudioPlayer.h header file in AVFoundation.framework.
Start by error-checking your returns. Is filePath nil? Do either of the AudioServices functions return an error? The most likely cause is case-sensitivity. The iPhone filesystem is case sensitive while the Mac is not. But the first step in debugging is to look at the errors the system is providing.
The simulator uses regular QuickTime for playback, so it's easy to have media assets which work in the sim, but fail on the device due to missing / unsupported codecs. The test is if you can play the file at all on the device, eg through Safari or the iPod app.
Is there a way to programmatically invoke the keypad "click" sound? My app has a custom keypad (built out of UIButtons) and I'd like to provide some audio feedback when the user taps on the keys. I tried creating my own sounds in Garageband, but wasn't happy with any of my creations. If there isn't a standard way to invoke the key click, can anyone point me to a library of sounds that might have such a gem?
There is a really fast solution to play the default keyboard sound:
Add AudioToolbox.framework
Add the following line wherever you want the sound to play:
AudioServicesPlaySystemSound(0x450);
As of iOS 4.2, adopt the UIInputViewAudioFeedback protocol on a custom subclass of UIView. Make this view your "inputView" and then call "playInputClick" at the appropriate time.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIInputViewAudioFeedback_ProtocolReference/Reference/Reference.html
Just to save some people time. Put this in your custom view:
- (BOOL) enableInputClicksWhenVisible {
return YES;
}
To make the click do this:
[[UIDevice currentDevice] playInputClick];
No need to copy the file into your own app - you should be able to get it directly from the UIKit framework:
CFURLRef soundFileURLRef = CFBundleCopyResourceURL(
CFBundleGetBundleWithIdentifier(CFSTR("com.apple.UIKit")),
CFSTR ("Tock"),CFSTR ("aiff"),NULL);
This is what I made out of it aSquared's comment:
NSString *path = [[NSBundle bundleWithIdentifier:#"com.apple.UIKit"] pathForResource:#"Tock" ofType:#"aiff"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);
The simplest way I've found is to extract Tock.aiff (the keyboard sound) from the iPhone Simulator and package it with your app, then play it using AudioServicesPlaySystemSound() at the appropriate time. On my machine, simply typing Tock.aiff into Spotlight turns up the file, but if you have to go looking for it, it's in the simulator version of UIKit.framework.
Using 0x450 as the SystemSoundID works for me (and at the correct volume - just playing the built-in Tock.aiff was too loud). No idea how portable that is - this is on an iPod Touch 3rd gen.
Still doesn't respect the preference for tick on/off.
Here's what I did:
Locate 'Tock.aiff' in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/System/Library/Frameworks/UIKit.framework
Drag it into your Resources folder in xCode, ticking 'Copy items into destination group's folder'
Import AVFoundation.framework into the Frameworks folder in xCode
Import AVFoundation at the top of your class:
#import <AVFoundation/AVAudioPlayer.h>
Use the following function:
- (void)PlayClick {
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"Tock"
ofType:#"aiff"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click setVolume:0.15f];
[click play];
}
That's it!
From what I can tell, the click sound isn't available to apps. I haven't seen anything in audio session services that is relevant. AudioServicesPlaySystemSound() looks promising, but there doesn't appear to be any system sound ID for the click sound (a closer look at the headers may turn up something). You could always loop over a call to AudioServicesPlaySystemSound(i) and see if anything plays. The iPhone software restore images probably have the sound, but it's probably not licensed for general use. Jailbreaking an iPhone to get at the tasty click sound doesn't need to be mentioned.
For (creative commons) sounds, check out the Freesound Project.
For the future, perhaps request that Apple expose system sounds other than the alert sound for use with AudioServicesPlaySystemSound().
Maybe a bit late ...
But in MrMage last post, if you do AudioServicesDisposeSystemSoundID(soundID); straight after AudioServicesPlaySystemSound(soundID);
then you won't hear a thing as you're discarding the system sound right after creating it.
You have to let it finish playing first.. Only call AudioServicesDisposeSystemSoundID to cancel the sound before it finishes
You do not have to dispose of the sound object right away.
Keep a pointer to that sound object in a property, and dispose of it only when you are about to play another sound before re-creating it.
And of course finally dispose of the SystemSoundID object in dealloc.