Disable audio route changes in an iOS App? - ios

I have an audio app that plays for many hours throughout the night. Sometimes the iPhone changes routes and says the speaker is no longer available and stops playing audio. I'm not sure why but my theory is that it tried to connect to a Bluetooth device. I get the following in the log:
AVAudioSessionRouteChangeReasonCategoryChange
AVAudioSessionRouteChangeReasonOldDeviceUnavailable
And since the old device is no longer available it stops the audio. I'm playing audio through the iPhone speaker, so I'm not sure why the speaker would no longer be be available.
My question is what can I do to prevent this from happening in code? I advise people to turn on Airplane mode or disable Bluetooth which always fixes the issue. I'm wondering if something could be done in code to help solve this problem.

Related

iOS Is it possible to play audio through speakers and headphones at the same time as of iOS 11?

Is it possible to play audio through speakers and headphones at the same time as of iOS 11? As of right now I am trying to find an app on the store that can do this, so far I have had no luck. I have found a few other threads, but they don't seem to be working for me or they say you can't. Here are a few of the links,
https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html
https://stackoverflow.com/a/35009801
https://apple.stackexchange.com/questions/48534/is-it-possible-to-play-sound-through-both-the-headphone-jack-and-my-internal-spe
iOS: Is it possible to send audio out both headphones and speakers at the same time?
Has anyone tried to play sounds through the speakers and headphones at the same time? Android + iOS
What is really strange is that I cannot find any official documentation saying whether this is possible only that 'The built-in speaker may be used only if no other eligible output ports (USB, HDMI, LineOut) are connected.'
If you need code let me know, but really I am just wondering if it is possible and what is the best route for doing this? The key thing is that I want to do it simultaneously. Thanks.
So after some further review, I would like to save everyone the headaches that I had to go through. Is it possible? Probably, however Apple as of right now does not supply any way to do this. There seems to be no official documentation as to if it can be done (please update this answer if you find some).
The best evidence I have to support my answer would that Apple states 'Important: The built-in speaker may be used only if no other eligible output ports (USB, HDMI, LineOut) are connected.' Also in the solutions posted here, neither method works. I have tested both with iPhone 7 on iOS 11.
NOTE* in the second method I think that the writer was confusing 'channels' with 'routes'. He is changing and selecting the channels (this would be like left, right, or middle speaker for devices that support it), whereas I was looking to change the route (play in the speaker, play in the headphones, HDMI, USB, and ect...). So, in my opinion, NO YOU CANNOT play on the headphones and the speaker at simultaneously (different sounds or the same), unless you are Apple or I am wrong.

AudioKit crackling after connecting headphones on iPhone 8+

We have an app, that uses AudioKit to record sound from the microphone and to playback it directly to your headphones.
If you connect your headphones to an iPhone 8+ after you started the app, it starts to crackle at the end of every recorded audioframe. If you do the same thing on every device older than iPhone 7 the app will work correctly.
If you plugged in the headphones before starting the app, it will work correctly in all scenarios, also if you plug in or plug off your headphones.
For better analysis I built a test-app, which only includes the audio-related functions.
This test-app behaves exactly like the other app.
We really have to fix this issue, because we have customers depending on the feature.
Any help in fixing this would be highly appreciated. AudioKit-version is 4.1.
We setup a github repository where you can checkout the code:
https://github.com/eskarion/audio-test
Thanks in advance!

Monitor volume level when using headphones

I'm looking to create an iphone app to help people monitor whether they are experiencing hearing loss from turning up the sound too high on their earbuds for a research project.
To do this, I want to periodically poll the volume level when music is playing on the iphone and the headphones are plugged in.
I did find the following answers that indicate it is possible however I am not familiar with iphone development.
checking when the audio is playing on the phone
Can my app be notified when another application starts/stops playing audio?
check if headphones are plugged in
Are headphones plugged in? iOS7
checking the volume level on the phone
https://stackoverflow.com/a/29170927/1361960
In Apple-way you cannot make app be notified when another application starts/stops playing audio.
Since the background execution of your app was limited according to the document.
Your code will only be executed when your application was running.

Robotic voice through iphone 6s microphone

I am developing an app to do live video streaming and when I recently upgraded to an iPhone 6s, all noise going through the microphone sounds robotic (or like a cricket). I can reproduce this issue only using the skype app, but no other app have this similar output. The skype support forums say that this is a problem they're seeing on iPhone 6s [1] but don't give any details as to what's causing it.
The interesting thing is it doesn't have issues when I use the microphone jack via my headphones, only when the built in microphone is used. Is there a permission or a change I need to make to my app to fix this?
[1] - http://community.skype.com/t5/iOS-iPhone-and-iPad/iPhone-6s-Distorted-sound/td-p/4138308
The 6S is apparently locked to a 48000 sample rate when using the built in microphone. Maybe you tried to set the format to 44100. I think the only way to deal with this is to query an active AVAudioSession for it's sampleRate property and set your format's sample rate to that.
This is a know issue consumer users are seeing. It might be the iOS 9 update was not working properly.
Here's a source (might not be that trusted for some but it's a start)
http://9to5mac.com/2015/09/30/iphone-6s-touch-id-3d-touch-speaker-power-issues/
You can try a different app from the App Store, or call someone on speaker, to check if the issues is reproducible.

Live streaming Audio from iOS

I have tried to live stream audio (AAC-LC) from iOS for 3 months without much success...
I tried Audio Queues, which work well but there is a strange delay (~4s) and I don't know why (high level API ?)
I tried Audio Units, it sometimes works on the simulator but never with the phone using a modified code from this source
I am really lost, can anyone help me ?
EDIT
I have to do a live streaming application (iPhone-> Wowza Server via RTSP). The video part works well with little delay (1s). Now I'm trying to add audio in addition to video but I'm stuck with the SDK.
tldr : I need to capture microphone input then send AAC frames over the network without getting huge delay
This app, which I just now completed, broadcasts audio between any two iOS devices on the same network:
https://drive.google.com/open?id=1tKgVl0X92SYvgpvbljRzilXNQ6iBcjqM
Compile it with the latest beta release of Xcode 9, and run it on two iOS 11 (beta) devices.
The app is simple; you launch it, and then start talking. Everything is automatic, from network connectivity to audio streaming.
Events generated by the app are displayed in an event log in the app:
Even though the code is simple and concise, the event log was provided to make understanding the app's architecture quicker and more easily.

Resources