Skip to content

Commit

Permalink
Merge pull request #6870 from vector-im/alfogrillo/audio_player_info
Browse files Browse the repository at this point in the history
Background audio messages info (PSB-180)
  • Loading branch information
Alfonso Grillo committed Oct 20, 2022
2 parents 00cf240 + ac655c4 commit 86e3fcb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Config/BuildSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ final class BuildSettings: NSObject {

static let allowInviteExernalUsers: Bool = true

static let allowBackgroundAudioMessagePlayback: Bool = true

// MARK: - Side Menu
static let enableSideMenu: Bool = true && !newAppLayoutEnabled
static let sideMenuShowInviteFriends: Bool = true
Expand Down
11 changes: 11 additions & 0 deletions Riot/Modules/Room/VoiceMessages/VoiceMessageAudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class VoiceMessageAudioPlayer: NSObject {
private var playbackBufferEmptyObserver: NSKeyValueObservation?
private var rateObserver: NSKeyValueObservation?
private var playToEndObserver: NSObjectProtocol?
private var appBackgroundObserver: NSObjectProtocol?

private let delegateContainer = DelegateContainer()

Expand Down Expand Up @@ -198,13 +199,23 @@ class VoiceMessageAudioPlayer: NSObject {
(delegate as? VoiceMessageAudioPlayerDelegate)?.audioPlayerDidFinishPlaying(self)
}
}

appBackgroundObserver = NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { [weak self] _ in
guard let self = self, !BuildSettings.allowBackgroundAudioMessagePlayback else { return }

self.pause()
self.delegateContainer.notifyDelegatesWithBlock { delegate in
(delegate as? VoiceMessageAudioPlayerDelegate)?.audioPlayerDidPausePlaying(self)
}
}
}

private func removeObservers() {
statusObserver?.invalidate()
playbackBufferEmptyObserver?.invalidate()
rateObserver?.invalidate()
NotificationCenter.default.removeObserver(playToEndObserver as Any)
NotificationCenter.default.removeObserver(appBackgroundObserver as Any)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ import MediaPlayer
}

private func setUpRemoteCommandCenter() {
guard BuildSettings.allowBackgroundAudioMessagePlayback else {
return
}

displayLink.isPaused = false

UIApplication.shared.beginReceivingRemoteControlEvents()
Expand Down Expand Up @@ -252,14 +256,8 @@ import MediaPlayer
return
}

let artwork = MPMediaItemArtwork(boundsSize: Constants.roomAvatarImageSize) { [weak self] size in
return self?.roomAvatar ?? UIImage()
}

let nowPlayingInfoCenter = MPNowPlayingInfoCenter.default()
nowPlayingInfoCenter.nowPlayingInfo = [MPMediaItemPropertyTitle: audioPlayer.displayName ?? VectorL10n.voiceMessageLockScreenPlaceholder,
MPMediaItemPropertyArtist: currentRoomSummary?.displayname as Any,
MPMediaItemPropertyArtwork: artwork,
nowPlayingInfoCenter.nowPlayingInfo = [MPMediaItemPropertyTitle: VectorL10n.voiceMessageLockScreenPlaceholder,
MPMediaItemPropertyPlaybackDuration: audioPlayer.duration as Any,
MPNowPlayingInfoPropertyElapsedPlaybackTime: audioPlayer.currentTime as Any]
}
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-6870.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed the info in the background audio message player.

0 comments on commit 86e3fcb

Please sign in to comment.