Skip to content

Commit

Permalink
Merge commit '85a51978f04d76fb4e5e4e8ec3a4b08c9874b5f8' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
laktyushin committed Mar 14, 2023
2 parents 6ced157 + 85a5197 commit cb79afd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 39 deletions.
10 changes: 10 additions & 0 deletions submodules/AccountContext/Sources/SharedMediaPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ public protocol SharedMediaPlaylistLocation {
func isEqual(to: SharedMediaPlaylistLocation) -> Bool
}

public func areSharedMediaPlaylistsEqual(_ lhs: SharedMediaPlaylist?, _ rhs: SharedMediaPlaylist?) -> Bool {
if let lhs = lhs, let rhs = rhs {
return lhs.id.isEqual(to: rhs.id) && lhs.location.isEqual(to: rhs.location)
} else if (lhs != nil) != (rhs != nil) {
return false
} else {
return true
}
}

public protocol SharedMediaPlaylist: AnyObject {
var id: SharedMediaPlaylistId { get }
var location: SharedMediaPlaylistLocation { get }
Expand Down
90 changes: 52 additions & 38 deletions submodules/TelegramUI/Sources/MediaManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -494,57 +494,71 @@ public final class MediaManagerImpl: NSObject, MediaManager {
switch type {
case .voice:
strongSelf.musicMediaPlayer?.control(.playback(.pause))
strongSelf.voiceMediaPlayer?.stop()
if let (account, playlist, settings, storedState) = inputData {
var continueInstantVideoLoopAfterFinish: Bool = true
var controlPlaybackWithProximity: Bool = true
if let playlist = playlist as? PeerMessagesMediaPlaylist {
continueInstantVideoLoopAfterFinish = playlist.context.sharedContext.energyUsageSettings.autoplayVideo
controlPlaybackWithProximity = playlist.context.sharedContext.currentMediaInputSettings.with({ $0.enableRaiseToSpeak })
}

let voiceMediaPlayer = SharedMediaPlayer(mediaManager: strongSelf, inForeground: strongSelf.inForeground, account: account, audioSession: strongSelf.audioSession, overlayMediaManager: strongSelf.overlayMediaManager, playlist: playlist, initialOrder: .reversed, initialLooping: .none, initialPlaybackRate: settings.voicePlaybackRate, playerIndex: nextPlayerIndex, controlPlaybackWithProximity: controlPlaybackWithProximity, type: type, continueInstantVideoLoopAfterFinish: continueInstantVideoLoopAfterFinish)
strongSelf.voiceMediaPlayer = voiceMediaPlayer
voiceMediaPlayer.playedToEnd = { [weak voiceMediaPlayer] in
if let strongSelf = self, let voiceMediaPlayer = voiceMediaPlayer, voiceMediaPlayer === strongSelf.voiceMediaPlayer {
voiceMediaPlayer.stop()
strongSelf.voiceMediaPlayer = nil
if areSharedMediaPlaylistsEqual(playlist, strongSelf.voiceMediaPlayer?.playlist), case .seek = control {
strongSelf.voiceMediaPlayer?.control(control)
} else {
strongSelf.voiceMediaPlayer?.stop()
var continueInstantVideoLoopAfterFinish: Bool = true
var controlPlaybackWithProximity: Bool = true
if let playlist = playlist as? PeerMessagesMediaPlaylist {
continueInstantVideoLoopAfterFinish = playlist.context.sharedContext.energyUsageSettings.autoplayVideo
controlPlaybackWithProximity = playlist.context.sharedContext.currentMediaInputSettings.with({ $0.enableRaiseToSpeak })
}
}
voiceMediaPlayer.cancelled = { [weak voiceMediaPlayer] in
if let strongSelf = self, let voiceMediaPlayer = voiceMediaPlayer, voiceMediaPlayer === strongSelf.voiceMediaPlayer {
voiceMediaPlayer.stop()
strongSelf.voiceMediaPlayer = nil

let voiceMediaPlayer = SharedMediaPlayer(mediaManager: strongSelf, inForeground: strongSelf.inForeground, account: account, audioSession: strongSelf.audioSession, overlayMediaManager: strongSelf.overlayMediaManager, playlist: playlist, initialOrder: .reversed, initialLooping: .none, initialPlaybackRate: settings.voicePlaybackRate, playerIndex: nextPlayerIndex, controlPlaybackWithProximity: controlPlaybackWithProximity, type: type, continueInstantVideoLoopAfterFinish: continueInstantVideoLoopAfterFinish)
strongSelf.voiceMediaPlayer = voiceMediaPlayer
voiceMediaPlayer.playedToEnd = { [weak voiceMediaPlayer] in
if let strongSelf = self, let voiceMediaPlayer = voiceMediaPlayer, voiceMediaPlayer === strongSelf.voiceMediaPlayer {
voiceMediaPlayer.stop()
strongSelf.voiceMediaPlayer = nil
}
}
voiceMediaPlayer.cancelled = { [weak voiceMediaPlayer] in
if let strongSelf = self, let voiceMediaPlayer = voiceMediaPlayer, voiceMediaPlayer === strongSelf.voiceMediaPlayer {
voiceMediaPlayer.stop()
strongSelf.voiceMediaPlayer = nil
}
}

var control = control
if case .seek = control {
} else if let timestamp = storedState?.timestamp {
control = .seek(timestamp)
}
voiceMediaPlayer.control(control)
voiceMediaPlayer.control(.playback(.play))
}

var control = control
if let timestamp = storedState?.timestamp {
control = .seek(timestamp)
}
voiceMediaPlayer.control(control)
} else {
strongSelf.voiceMediaPlayer?.stop()
strongSelf.voiceMediaPlayer = nil
}
case .music, .file:
strongSelf.musicMediaPlayer?.stop()
strongSelf.voiceMediaPlayer?.control(.playback(.pause))
if let (account, playlist, settings, storedState) = inputData {
let musicMediaPlayer = SharedMediaPlayer(mediaManager: strongSelf, inForeground: strongSelf.inForeground, account: account, audioSession: strongSelf.audioSession, overlayMediaManager: strongSelf.overlayMediaManager, playlist: playlist, initialOrder: settings.order, initialLooping: settings.looping, initialPlaybackRate: storedState?.playbackRate ?? .x1, playerIndex: nextPlayerIndex, controlPlaybackWithProximity: false, type: type, continueInstantVideoLoopAfterFinish: true)
strongSelf.musicMediaPlayer = musicMediaPlayer
musicMediaPlayer.cancelled = { [weak musicMediaPlayer] in
if let strongSelf = self, let musicMediaPlayer = musicMediaPlayer, musicMediaPlayer === strongSelf.musicMediaPlayer {
musicMediaPlayer.stop()
strongSelf.musicMediaPlayer = nil
if areSharedMediaPlaylistsEqual(playlist, strongSelf.musicMediaPlayer?.playlist), case .seek = control {
strongSelf.musicMediaPlayer?.control(control)
} else {
strongSelf.musicMediaPlayer?.stop()
let musicMediaPlayer = SharedMediaPlayer(mediaManager: strongSelf, inForeground: strongSelf.inForeground, account: account, audioSession: strongSelf.audioSession, overlayMediaManager: strongSelf.overlayMediaManager, playlist: playlist, initialOrder: settings.order, initialLooping: settings.looping, initialPlaybackRate: storedState?.playbackRate ?? .x1, playerIndex: nextPlayerIndex, controlPlaybackWithProximity: false, type: type, continueInstantVideoLoopAfterFinish: true)
strongSelf.musicMediaPlayer = musicMediaPlayer
musicMediaPlayer.cancelled = { [weak musicMediaPlayer] in
if let strongSelf = self, let musicMediaPlayer = musicMediaPlayer, musicMediaPlayer === strongSelf.musicMediaPlayer {
musicMediaPlayer.stop()
strongSelf.musicMediaPlayer = nil
}
}

var control = control
if case .seek = control {
} else if let timestamp = storedState?.timestamp {
control = .seek(timestamp)
}
strongSelf.musicMediaPlayer?.control(control)
strongSelf.musicMediaPlayer?.control(.playback(.play))
}

var control = control
if let timestamp = storedState?.timestamp {
control = .seek(timestamp)
}
strongSelf.musicMediaPlayer?.control(control)
} else {
strongSelf.musicMediaPlayer?.stop()
strongSelf.musicMediaPlayer = nil
}
}
Expand Down
4 changes: 3 additions & 1 deletion submodules/TelegramUI/Sources/SharedMediaPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ final class SharedMediaPlayer {
private let audioSession: ManagedAudioSession
private let overlayMediaManager: OverlayMediaManager
private let playerIndex: Int32
private let playlist: SharedMediaPlaylist
let playlist: SharedMediaPlaylist

private var playbackRate: AudioPlaybackRate

Expand Down Expand Up @@ -194,6 +194,8 @@ final class SharedMediaPlayer {

if controlPlaybackWithProximity {
self.forceAudioToSpeaker = !DeviceProximityManager.shared().currentValue()
} else {
self.forceAudioToSpeaker = true
}

playlist.currentItemDisappeared = { [weak self] in
Expand Down

0 comments on commit cb79afd

Please sign in to comment.