Skip to content

Volume tracker #1116

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

Closed
3 tasks done
defagos opened this issue Jan 15, 2025 · 1 comment · Fixed by #1118
Closed
3 tasks done

Volume tracker #1116

defagos opened this issue Jan 15, 2025 · 1 comment · 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
@defagos defagos moved this from 🚧 In Progress to 🍿 Code Review in Pillarbox Jan 21, 2025
@defagos
Copy link
Member Author

defagos commented Jan 23, 2025

We considered a few options:

  • Wrapping an MPVolumeView into a SwiftUI view. The problem is that the customization is limited. This probably explains why Apple uses another private volume slider in AVPlayerViewController.
  • Alternatively we planned to introduce a VolumeTracker, an observable object, as a way to build custom volume-related controls. Internally, though, we had to use a dummy MPVolumeView and access its subview hierarchy to make it possible to change the volume. This is doomed to fail, though, since an MPVolumeView needs to be inserted into the view hierarchy to ensure that system volume controls do not appear redundantly when changing the volume.
  • This is why we considered another option, namely wrapping an almost transparent MPVolumeView into a SwiftUI view. As said above this requires accessing the underlying slider (potentially fragile) but, moreover, updates obtained through key-value observation of its value property were not smooth (when bound to a slider, the slider knob would move in large chunks, a bit erratically). Alternatively we considered key-value observation of AVAudioSession outputVolume property, documented as officially supported, but this approach requires the audio session to be somehow active, otherwise updates are not received.

For all these reasons we decided not to provide any system volume-related API. It is obvious that providing volume controls (outside MPVolumeView) is currently not properly supported by current APIs.

During our experimentations we also shared code between demo playback progress and volume sliders. We intended to provide the common generic slider implementation as a PillarboxPlayer UI component but finally decided not to, as the API design was still half-baked (should we support slider vertical orientation? A styling API?).

@github-project-automation github-project-automation bot moved this from 🍿 Code Review to ✅ Done in Pillarbox Jan 23, 2025
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: ✅ Done
Development

Successfully merging a pull request may close this issue.

3 participants