Recording audio and playing iPod at the same time? - iphone

I am trying to be able to record audio through the iPhone's microphone while simultaneously playing music from the iPod.
The recording and playback work separately, but if I try to playback from the iPod and record at the same time the recording stops.
By the way, I am not actually recording the audio to save the sound file, but merely analyzing it. It's for my app that flashes to the beat of the music you play.
Does Apple just not allow record and playback at the same time?

You should be able to do this by setting the Audio Session to AVAudioSessionCategoryPlayAndRecord. Information on this is in the Apple docs, although admittedly the Core Audio documentation is (and has always been) lacking in clarity.
AVSession Class Reference
Also, it's worth noting that AVAudioRecorder and AVAudioPlayer automatically set the session for you. You can wrestle control and set the session manually using the key I highlighted above.

the problem may be you are recording audio to the same path where the playing file is saved.
try to play an audio from a url with AVAudioPlayer and you can record with AVAudioRecorder at the same time.
Cheers :) :)

Related

How to increase volume of sound recorded using AVAudioRecorder

I am using the link mentioned below to record audio through my iPhone app:
How do I record audio on iPhone with AVAudioRecorder?
When I try to play my recordings using AVAudioPlayer on my device, I have seen that the sound volume of my recordings is very low even when my device volume is full.
Recording is working fine.
What can be done to increase the volume?
When recording audio, set the audio session to AVAudioSessionCategoryPlayAndRecord or just AVAudioSessionCategoryRecord. When you're done, set it back to just AVAudioSessionCategoryPlayback.
Solution for iOS 12 + Swift 5.0:
When starting recording use:
recordingSession.setCategory(.record) // recordingSession is of type AVAudioSession
When playing the record audio use:
recordingSession.setCategory(.playback)
Note: Place the statements into a do-catch block.
you can try AVaudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker)
this is because your output device set to inbuilt speaker in iPhone.
while iTab have only speaker so no problem occurs.

AVAudioRecorder

I have been working on an Audio Application where a user can record and play the recorded audio file. In my app during recording a user can pause the recording and play it to listen what he has recorded and then again he can continue the recording.
So now my question is that if a user have recorded a 20 seconds audio and when he plays the audio and wants to resume recording from over 10 seconds overlapping/discarding the next 10 seconds audio. ?
I have looked into the AVAudioRecorder documentation and there is not property to set the duration of the recorder to start recording from specific time. So please someone tell me if it is possible or not. ?
Thanks
Maybe there’s an easier solution, but you may want to look at AVMutableComposition. You can easily create an empty mutable composition, insert the recorded sound into an audio track and then edit the track as needed – delete segments, add another recording, etc.

Recording and then Playing Audio using AVAudioRecorder and AVAudioPLayer

I have an AVAudioRecorder that captures the sound. What I want is to be able to Play(AVAudioPlayer) the recorded(AVAudioRecorder) sound as it is being recorded.
I have set up the AVAudioSession property to playandRecord... but I dont know how do I program the following sort of scenario?
Like you have your head phones on and you are walking,some one beeps at you, but u can't listen because of your headphones on, but what if your iPhone captures that beep and plays right away.
If i am recording and playing on the same file at the same time. Will that be a problem?
Any help regarding this would be anticipated.
No there is no problem in that. recording and playing on the same file at the same time

how can we play audio and video together in ipod/iphone?

HI all
i am playing a video(.mp4 format) without sound (i mean it doesn't have sound it is a mute video ) and in the background i am playing an audio file (.mp3 format) when i play my code through simulator it works fine as i want like when i tap on the video it is just mute but behind i am playing the audio so for user it seems that video has this sound but when i installed my code in device and play video than it doesn't work like so it play video but without sound than how can i play an audio and a video together in the above format ?
actually we are not just playing a single video or audio file it just comes from an array by choosing randomly and same for the audio file so we cann't do this i think so any other idea for it ??
Should we use another format for audio aur video for doing this thing ??
thanks for the help
Balraj verma
The problem is that the default Audio Session does not allow audio mixing.
In the Reference Library (Working with Movie Players) they say you should use a mixable category configuration for your audio session, for example the Ambient category. In the Application Delegate:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryAmbient error: &setCategoryError];
Apple's documentation states that some audio formats are not suited to be played back simultaneously because of hardware restrictions. See Playing Multiple Sounds Simultaneously for more details.
A solution for you would be to use PCM encoded audio which should allow simultaneous playback.
I would like to add that I managed to play two mp3 audio files at the same time on 3G and 3GS iPhones. Which shouldn't be possible according to documentation but worked for me.
You can rather use the an instance of AvAudioPlayer in a new thread. Use the following link to see how does this work
http://www.mobileorchard.com/easy-audio-playback-with-avaudioplayer/
create an instance of MPMoviePlayer to start playback of videos.
Hope this will work.
You should combine the audio and video using some video editing software like iMovie or Windows Movie Maker. Using the software to "composite" audio and video together is not a good idea and adds additional overhead, synchronization issues, etc.
As far as I know, you can't play AVAudioPlayer and MPMoviePlayer at the same time.

Using AVAudioPlayer and MPMoviePlayerController simultaneously

I need to use AVAudioPlayer and MPMoviePlayerController simultaneously i.e play a movie while the background loop is playing is it possible.
My bg music loop stops working when a movie starts playing , so I tried to stop the bg music loop and start the movie and when movie stops, start playing the bg loop again but this is also is not working.
In fact it is achievable from 3.x onwards. I had once done this in one of my applications and its got nothing to do with any backgrounding or any other new iOS features. You just need to configure the audio sessions properly. If you make sure that both your audio player as well as the video player are using the ambient audio session it is possible.
Prior to starting your AVAudioPlayer to play, set the category of audio session to AVAudioSessionCategoryAmbient. This is the system wide common audio session. This allows other sounds to play along such as the sound from the iPod application.
In case you are using the MPMoviePlayerController from iOS 3.2 or above make sure you set the useApplicationAudioSession to NO. This ensures other sounds to play along. It is the default in earlier versions of iOS.
There is a limitation in the current iPhone SDK (2.x and 3.x) that does not allow any background audio playing while a movie is playing in the MPMoviePlayerController API. Unfortunately, it is not possible to do what you are asking at this time within the iPhone SDK.
I would suggest logging an enhancement request to Apple to offer this in a future version of the SDK.