mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-10-09 03:20:48 +00:00
Use raise to listen instead instead of bare proximity sensor for ear speaker switching
This commit is contained in:
parent
927f7786a0
commit
1aff8f3540
@ -9,6 +9,7 @@ import TelegramAudio
|
||||
import AccountContext
|
||||
import TelegramUniversalVideoContent
|
||||
import DeviceProximity
|
||||
import RaiseToListen
|
||||
|
||||
private enum SharedMediaPlaybackItem: Equatable {
|
||||
case audio(MediaPlayer)
|
||||
@ -120,7 +121,9 @@ final class SharedMediaPlayer {
|
||||
|
||||
private var playbackRate: AudioPlaybackRate
|
||||
|
||||
private var proximityManagerIndex: Int?
|
||||
//private var proximityManagerIndex: Int?
|
||||
private var raiseToListen: RaiseToListenManager?
|
||||
|
||||
private let controlPlaybackWithProximity: Bool
|
||||
private var forceAudioToSpeaker = false
|
||||
|
||||
@ -346,9 +349,10 @@ final class SharedMediaPlayer {
|
||||
} else {
|
||||
strongSelf.playbackStateValue.set(.single(nil))
|
||||
if !state.loading {
|
||||
if let proximityManagerIndex = strongSelf.proximityManagerIndex {
|
||||
DeviceProximityManager.shared().remove(proximityManagerIndex)
|
||||
}
|
||||
strongSelf.raiseToListen = nil
|
||||
// if let proximityManagerIndex = strongSelf.proximityManagerIndex {
|
||||
// DeviceProximityManager.shared().remove(proximityManagerIndex)
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -362,18 +366,44 @@ final class SharedMediaPlayer {
|
||||
})
|
||||
|
||||
if controlPlaybackWithProximity {
|
||||
self.proximityManagerIndex = DeviceProximityManager.shared().add { [weak self] value in
|
||||
let forceAudioToSpeaker = !value
|
||||
if let strongSelf = self, strongSelf.forceAudioToSpeaker != forceAudioToSpeaker {
|
||||
strongSelf.forceAudioToSpeaker = forceAudioToSpeaker
|
||||
strongSelf.playbackItem?.setForceAudioToSpeaker(forceAudioToSpeaker)
|
||||
if !forceAudioToSpeaker {
|
||||
strongSelf.control(.playback(.play))
|
||||
} else {
|
||||
strongSelf.control(.playback(.pause))
|
||||
self.raiseToListen = RaiseToListenManager(shouldActivate: {
|
||||
return true
|
||||
}, activate: { [weak self] in
|
||||
if let strongSelf = self {
|
||||
let forceAudioToSpeaker = false
|
||||
if strongSelf.forceAudioToSpeaker != forceAudioToSpeaker {
|
||||
strongSelf.forceAudioToSpeaker = forceAudioToSpeaker
|
||||
strongSelf.playbackItem?.setForceAudioToSpeaker(forceAudioToSpeaker)
|
||||
if !forceAudioToSpeaker {
|
||||
strongSelf.control(.playback(.play))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, deactivate: { [weak self] in
|
||||
if let strongSelf = self {
|
||||
let forceAudioToSpeaker = true
|
||||
if strongSelf.forceAudioToSpeaker != forceAudioToSpeaker {
|
||||
strongSelf.forceAudioToSpeaker = forceAudioToSpeaker
|
||||
strongSelf.playbackItem?.setForceAudioToSpeaker(forceAudioToSpeaker)
|
||||
if forceAudioToSpeaker {
|
||||
strongSelf.control(.playback(.pause))
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
self.raiseToListen?.enabled = true
|
||||
// self.proximityManagerIndex = DeviceProximityManager.shared().add { [weak self] value in
|
||||
// let forceAudioToSpeaker = !value
|
||||
// if let strongSelf = self, strongSelf.forceAudioToSpeaker != forceAudioToSpeaker {
|
||||
// strongSelf.forceAudioToSpeaker = forceAudioToSpeaker
|
||||
// strongSelf.playbackItem?.setForceAudioToSpeaker(forceAudioToSpeaker)
|
||||
// if !forceAudioToSpeaker {
|
||||
// strongSelf.control(.playback(.play))
|
||||
// } else {
|
||||
// strongSelf.control(.playback(.pause))
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@ -384,9 +414,9 @@ final class SharedMediaPlayer {
|
||||
self.playbackStateValueDisposable?.dispose()
|
||||
self.prefetchDisposable.dispose()
|
||||
|
||||
if let proximityManagerIndex = self.proximityManagerIndex {
|
||||
DeviceProximityManager.shared().remove(proximityManagerIndex)
|
||||
}
|
||||
// if let proximityManagerIndex = self.proximityManagerIndex {
|
||||
// DeviceProximityManager.shared().remove(proximityManagerIndex)
|
||||
// }
|
||||
|
||||
if let playbackItem = self.playbackItem {
|
||||
switch playbackItem {
|
||||
|
Loading…
x
Reference in New Issue
Block a user