Hi
I am developing an app which plays spoken audio. I experience problems when I want to play audio over the car system with Bluetooth. The Bluetooth connection is fine and other audio apps (e.g. Spotify) just work fine. When I want to play audio with my app, I can start audio playback successfully but I hear no sound. It works with the device speakers and interestingly even with another mobile Bluetooth sound box. Both the car system and the mobile soundbox use A2DP.
Below you can find the relevant method that starts the audio playback. The full app can be found at https://gitlab.com/spiri-voyage/spiri-test.
func play() {
if playbackState == .Stopped {
let url = Bundle.main.path(forResource: "Kolinbrunnen", ofType:
"mp3")
let item = AVPlayerItem(url: URL(fileURLWithPath: url!))
player.replaceCurrentItem(with: item)
let audioSession = AVAudioSession.sharedInstance()
do
{
try audioSession.setCategory(.playback, mode: .spokenAudio)
addInfo("Audio session category configured for foreground.")
try audioSession.setActive(true)
addInfo("Audio session activated.")
player.play()
addInfo("Playing audio.")
playbackState = .Playing
}
catch {
addInfo("Failed to play audio: error setting category or activating audio
session.")
}
}
else if playbackState == .Paused {
player.play()
addInfo("Audio resumed.")
playbackState = .Playing
}
else {
addInfo("Ignoring play command: audio already playing.")
}
}
Is the audio session handling wrong? Do I not use the AVPlayer correctly? I read the documentation many times, I don't know what else to do.
Tested with iPhone SE, software version 14.4.2.
Any help is greatly appreciated.
Kind regards
Rony
Post
Replies
Boosts
Views
Activity
My app is programmed to play audio in the background. A user can for example listen to music with Spotify while my app is running in the background. When entering a certain region, my app starts playing audio and ducks Spotify. This worked fine until I tried the following scenario:
Connect iPhone to car audio system with bluetooth
Listen to Spotify
My app is running in the background
My app downloads the audio with 4G from the internet
I experienced the following:
Streaming of Spotify music works fine
When my app enters the region, the audio starts but stops after a couple of seconds.
After that, audio seems to be messed up. Sometimes I can resume it, sometimes not, also the other app's audio sometimes does not play anymore.
I experienced this in 3 different cars. When my app is the only app playing audio, it works fine. When connecting my app with USB or AUX it works fine in all cases (my app in foreground/background and other audio playing).
Any help would be greatly appreciated. Thanks in advance.
Best regards
Rony
Currently, region monitoring only allows to define circular regions. Does Apple have any plans to support more complex geometries like polygons in the future? Or does it make more sense to put that logic into the cloud and use a service like Radar.io? The advantages of using a cloud service is having the logic in one central place and battery consumption is lower. What is Apple's view on this?
Thank you very much.
Kind regards
Rony
I am building a iOS app with Ionic and Capacitor. I use AVFoundation inside a Capacitor plugin which provides a public method to play an audio file accessible at a remote URL. The basics work, the audio file is successfully played. Now I want to correctly handle interruptions and background audio. By background audio I mean start playing an audio file while the app is in the background and not continue playing it in the background when it was started in foreground before. To summarize, the app should behave the following way:
If another app (e.g. Spotify) is playing audio and my app wants to play audio, the other app should be interrupted and my app's audio should start playing without mixing.
If my app's audio is finished playing, the other app's audio should resume.
My app must be able to start playing audio without mixing when it is in the background.
I want to show a play/pause button and the audio title on the lockscreen and in the notification center.
I added the background mode audio capability. I use AVPlayer, AVAudioSession, MPNowPlayingInfoCenter and MPRemoteCommandCenter, followed best practices, especially regarding activation and deactivation of an audio session and interruption handling. I tried several solutions, but never managed to achieve all of the above goals. What I found out so far is the following:
Goal 1. above is easy to achieve. Simply activate an audio session without mixing and start playing the audio.
After the audio of my app is finished, I deactivate the audio session and use the option notifyOthersOnDeactivation. Unfortunately, I can only achieve goal 2. above if I don't use remote commands nor notification info. This means, my app then has no audio control UI on the lockscreen nor in the notification center. Somehow the configurations of MPNowPlayingInfoCenter and MPRemoteCommandCenter seem to affect the interruption behavior. As far as I could see, Apple documentation doesn't say anything about this.
I only manage to start playing audio in the background when setting the option mixWithOthers. Apple documentation doesn't say anything about this, either.
Any help would be greatly appreciated.
Rony
According to the documentation - https://developer.apple.com/documentation/avfoundation/avaudiosession/1616627-setactive of the method setActive of AVAudioSession, the method declaration says the method returns no value, but further down in the documentation it says it returns a value. Xcode says, setActive returns no value. Is the documentation outdated?