kAudioSessionCategory_MediaPlayback is silenced with ringer off? - iphone

according to the documentation the correct way to allow sounds to be played even if the ringer switch is set to off is like so:
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
I then throughout the app play short wav files that play perfectly using
AudioServicesPlaySystemSound(sysSound)
....until the ringer is switched off and then.....silence?
No matter what I do I cannot get the sound to play if the ringer is off?
Any one ..... please?

Did you try using AudioSessionSetActive(YES); at the end?

AudioServicesPlaySystemSound never plays while the ringer is switched off in my experience. If you play sounds using AVAudioPlayer instead, you should get the behavior you are looking for:
let player: AVAudioPlayer = try AVAudioPlayer(contentsOf: url)
player.delegate = self
player.play()
You will also need to activate the audio session before playing if you have not already. And set an audio session category that supports sounds while silenced like you already did. Consider stopping the audio session when no audio is playing to conserve battery life.
A notable difference between AudioServices and AVAudioPlayer is that if the ringer is silenced, AudioServices playback will return almost immediately while AVAudioPlayer will take the full length of the sound before completing. As far as I know, the only way to detect and respond to the user's ringer setting is to use AudioServices instead of AVAudioPlayer. If you just want to play while silenced though this should not be relevant.

Related

Record and play louder volume avaudio

I used the tutorial found here to record and playback a sound. The recording and playback work fine, but I can barely here the sound when I play it back. I've tried the following code:
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),&audioRouteOverride);
I add it to my "play" method, right before I play the sound. I've also tried to add it to my viewDidLoad. Both of them make it so I can't here the playback at all. It seems to be working for others, but where should I add it?
I checked out SpeakHere, Apples example AVAudio app. The volume is very quiet. When I use the Voice Memos app, the recording is played back very very loud. Has anyone been able to play a recorded message and play it back that loud?
Try setting this property of AVAudioPlayer ...
#property float volume; /* The volume for the sound. The nominal range is from 0.0 to 1.0. */

How to mute a video on iOS while iPod still playing audio?

I'm developing a teaching app playing video animations with no sound.
I would like to prevent the movie player from stopping other audio sessions (such as the iPod) when the video is launched.
Is there any way to indicate the iOS the video has no audio or play in mute mode?
Thanks in advance for your help!
You can always detect if the music is playing and and resume it right way after the movie started playing.
Detecting player state
[MPMusicPlayerController iPodMusicPlayer] playbackState]
Resume
[[MPMusicPlayerController iPodMusicPlayer] play];
I think maybe something like this
UInt32 allowMixing = YES;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
AudioSessionSetActive(YES);
should do the the trick.
Keep in mind, you'll need to add the AudioToolbox library to your application, and add:
#import <AudioToolbox/AudioToolbox.h>
to your classes imports in order to use the above code.
It should allow your app's sounds to mix with the other app's sounds in the background. In this case, since your app is silent, it will just allow background sounds to keep playing.
Hope it helps!

AVCaptureSession cancels background audio

Whenever I start an AVCaptureSession running with the microphone as an input it cancels whatever background music is currently running (iPod music for instance). If I comment out the line adding the audio input, the background audio continues.
Does anyone know of a way to record video clips with the microphone while continuing to allow background audio to play? I've looked around a lot, and can't seem to find any references to this behavior.
Thanks for any help!
Try setting kAudioSessionProperty_OverrideCategoryMixWithOthers as seen in https://stackoverflow.com/a/7426406/16572
Is the background music a looping track? -- if so you could make it a system sound and tell it to re-play when it finishes playing. The reason being you can only have tracks up to 30 seconds as a system sound.
Don't know if it helps -- but thats one approach :)
Cheers,
Michael
I had the same problem and used this in my app and it worked:
UInt32 audioRouteOverride = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,sizeof (audioRouteOverride),&audioRouteOverride);
I was able to record video and audio and at the same time play a sound out of the speakers.

MPMusicPlayerController doesn't respect device mute switch?

I'm using the MPMusicPlayerController application music player, created like:
appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
The issue is that it will play music no matter which setting the device mute switch is in, it doesn't seem to care either way. Is there some audio session mode I need to be in to have it respect the mute switch?
Yes. You want to use one of the following constants for your Audio Session Category:
kAudioSessionCategory_AmbientSound
kAudioSessionCategory_SoloAmbientSound
These are the ones that go silent when the Ring/Silent switch is set to silent.
Fwiw I'm seeing the same problem. I suspect that, like the iPod app itself, it will never respect the mute switch.
Try using iPodMusicPlayer instead of applicationMusicPlayer.

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.