2021-06-17 10:33:37 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import DSWaveformImage
|
|
|
|
|
|
|
|
enum VoiceMessagePlaybackControllerState {
|
|
|
|
case stopped
|
|
|
|
case playing
|
|
|
|
case paused
|
|
|
|
case error
|
|
|
|
}
|
|
|
|
|
|
|
|
class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMessagePlaybackViewDelegate {
|
2021-06-24 08:33:14 +00:00
|
|
|
|
2021-06-24 11:02:41 +00:00
|
|
|
private enum Constants {
|
|
|
|
static let elapsedTimeFormat = "m:ss"
|
|
|
|
}
|
|
|
|
|
2021-06-24 08:33:14 +00:00
|
|
|
private static let timeFormatter: DateFormatter = {
|
|
|
|
let dateFormatter = DateFormatter()
|
2021-06-24 11:02:41 +00:00
|
|
|
dateFormatter.dateFormat = Constants.elapsedTimeFormat
|
2021-06-24 08:33:14 +00:00
|
|
|
return dateFormatter
|
|
|
|
}()
|
2021-06-23 14:52:08 +00:00
|
|
|
|
2021-06-17 10:33:37 +00:00
|
|
|
private let audioPlayer: VoiceMessageAudioPlayer
|
|
|
|
private var displayLink: CADisplayLink!
|
|
|
|
private var samples: [Float] = []
|
|
|
|
|
|
|
|
private var state: VoiceMessagePlaybackControllerState = .stopped {
|
|
|
|
didSet {
|
|
|
|
updateUI()
|
|
|
|
displayLink.isPaused = (state != .playing)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let playbackView: VoiceMessagePlaybackView
|
|
|
|
|
2021-06-23 14:52:08 +00:00
|
|
|
init(mediaServiceProvider: VoiceMessageMediaServiceProvider) {
|
2021-06-23 12:37:34 +00:00
|
|
|
playbackView = VoiceMessagePlaybackView.loadFromNib()
|
2021-06-23 14:52:08 +00:00
|
|
|
audioPlayer = mediaServiceProvider.audioPlayer()
|
2021-06-17 10:33:37 +00:00
|
|
|
|
2021-06-23 14:52:08 +00:00
|
|
|
audioPlayer.registerDelegate(self)
|
2021-06-17 10:33:37 +00:00
|
|
|
playbackView.delegate = self
|
|
|
|
|
2021-06-22 06:59:14 +00:00
|
|
|
displayLink = CADisplayLink(target: WeakObjectWrapper(self), selector: #selector(handleDisplayLinkTick))
|
2021-06-17 10:33:37 +00:00
|
|
|
displayLink.isPaused = true
|
|
|
|
displayLink.add(to: .current, forMode: .common)
|
2021-06-23 12:37:34 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(updateTheme), name: .themeServiceDidChangeTheme, object: nil)
|
|
|
|
updateTheme()
|
2021-06-17 10:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var attachment: MXKAttachment? {
|
|
|
|
didSet {
|
|
|
|
if oldValue?.contentURL == attachment?.contentURL &&
|
|
|
|
oldValue?.eventSentState == attachment?.eventSentState {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch attachment?.eventSentState {
|
|
|
|
case MXEventSentStateFailed:
|
|
|
|
state = .error
|
|
|
|
default:
|
|
|
|
state = .stopped
|
|
|
|
loadAttachmentData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - VoiceMessagePlaybackViewDelegate
|
|
|
|
|
2021-06-22 10:19:39 +00:00
|
|
|
func voiceMessagePlaybackViewDidRequestPlaybackToggle() {
|
2021-06-17 10:33:37 +00:00
|
|
|
if audioPlayer.isPlaying {
|
|
|
|
audioPlayer.pause()
|
|
|
|
} else {
|
|
|
|
audioPlayer.play()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - VoiceMessageAudioPlayerDelegate
|
|
|
|
|
|
|
|
func audioPlayerDidFinishLoading(_ audioPlayer: VoiceMessageAudioPlayer) {
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
|
|
|
|
func audioPlayerDidStartPlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
|
|
|
|
state = .playing
|
|
|
|
}
|
|
|
|
|
2021-06-23 14:52:08 +00:00
|
|
|
func audioPlayerDidPausePlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
|
2021-06-17 10:33:37 +00:00
|
|
|
state = .paused
|
|
|
|
}
|
|
|
|
|
2021-06-23 14:52:08 +00:00
|
|
|
func audioPlayerDidStopPlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
|
|
|
|
state = .stopped
|
|
|
|
}
|
|
|
|
|
2021-06-17 10:33:37 +00:00
|
|
|
func audioPlayer(_ audioPlayer: VoiceMessageAudioPlayer, didFailWithError error: Error) {
|
|
|
|
state = .error
|
|
|
|
MXLog.error("Failed playing voice message with error: \(error)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func audioPlayerDidFinishPlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
|
|
|
|
audioPlayer.seekToTime(0.0)
|
|
|
|
state = .stopped
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
@objc private func handleDisplayLinkTick() {
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateUI() {
|
|
|
|
var details = VoiceMessagePlaybackViewDetails()
|
|
|
|
|
|
|
|
details.playbackEnabled = (state != .error)
|
|
|
|
details.playing = (state == .playing)
|
|
|
|
details.samples = samples
|
|
|
|
|
|
|
|
switch state {
|
|
|
|
case .stopped:
|
2021-06-24 08:33:14 +00:00
|
|
|
details.currentTime = VoiceMessagePlaybackController.timeFormatter.string(from: Date(timeIntervalSinceReferenceDate: audioPlayer.duration))
|
2021-06-17 10:33:37 +00:00
|
|
|
details.progress = 0.0
|
|
|
|
default:
|
2021-06-24 08:33:14 +00:00
|
|
|
details.currentTime = VoiceMessagePlaybackController.timeFormatter.string(from: Date(timeIntervalSinceReferenceDate: audioPlayer.currentTime))
|
2021-06-17 10:33:37 +00:00
|
|
|
details.progress = (audioPlayer.duration > 0.0 ? audioPlayer.currentTime / audioPlayer.duration : 0.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
playbackView.configureWithDetails(details)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func loadAttachmentData() {
|
|
|
|
guard let attachment = attachment else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if attachment.isEncrypted {
|
|
|
|
attachment.decrypt(toTempFile: { [weak self] filePath in
|
2021-06-24 09:28:50 +00:00
|
|
|
self?.convertAndLoadFileAtPath(filePath)
|
2021-06-17 10:33:37 +00:00
|
|
|
}, failure: { [weak self] error in
|
|
|
|
// A nil error in this case is a cancellation on the MXMediaLoader
|
|
|
|
if let error = error {
|
|
|
|
MXLog.error("Failed decrypting attachment with error: \(String(describing: error))")
|
|
|
|
self?.state = .error
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
attachment.prepare({ [weak self] in
|
2021-06-24 09:28:50 +00:00
|
|
|
self?.convertAndLoadFileAtPath(attachment.cacheFilePath)
|
2021-06-17 10:33:37 +00:00
|
|
|
}, failure: { [weak self] error in
|
2021-06-22 10:19:39 +00:00
|
|
|
// A nil error in this case is a cancellation on the MXMediaLoader
|
|
|
|
if let error = error {
|
|
|
|
MXLog.error("Failed preparing attachment with error: \(String(describing: error))")
|
|
|
|
self?.state = .error
|
|
|
|
}
|
2021-06-17 10:33:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 09:28:50 +00:00
|
|
|
private func convertAndLoadFileAtPath(_ path: String?) {
|
2021-06-17 10:33:37 +00:00
|
|
|
guard let filePath = path else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-24 09:28:50 +00:00
|
|
|
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
|
|
|
let newURL = temporaryDirectoryURL.appendingPathComponent(ProcessInfo().globallyUniqueString).appendingPathExtension("m4a")
|
2021-06-17 10:33:37 +00:00
|
|
|
|
2021-06-24 09:28:50 +00:00
|
|
|
VoiceMessageAudioConverter.convertToMPEG4AAC(sourceURL: URL(fileURLWithPath: filePath), destinationURL: newURL) { [weak self] result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
self?.loadFileAtURL(newURL)
|
|
|
|
case .failure(let error):
|
|
|
|
self?.state = .error
|
|
|
|
MXLog.error("Failed failed decoding audio message with: \(error)")
|
|
|
|
}
|
2021-06-17 10:33:37 +00:00
|
|
|
}
|
2021-06-24 09:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func loadFileAtURL(_ url: URL) {
|
2021-06-17 10:33:37 +00:00
|
|
|
|
2021-06-24 09:28:50 +00:00
|
|
|
audioPlayer.loadContentFromURL(url)
|
2021-06-17 10:33:37 +00:00
|
|
|
|
|
|
|
let requiredNumberOfSamples = playbackView.getRequiredNumberOfSamples()
|
|
|
|
|
|
|
|
if requiredNumberOfSamples == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-24 09:28:50 +00:00
|
|
|
let analyser = WaveformAnalyzer(audioAssetURL: url)
|
2021-06-17 10:33:37 +00:00
|
|
|
analyser?.samples(count: requiredNumberOfSamples, completionHandler: { [weak self] samples in
|
|
|
|
guard let samples = samples else {
|
|
|
|
self?.state = .error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self?.samples = samples
|
|
|
|
self?.updateUI()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-06-23 12:37:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@objc private func updateTheme() {
|
|
|
|
playbackView.update(theme: ThemeService.shared().theme)
|
|
|
|
}
|
2021-06-17 10:33:37 +00:00
|
|
|
}
|