Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Volume tracker #1116

Open
2 of 3 tasks
defagos opened this issue Jan 15, 2025 · 0 comments · May be fixed by #1118
Open
2 of 3 tasks

Volume tracker #1116

defagos opened this issue Jan 15, 2025 · 0 comments · May be fixed by #1118
Assignees
Labels
enhancement New feature or request

Comments

@defagos
Copy link
Member

defagos commented Jan 15, 2025

As a developer integrating Pillarbox I would like to be able to integrate custom volume controls.

Hints

MPVolumeView is the only API that makes it possible to change the system volume but offers limited customization. It can be almost be turned into a programmatic API with the following trick:

import SwiftUI
import Combine
import MediaPlayer

final class VolumeManager: ObservableObject {
    let volumeView = MPVolumeView()

    private weak var volumeSlider: UISlider? {
        volumeView.subviews.compactMap { $0 as? UISlider }.first
    }

    @Published var volume: Float = 0 {
        didSet {
            volumeSlider?.value = volume
        }
    }

    init() {
        AVAudioSession.sharedInstance().publisher(for: \.outputVolume)
            .assign(to: &$volume)
    }
}

struct ContentView: View {
    @StateObject private var volumeManager = VolumeManager()

    var body: some View {
        Slider(value: $volumeManager.volume)
    }
}

#Preview {
    ContentView()
}

This lets the associated UI be freely customized, but of course there is a private slider access that might be risky.

Note that the initial volume returned by MPVolumeView might not be up to date. For this reason it is better to read the volume via AVAudioSession and to write it with MPVolumeView (since the outputVolume property is read-only).

Acceptance criteria

  • A way to create a custom volume control UI is provided.
  • The demo provides a corresponding slider somewhere.

Tasks

  • Provide component for volume tracking.
  • Extract custom slider construction.
  • Add volume slider with same look & feel as progress slider.
@defagos defagos converted this from a draft issue Jan 15, 2025
@defagos defagos added the enhancement New feature or request label Jan 15, 2025
@defagos defagos changed the title Volume manager Volume tracker Jan 16, 2025
@defagos defagos moved this from 📋 Backlog to 🚧 In Progress in Pillarbox Jan 17, 2025
@defagos defagos assigned defagos, ASKywepT and waliid and unassigned ASKywepT Jan 17, 2025
@waliid waliid linked a pull request Jan 20, 2025 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: 🚧 In Progress
Development

Successfully merging a pull request may close this issue.

3 participants