UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SimplyCoreAudio - Werd behavior when Airpods Connected

Forums > SwiftUI

@Zenki  

Hi there, second topic here. I'm using SimplyCoreAudio from Github to animate volume. My Stack looks like:

ZStack{
      Text("Some Text Here")
            .font(.custom("Impact", size: isMuted ? 50 : 90).width(.compressed))
            .fontWeight(.black)
}

and i'm listening for output device changes like this:

.onReceive(NotificationCenter.default.publisher(for: .defaultSystemOutputDeviceChanged)){ _ in
            if let outputDevice = simplyCA.defaultOutputDevice {
                if outputDevice.name.contains("AirPods") {
                    isAirPodsConnected = true
                } else {
                    isAirPodsConnected = false
                }
            }
        }

If "isAirPodsConnected" (Airpods not connected) is false, no problem. The problem is when i connect my Airpods, the isMuted variable is triggered and stay as true (still able to hear sound). Only when Airpods are connected!! This make the text appear small, big when the volume change but goes back to small after no time... and if really muted, the text appear as big... it works the reverse way...

.onReceive(NotificationCenter.default.publisher(for: .deviceMuteDidChange)){ _ in
          isMuted.toggle()
  }

If i delete isMuted.toggle(), problem solved, but no animation...

Why the isMuted variable is "linked" to Airpods? Am i doing something wrong or missing something? Can i have some help please?

Thanks.

Here's my full code:

import SwiftUI
import SimplyCoreAudio

struct SliderView: View {
    @State var isAirPodsConnected = VolumeWindow.isAirPodsConnected
    @State private var outputVolume: Float = 0.0
    @State private var isMuted = false
    @State private var timer: Timer?

    var body: some View {
        ZStack{
            Text("Some Text Here")
                .font(.custom("Impact", size: isMuted ? 50 : 90).width(.compressed))
                .fontWeight(.black)
        }
        .frame(width: 180, height: 100)
        .onReceive(NotificationCenter.default.publisher(for: .defaultSystemOutputDeviceChanged)){ _ in
            if let outputDevice = simplyCA.defaultOutputDevice {
                if outputDevice.name.contains("AirPods") {
                    isAirPodsConnected = true
                } else {
                    isAirPodsConnected = false
                }
            }
        }
        .onReceive(NotificationCenter.default.publisher(for: .deviceVolumeDidChange)){ _ in
            isMuted = false
            if let currentVolume = simplyCA.defaultOutputDevice?.virtualMainVolume(scope: .output) {
                self.outputVolume = currentVolume
                if self.outputVolume == 0 {
                    isMuted = true
                } else {
                    isMuted = false
                }
            }
        }
        .onReceive(NotificationCenter.default.publisher(for: .deviceMuteDidChange)){ _ in
                isMuted.toggle()
        }
    }
}

   

@Zenki  

If this can help, i have this error messages in the console when i connect my Airpods:

HALCProxyObjectMap.cpp:153    HALCProxyObjectMap::_CopyObjectByObjectID: failed to create the local object
HALC_ShellDevice.cpp:2606   HALC_ShellDevice::RebuildControlList: couldn't find the control object

   

You might join the Core Audio listserv hosted by Apple and post your question there. However, the group has low activity, I would not bet on anyone being familiar with SimplyCoreAudio or AirPod interaction.

lists.apple.com/mailman/listinfo/coreaudio-api

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.