Playing sound effect just before AVAudioRecorder - ios

I am recording audio with AVAudioRecorder. I want to however play a short sound before the recording starts. I am doing this with AudioServicesCreatSystemSoundId. I notice that I need to pause the thread after calling the sound effect or else it will start playing, then get interrupted by the AVAudioRecord record call and then finish playing after the recording has been stoped again.
So I have something like this:
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
[NSThread sleepForTimeInterval:0.7];
[_audioRecorder record];
The problem is that one notices the delay. I know there are apps out there (like i.e. Voxer) that make the sound without delaying the actual recording. I tried splitting of the first two lines in a block, but that didn't help. How do they do it?
Thanks

For low latency (no noticeable start delay) audio recording, use a pre-started RemoteIO Audio Unit instead of the AVAudioRecorder API.

Related

AVFoundation AVAudioPlayer stop is pausing - Objective-C

It's fairly hard to show code for this as it is not noticeable in the code. I have some actions like -(IBAction)PlaySound1:(id)sender; and -(IBAction)PlaySound2:(id)sender; and audio files. I have the sounds working all fine. I also have -(void)Stop;. When Play sound 1 is pressed it calls the method Stop and stops sound 2. and the same with sound 2.
The problem I am encountering is when you play sound 1 and stop sound 2, when you play sound 2 it starts from where it left off.
Inside my (void) I have [PlaySound1 stop] //and [PlaySound2 stop]
If anyone could help that would be great. If something was unclear, just comment on this and I'll try and explain it better.
Thanks :)
Stopping an AVAudioPlayer does not reset the play head.
The stop method does not reset the value of the currentTime property to 0. In other words, if you call stop during playback and then call play, playback resumes at the point where it left off.
To resume playback from the start set the currentTime property to 0.0 before calling play.
example -
playSound2.currentTime = 0.0;
[playSound2 play];

AVAudioRecorder doesn't append the recording after comes from background

I am using AVAudioRecorder for recording while I am recording I press home button and comes to fore ground and checked the status of the AVAudioRecorder (isRecording) is still YES. But when i play the audio it plays only what i recorded before goes to background.
Note: This problem comes only in ios7. It works well in ios6
Have you set the app to allow background recording? Under the Project Capabilities, turn on Background Modes and select Audio and AirPlay.
Be warned, in iOS7, when background recording through AVAudioRecorder is interrupted, the recorded file is closed instead of paused. If a user get a phone call while recording in the background, the audio file will stop recording and cannot be resumed. You will need to start a new audio file and merge them, or use Audio Queues and manage the interruptions yourself.
See this answer for more info: Audio interruption when iOS application is recording in background

iOS: AVQueuePlayer does not indicate that it has stopped playback

I have an AVQueuePlayer which loads URLs to play audio files and it works well for the most part. However, I have run into a problem where after the player finishes playing a file (with another file in the queue), it will simply stop playing. Normally, the app would be able to use the player's rate, status, and items. In this case, I have gone through with the debugger and everything looks normal.
Everything appears to be playing, except for the player itself. After forcing the player to play, the player will skip to the next track, indicating that the AVPlayerItem it had was not loaded (I can confirm the audio urls are valid).
Does anyone have any ideas how I catch this programatically?
You need to have the delegate method:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
Once this method is finished, start the new audio.

Best way to pause an iOS Audio Stream When Phone Call Comes In

I use an AVPlayerItem to play a stream of music. What is best way to pause the stream when a phone call comes in, and resume it when the call ends?
I think, that you use AVAudioPlayer.
So AVAudioPlayer got methods - play/pause.
Sample code:
AVAudioPlayer *thePlayer;
[thePlayer pause];

iOS: play audio file at a specific time and stop after a specific duration

I have an audio file with length of 500 miniseconds for my app. I want app users to play the audio at minisecond 100 (by pressing a button) and the audio will automatically stops at minisecond 150. This is a code that I have done so far:
AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:filePath]
error:nil] autorelease];
[audioPlayer setCurrentTime:100.00f];
[audioPlayer play];
Would you please help me with the stopping part? Thank you
You could create an instance of NSTimer with the desired play interval, then when the timer fires, stop audio. Create the timer on the main thread, and in a place in your code in an applicable area where you can get a reference for the audioPlayer. Make sure you handle interruptions, IE, User pauses or stops audio somehow, or if the audioPlayer is deallocated somewhere, your app goes into the background, or whatever else you need to handle, by invalidating and disposing of the timer. See the docs for information on NSTimer. http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html

Resources