检测用户何时将AVPlayer静音



我正在使用AVPlayer和AVPlayerItem进行直播。有没有办法知道用户在播放时是否对视频进行了静音/取消静音

您可以检查AVPlayer的isMuted值。

文件:

https://developer.apple.com/documentation/avfoundation/avplayer/1387544-ismuted

您可以使用KVOCombine的组合来观察isMuted属性的更改:

let player: AVPlayer
private var cancellables = Set<AnyCancellable>()
//...
player
.publisher(for: .isMuted)
.sink { isMuted in
// here you can handle the fact the the user has muted or unmuted the player  
}
.store(in: &cancellables)

您应该实现KVo来检测静音/取消静音更改。

注意:在swift中,虽然属性是isMuted,但你仍然应该在KVo中使用ObjectiveC属性,所以听muted键,你就被设置了。

最新更新