iOS can't play audio while recording - ios

I'm working on the iOS AurioTouch2 demo. I wanna play a song while processing audio data. Here is the playing-audio code.
NSString *path = [[NSBundle mainBundle] pathForResource:#"FoldBeep" ofType:#"wav"];
SystemSoundID theSoundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &theSoundID);
I tried. It don't work. Can someone help me?

Related

App Audio Not Playing

So in my file I have the following code with the intention that a music file plays in the background :
-(void) BackGroundMusic {
NSString *soundPath = [[NSBundle mainBundle]pathForResource:#"LostInTheSea" ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound(soundID);
}
And then in the view did load section I have :
[self BackGroundMusic];
I have all the toolboxes added such as AVFoundation and AudioToolbox but i can not hear any audio playing . Please help !
You should not be using system sound to play mp3 files as user523234 explains.
Use AVAudioPlayer instead. Here's an example that will loop infinitely:
#import "AVFoundation/AVAudioPlayer.h"
#import "AVFoundation/AVAudioSession.h"
-(void)playbgMusic {
AVAudioPlayer *soundbgMusic;
NSString *soundPath;
soundPath= [[NSBundle mainBundle] pathForResource:#"bgMusic" ofType:#"mp3"];
soundbgMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
soundbgMusic.volume = 1.0;
soundbgMusic.numberOfLoops = -1;
[soundbgMusic prepareToPlay];
soundbgMusic.currentTime = 0.0;
[soundbgMusic play];
}
According to the document for AudioServicesPlaySystemSound:
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

iOS - adding a sound on demand in iOS application

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

Can't play lock.aiff inside SpringBoard.app

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

iOS Audio Files not playing

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).

How to manage UIButton click sound iOS

I am using the following code to get a sound on button click.And it works perfect. The problem is this sound is not getting reduced if i reduce the volume on the phone. I want to manage button click sound as per the volume button. I am not using input view.
SystemSoundID soundID;
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound1" ofType:#"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID ((CFURLRef) CFBridgingRetain(soundUrl) , &soundID);
AudioServicesPlaySystemSound(soundID);
As Duncan C suggests, try AVAudioPlayer:
Import AVFoundation.framework.
Replace your code snippet with:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound1" ofType:#"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[player play];
What I do is to create an AVAudioPlayer for my button sound and play that. AVAudioPlayer honors the system volume.
I've created a separate IBAction method that plays the sound, and then I link my buttons to both actions. That way it's easy to turn the sound on and off simply by linking the action. If you always want sounds to play then you might want to put a call to a sound play method directly in your IBAction methods.
Check Click here that might help you.

Resources