alert sound when start recording - iphone

First time i am capturing video in my App, but when i start capturing i want that button click sound as native application. I have searched a lot and came to know that it is a system sound. Can anyone please tell me how can i add that sound in my application when i start capturing video and when i stop capturing.
Currently i am using AVAudioPlayer to play sound as shown below
NSURL *url1 = [[NSBundle mainBundle] URLForResource:str withExtension:#"wav"];
_startAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
_startAudioPlayer.delegate= self;
[_startAudioPlayer prepareToPlay];
[_startAudioPlayer play];
Thanks in advance.....

Just import AudioToolbox.framework.
Create the URL for the source audio file
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: #"tap"
withExtension: #"aif"];
// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
soundFileURLRef,
&soundFileObject
);
Play sound
AudioServicesPlayAlertSound (soundFileObject);
Here is a nice tutorial link you can follow this

When you want to start capturing video at that time call this bellow method like this...
[self playSound];
use my this bellow method for play sound...
-(void)playSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"adriantnt_release_click" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
first store any mp3 or any audio file in your project folder For Ex: i add adriantnt_release_click.mp3 file.
Also add AVAudioPlayerDelegate in your .h file and add AudioToolbox.framework in your project..

Related

How do I get sound to play in an animation I created in xcode?

I managed to create this application that includes an animation in it. But I want to make it stand out and more fun by adding a .wav file that I downloaded from the web. I want the sound to play for the duration of the animation. Only started coding a month ago so any help would be deeply appreciated
You could use the AVAudioPlayer
just create a method and connect it to your button or animation
-(void) playLoopingMusicInApplication {
AVAudioPlayer *audioPlayer;
// set the pathForResource: to the name of the sound file, and ofType: to AAC preferrably.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"WAV"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.delegate = self;
//infinite
player.numberOfLoops = -1;
[player play];
//[player release];
}
PS - You will need to get the AVFoundation framework, and the AudioToolbox framework in your project
hope this helps!

button s sound makes button press "delay" with [buttonsound prepareToPlay];

After the app starts, and I press the start button, it s "lagg" but I used this code into viewdidload and viewdidappear too:
gombhang = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"button4" ofType:#"mp3"]];
gombha = [[AVAudioPlayer alloc] initWithContentsOfURL:gombhang error:nil];
[gombha prepareToPlay];
gombha.delegate = self;
How can I fix this lagg? If I can t fix it apple will reject it?
As I know Audioplayer takes few seconds to load the audio file .
NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"button4" ofType:#"wav"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFile]; AudioServicesCreateSystemSoundID((CFURLRef) CFBridgingRetain(soundFileURL), &_MySound); AudioServicesPlaySystemSound(_MySound);
And another way: I made an empty/ muted 0.1 sec sound file, and played in viewdidload, and this method loaded the avaudioplayer before I clicked on the start button, so it doesn t lagg anymore.

Playing sound effect with no latency in Objective-C

