Hello,
I hope this message finds you well. I am currently working on a Unity-based iOS application that requires continuous microphone input while also producing sound outputs. For this we need to use iOS echo cancellation, so some sounds need to be played via the iOS layer w/ echo cancellation, I am manually setting up the Audio Session after the app starts. Using the .playAndRecord mode of AVAudioSession. However, I am facing an issue where the volume of the sound output is inconsistent across different iOS devices and scenarios.
The process is quite simple, for each AudioClip we are about to play via unity, we copy the buffer data to our iOS Swift layer, which then does all the processing then plays the audio via the native layer.
Here are the specific issues I am encountering:
The volume level for the game sound effects fluctuate between a normal audible volume and a very low volume.
The sound output behaves differently depending on whether the app is launched with the device at full volume or on mute, and if the app is put into background and in foreground afterwards.
The volume inconsistency affects my game negatively, as it is very hard to hear some audios, regardless of the device or its initial volume state. I have followed the basic setup for AVAudioSession as per the documentation, but the inconsistencies persist.
I'm also aware that Unity uses FMOD to set up the audio routing in iOS, we configure our custom routing after that.
We tried tweaking the output volume prior to playing an audio so there isn't much discrepancy, this seems to align the output volume, however there is still some places where the volume is super low, I've looked into the waveforms in Unity and they all seem consistent, there is no reason why the volume would take a dip.
private var audioPlayer = AVAudioPlayerNode()
@objc public func Play() {
audioPlayer.volume = AVAudioSession.sharedInstance().outputVolume * 0.25
audioPlayer.play()
}
We also explored changing the audio session options to see if we had any luck but unfortunately nothing has changed.
private func ConfigAudioSession() {
let audioSession = AVAudioSession.sharedInstance();
do {
try audioSession.setCategory(.playAndRecord, options: [.mixWithOthers, .allowBluetooth, .defaultToSpeaker]);
try audioSession.setMode(.spokenAudio)
try audioSession.setActive(true);
}
catch {
//Treat error
}
}
Could anyone provide guidance or suggest best practices to ensure a stable and consistent volume output in this scenario? Any advice on this issue would be greatly appreciated.
Thank you in advance for your help!
Audio
RSS for tagIntegrate music and other audio content into your apps.
Posts under Audio tag
68 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
I have an iPad Pro 12.9". I am looking to make an app which can take a simultaneous audio recording from two different microphones at the same time. I want to be able to specify which of the 5 built-in microphones each audio stream should use - ideally one should be from the microphone on the left side of the iPad, and the other should be from one of the mics at the top of the iPad. Is this possible to achieve with the API?
The end goal here is to be able to use the two audio streams and do some DSP on the recordings to determine the approximate direction a particular sound comes from.
In my iOS app, I've the functionality to record audio and video using the AVFoundation framework. While audio recording works smoothly on some devices, such as iPads and certain others, I'm encountering issues with newer models like iPhone 14, 14 Pro, and 15 series. Specifically, when attempting to initiate audio recording by tapping the microphone icon, the interface becomes unresponsive and remains static.
This issue surfaced following an update to iOS 17.2.1. It seems to affect only a subset of devices, despite video recording functioning correctly across all devices.
Just installed iOS 18 Beta 3.
I am seeing my AccessibilityUIServer using the microphone and this is causing no notification sounds, inability to use Siri by voice and volume is grayed out.
If I start to play anything with sound AccessibilityUIServer releases the microphone and I am able to use the app.
Calls still work since AccessibilityUIServer will release and the phone will ring.
Feed back ID is FB14241838.
I have a new iPhone 15 PRO and some new Usb-C Earphones too, both are 3 days old. Since the first day I have been having this Error 1852797029. I can be listening to music on Apple Music for a while but when I stop it and a while passes without resuming playback, when I resume it it gives me this error and I have to close the App and disconnecting and connecting the earphones again. It's very annoying and I'm very angry that this is happening to me from day one. With both devices completely new. Does anyone have a solution other than connecting and disconnecting the earphones?
I have an application which is based on a video streaming service. A critical thing about this app is that these videos must be secured all the way and can't be pirated. The current problem I have is that anyone can use voice recorder to capture my videos audio which isn't suitable this app purpose.
My question is
Is there anyway that I can disable using any voice recording apps (or the microphone) and detect if someone tried to while streaming videos from my app?
Thanks in advance
I'm trying to capture audio samples from the selected output device on macOS using ScreenCaptureKit?
Thank you
hello all!
I'm setting up a really simple media player in my swiftui app.
the code is the following:
import AVFoundation
import MediaPlayer
class AudioPlayerProvider {
private var player: AVPlayer
init() {
self.player = AVPlayer()
self.player.automaticallyWaitsToMinimizeStalling = false
self.setupAudioSession()
self.setupRemoteCommandCenter()
}
private func setupAudioSession() {
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print("Failed to set up audio session: \(error.localizedDescription)")
}
}
private func setupRemoteCommandCenter() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.addTarget { [weak self] _ in
guard let self = self else { return .commandFailed }
self.play()
return .success
}
commandCenter.pauseCommand.addTarget { [weak self] _ in
guard let self = self else { return .commandFailed }
self.pause()
return .success
}
}
func loadAudio(from urlString: String) {
guard let url = URL(string: urlString) else { return }
let asset = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
self.player.pause()
self.player.replaceCurrentItem(with: playerItem)
NotificationCenter.default.addObserver(self, selector: #selector(self.streamFinished), name: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem)
}
func setMetadata(title: String, artist: String, duration: Double) {
var nowPlayingInfo = [
MPMediaItemPropertyTitle: title,
MPMediaItemPropertyArtist: artist,
MPMediaItemPropertyPlaybackDuration: duration,
MPNowPlayingInfoPropertyPlaybackRate: 1.0,
] as [String: Any]
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
@objc
private func streamFinished() {
self.player.seek(to: .zero)
try? AVAudioSession.sharedInstance().setActive(false)
MPNowPlayingInfoCenter.default().playbackState = .stopped
}
func play() {
MPNowPlayingInfoCenter.default().playbackState = .playing
self.player.play()
}
func pause() {
MPNowPlayingInfoCenter.default().playbackState = .paused
self.player.pause()
}
}
pretty scholastic.
The code works when called on views. It also shows up within the lock screen / dynamic island (when in background), but here lies the problems:
The play/pause button do not appear neither in the Command Center nor in the dynamic island. If I tap on the position these button should show up, the command works. Just the icons are not appearing.
the waveform animation does not animate when playing.
Many audio apps are working just fine so is my code lacking something. But I don't know why.
What is missing?
Thanks in advance!
Hi,
I would like to stream live audio (hls) from watch itself, our app can start stream on paired device, but when phone is not near by i want to start streaming on watchos (just like spotify or Apple music app) I watched the 2019 wwdc video about streaming and also looked to the documentation
Documentation : https://developer.apple.com/documentation/watchkit/storyboard_support/playing_background_audio
I can present the route controller to select output, but for example after selecting Air Podcas, stream did not start..
Here is the code: (I have enabled background mode audio)
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, policy: .longFormAudio, options: [])
}
catch {
print("no audiosession!")
}
AVAudioSession.sharedInstance().activate(options: []) { success, error in
dump(success)
dump(error)
DispatchQueue.main.async {
if let streamURL = moduleItem.media?[0] as? String {
dump(streamURL)
let asset = AVURLAsset(url: URL(string: streamURL)!, options: nil)
let item = AVPlayerItem(asset: asset)
let player = AVQueuePlayer(playerItem: item)
player.play()
}
}
}
Hi,
So recently I've been itching to get some actual work done and create something that is genuinely useful to me. Mainly, I want to practice my skills and hopefully build up some new ones.
My concept/idea is to create something that resembles spatial audio ( or spatialized stereo ), but on steroids for macOS. I want to create something that has much more customizability, but also ease of use. I'd like to make use of the power in products like AirPods Pro and create a more immersive audio experience. However, before I make any serious plans, I'd like to ask for some feedback, really just to consider the feasibility of something like this. I throughly enjoy a rich audio experience, but I feel the experience as is on macOS is quite lackluster, especially compared to iOS with system wide spatialized stereo already available.
I would genuinely appreciate any feedback or support that could be given on this project. Thank you for your time.
I also apologize if this is the wrong usage of this forum/support.
Does anyone have a ready-made script/shortcut like the one shown in the video?
Hi, does anyone know how to capture audio input in vision os? I tried the sample code from official examples https://developer.apple.com/documentation/avfoundation/avcapturesession , but it did work.
Has anyone found a way to retrieve user token if I want to make an app on Flutter that uses that to make Apple Music API calls to just retrieve the user data and display it on the application?
Something like when a user opens their app, there is a button that says connect with apple. It takes the user to give permissions for apple music and that retrieves their user token.
I know there is Music Kit on Swift but i wonder if there is something like that on flutter
https://developer.apple.com/videos/play/wwdc2023/10235/ - In this WWDC session,
at 3:19 - Apple has introduced **Other audio ducking ** feature
In iOS17, we can control the amount of 'other audio' ducking through the AVAudioEngine. Is this also possible on AVAudioSession ?
We are using an AVAudioSession for a VOIP call while concurrently attempting to play a video through an AVPlayer. However, the volume of the AVPlayer is considerably low.
Does anyone have any ideas on how to achieve the level of control that AVAudioEngine offers?
I am trying to create an app for a custom Now Playing UI. How can I grab the following:
Song Name
Album Name
Artist Name
Album Art
I'm attempting to record from a device's microphone (under iOS) using AVAudioRecorder. The examples are all quite simple, and I'm following the same method. But I'm getting error messages on attempts to record, and the resulting M4A file (after several seconds of recording) is only 552 bytes long and won't load. Here's the recorder usage:
func startRecording()
{
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 22050,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do
{
recorder = try AVAudioRecorder(url: tempFileURL(), settings: settings)
recorder?.delegate = self
recorder!.record()
recording = true
}
catch
{
recording = false
recordingFinished(success: false)
}
}
The immediate sign of trouble appears to be the following, in the console. Note the 0 bits per channel and irrelevant 8K sample rate:
AudioQueueObject.cpp:1580 BuildConverter: AudioConverterNew returned -50 from: 0 ch, 8000 Hz, .... (0x00000000) 0 bits/channel, 0 bytes/packet, 0 frames/packet, 0 bytes/frame to: 1 ch, 8000 Hz, Int16
A subsequent attempt to load the file into AVAudioPlayer results in:
MP4_BoxParser.cpp:1089 DataSource read failed MP4AudioFile.cpp:4365 MP4Parser_PacketProvider->GetASBD() failed AudioFileObject.cpp:105 OpenFromDataSource failed AudioFileObject.cpp:80 Open failed
But that's not surprising given that it's only 500+ bytes and we had the earlier error. Anybody have an idea here? Every example on the Web shows essentially this exact method.
I've also tried constructing the recorder with
let audioFormat = AVAudioFormat.init(standardFormatWithSampleRate: 44100, channels: 1)
if audioFormat == nil
{
print("Audio format failed.")
}
else
{
do
{
recorder = try AVAudioRecorder(url: tempFileURL(), format: audioFormat!)
...
with mostly the same result. In that case the instantiation error message was the following, which at least mentions the requested sample rate:
AudioQueueObject.cpp:1580 BuildConverter: AudioConverterNew returned -50 from: 0 ch, 44100 Hz, .... (0x00000000) 0 bits/channel, 0 bytes/packet, 0 frames/packet, 0 bytes/frame to: 1 ch, 44100 Hz, Int32
I first encountered this issue on my Spotify web app on 6th March 2024 where the song will restart and/or jumps to another song within the playlist, plays for a bit and occasionally restarts a number of times.
I will never know when Spotify will restart/jumps the song. No issues on Apple Music and on my iPhone & Apple Watch & then it started happening today 21 March 2024.
I tried Googling but to no avail and exhausted all solutions with Spotify's care team (re-installing, clearing the app and macbook's cache, host files & etc, restarting my devices).
I assume that it's now Apple's software issue with macOS Sonoma? Please help, anyone / Apple!!!
Details:
The spotify version i'm having is 1.2.33.1039.g8ddb5918
Macbook Air M2 2022 with macOS Sonoma 14.4
iPhone 14 Pro
I would like to detect audio input device state change in system logs. Right now i can detect the activation using:
log show
--info
--predicate
"process == 'coreaudiod' && category == 'access'".
But i'm unable to detect deactivation and have no idea which predicate to use.
Some of installers which we have suddenly become broken for users running the latest version of OS X, I found that the reason was that we install Core Audio HAL driver and because I wanted to avoid system reboot I relaunched coreaudio daemon via from a pkg post-install script.
sudo launchctl kickstart -kp system/com.apple.audio.coreaudiod
So with the OS update the command fails, if a computer has SIP enabled (what is the default).
sudo launchctl kickstart -kp system/com.apple.audio.coreaudiod
Password:
Could not kickstart service "com.apple.audio.coreaudiod": 1: Operation not permitted
It would be super nice if either the change can be:
reverted OR
I and similar people to know a workaround of how to hot-plug (and unplug) such a HAL driver.
We have an angular/ionic based app that has an audio playback feature. It appears that iphone (but not ipad) users who upgrade to iOS 17.4 can no longer play audio in our app. iPad users who upgraded to 17.4 don't have an issue.
We use the HTMLAudioElement for audio playback. It appears that in 17.4 it is no longer firing the 'canplay' event that we listening for, for starting our playback. The other data buffering events like 'loadeddata' are also not being delivered. By changing the logic to listen for the 'loadstart' event, audio playback works and then the remaining 'canplaythrough' and 'canplay' are delivered. In other words, I need to start playback before any data buffering status events are delivered, otherwise they never get delivered. I am testing this against an audio delivery server on my same machine and have confirmed that the data is correctly delivered.
Is anyone else experiencing a similar issue on iphones in iOS 17.4?