Skip to content

HeadphoneMotion manages motion-related data from headphones using Apple's CoreMotion API.

License

Notifications You must be signed in to change notification settings

simonbogutzky/HeadphoneMotion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HeadphoneMotion for iOS

HeadphoneMotion manages motion-related data from headphones using Apple's CoreMotion API. It handles the entire lifecycle of motion data collection from headphones, including initiating updates, processing received motion data, and handling connection and disconnection events.

Installing HeadphoneMotion

HeadphoneMotion supports Swift Package Manager

Github Repo

You can pull the HeadphoneMotionManager Github Repo and include the HeadphoneMotionManager.swift in your project.

Swift Package Manager

To install HeadphoneMotion using Swift Package Manager you can follow the tutorial published by Apple using the URL for the HeadphoneMotionManager repo with the current version:

  1. In Xcode, select “File” → “Add Packages...”
  2. Enter https://github.com/simonbogutzky/HeadphoneMotion.git

or you can add the following dependency to your Package.swift:

.package(url: "https://github.com/simonbogutzky/HeadphoneMotion.git", from: "1.0.0")

Integration

Plist

Add Privacy - Motion Usage Description

Service Example

final class HeadphoneMotionService: NSObject, ObservableObject {
    // MARK: - Properties

    private let headphoneMotionManager = HeadphoneMotionManager()
    @Published private(set) var state: State = .disconnected

    // MARK: - Methods

    func pause() {
        headphoneMotionManager.stopDeviceMotionUpdates()
        headphoneMotionManager.delegate = nil
    }

    func resume() {
        headphoneMotionManager.delegate = self
        do {
            try headphoneMotionManager.startDeviceMotionUpdates()
        } catch {
            let headphoneMotionManagerError = error as? HeadphoneMotionManagerError

            switch headphoneMotionManagerError {
            case .authorizationStatusRestricted:
                state = .unauthorized
            case .authorizationStatusDenied:
                state = .unauthorized
            case .authorizationStatusUnknown:
                state = .unauthorized
            case .deviceMotionIsNotAvailable:
                state = .failure(error)
            case .deviceMotionIsActive:
                state = .fetchingFirstDeviceMotion
            case .none:
                state = .failure(NSError(domain: "HeadphoneMotionService", code: -1000))
            }
        }
    }
}

extension HeadphoneMotionService {
    enum State {
        case unauthorized
        case connected
        case disconnected
        case fetchingFirstDeviceMotion
        case latestDeviceMotion(CMDeviceMotion)
        case failure(Error)
    }
}

extension HeadphoneMotionService: HeadphoneMotionManagerDelegate {
    // MARK: - Methods

    func headphoneMotionManagerDidConnect() {
        state = .connected
    }

    func headphoneMotionManagerDidDisconnect() {
        state = .disconnected
    }

    func headphoneMotionManagerDidReceiveDeviceMotion(_ motion: CMDeviceMotion) {
        state = .latestDeviceMotion(motion)
    }

    func headphoneMotionManagerDidFailWithError(_ error: any Error) {
        state = .failure(error)
    }
}

Contributing

I always appreciate contributions from the community.

About

HeadphoneMotion manages motion-related data from headphones using Apple's CoreMotion API.

Topics

Resources

License

Stars

Watchers

Forks

Languages