I want to play some simple sound effects when people press certain buttons in my app and I've tried several things, but I always get a latency that makes the sound seem out of sync.
I've followed the tutorials here, so I've tried the build in Audio Services, but that had a latency and I've tried the AVAudioPlayer, but that had a latency as well, even though I used "prepareToPlay".
Do I really have to install a big and messy library like Finch to get a simple sound effect with no latency in my simple app?
Hope you can clarify things for me!
UPDATE
Code for Audio Services:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:#"PlopsX4"
withExtension:#"aif"];
AudioServicesCreateSystemSoundID((CFURLRef)soundURL, &sound1);
AudioServicesPlaySystemSound(sound1);
Code for AVAudioPlayer in viewDidLoad:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:#"PlopsX4"
withExtension:#"aif"];
self.avSound = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundURL error:nil];
[self.avSound prepareToPlay]
Code for AVAudioPlayer in method where I want to play the file:
[self.avSound play];
The way I got this to work (and I think I got the idea from another post on SO) was to create an empty 1 second .wav file and "play" it at initialization. After that, there was no delay when I called the other sound effects.
Something like this in my sound player object
SystemSoundID blID;
SystemSoundID cID;
+(void)doInit
{
if (spinitialized){
AudioServicesPlaySystemSound (blID);
return;
}
NSString *str = [[NSBundle mainBundle] pathForResource:#"ding.wav" ofType:#""];
NSURL *uref = [NSURL fileURLWithPath:str];
OSStatus error = AudioServicesCreateSystemSoundID ((CFURLRef)uref, &cID);
if (error) NSLog(#"SoundPlayer doInit Error is %ld",error);
str = [[NSBundle mainBundle] pathForResource:#"blank.wav" ofType:#""];
uref = [NSURL fileURLWithPath:str];
error = AudioServicesCreateSystemSoundID ((CFURLRef)uref, &blID);
if (error) NSLog(#"SoundPlayer doInit Error is %ld",error);
AudioServicesPlaySystemSound (blID);
spinitialized = YES;
}
Then I could just play the "ding" without any delay using:
AudioServicesPlaySystemSound (cid);
SOLUTION
I ended up doing something similar as Mike above. I ended up playing the sound with the sound volume down in viewDidLoad:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:#"PlopsX4"
withExtension:#"aif"];
self.plops = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
[self.plops setVolume:0.0];
[self.plops play];
And then in the function where I play it I do:
[self.plops setVolume:1.0];
[self.plops play];
One thing I found that seems to help is, upon entry to the screen where the sound MIGHT be played, to play a fraction of a second of a sound. This seems to "prime" the sound mechanism -- allocating internal objects, paging in stuff, etc.
(I observe that this is similar to Mike M's approach.)

Delay in playing sounds using AVAudioPlayer

-(IBAction)playSound{ AVAudioPlayer *myExampleSound;
NSString *myExamplePath = [[NSBundle mainBundle] pathForResource:#"myaudiofile" ofType:#"caf"];
myExampleSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
myExampleSound.delegate = self;
[myExampleSound play];
}
I want to play a beep sound when a button is clicked. I had used the above code. But it is taking some delay in playing the sound.
Anyone please help.
There are two sources of the delay. The first one is bigger and can be eliminated using the prepareToPlay method of AVAudioPlayer. This means you have to declare myExampleSound as a class variable and initialize it some time before you are going to need it (and of course call the prepareToPlay after initialization):
- (void) viewDidLoadOrSomethingLikeThat
{
NSString *myExamplePath = [[NSBundle mainBundle]
pathForResource:#"myaudiofile" ofType:#"caf"];
myExampleSound =[[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:myExamplePath] error:NULL];
myExampleSound.delegate = self;
[myExampleSound prepareToPlay];
}
- (IBAction) playSound {
[myExampleSound play];
}
This should take the lag down to about 20 milliseconds, which is probably fine for your needs. If not, you’ll have to abandon AVAudioPlayer and switch to some other way of playing the sounds (like the Finch sound engine).
See also my own question about lags in AVAudioPlayer.
AudioServicesPlaySystemSound is an option. Tutorial here, sample code here.

How to play audio file on websites on iPhone?

I want to play an audio file which is posted on a website. I'm trying to use AVAudioPlayer and set the url to its website link. But the bgPlayer turns out to be nil. I think I can download this audio file and play it afterwards, but is there any way to play it while I'm downloading it?
NSURL *bgURL = [NSURL URLWithString:#"http://www.radioslots.com/iphone/test/tmp/1.mp3"];
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil];
bgPlayer.numberOfLoops = -1;
if (bgPlayer) {
[bgPlayer play];
}
AVAudioPlayer isn't made to open network streams, I could be mistaken but I'm pretty sure it's not possible.
Just off the top of my head, I can think of two ways that would probably work:
1. Use MPMoviePlayer, as this can open network streams and despite the name works with audio. The only thing is this pops up the modal player with controls.
2. Download the network file and store it locally, then use AVAudioPlayer to play the local file.
I have just written answer here.Check it and replace the url used there with yours..
you just replace
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"songname" ofType:#"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
with
NSString *soundFilePath = #"Your URL";
NSURL *newURL = [[NSURL alloc] initWithString: soundFilePath];
There is a proper documentation available at this link
But I think you should also check the error code you are getting by this way:
NSError *error;
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:&error];
Hope this helps.