Last active
June 1, 2022 15:36
-
-
Save AmitaiB/81ffb060cb4052bbdb4216c333e8c7ba to your computer and use it in GitHub Desktop.
Extends JWPlayer to have basic mute API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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