Skip to content

Instantly share code, notes, and snippets.

@AmitaiB
Last active June 1, 2022 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmitaiB/81ffb060cb4052bbdb4216c333e8c7ba to your computer and use it in GitHub Desktop.
Save AmitaiB/81ffb060cb4052bbdb4216c333e8c7ba to your computer and use it in GitHub Desktop.
Extends JWPlayer to have basic mute API
//
// JWPlayer+Mute.swift
//
// Created by Amitai Blickstein on 6/1/22.
//
import Foundation
import JWPlayerKit
fileprivate let kVolumeKey = "kVolumeKey"
extension JWPlayer {
func mute() {
guard !isMuted else { return }
originalVolume = volume
volume = 0
}
func unmute() {
guard isMuted else { return }
volume = originalVolume ?? 1
originalVolume = nil
}
var isMuted: Bool {
get { volume == 0 }
set { newValue ? mute() : unmute() }
}
func toggleMute() {
isMuted.toggle()
}
var originalVolume: CGFloat? {
get { UserDefaults.standard.value(forKey: kVolumeKey) as? CGFloat }
set { UserDefaults.standard.set(newValue, forKey: kVolumeKey) }
}
}
// Usage
player.mute() // mutes
player.isMuted = true // mutes
player.isMuted.toggle() // unmutes to previous volume
// etc..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment