-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4090 - Add voice message controller, audio recorder and toolbar view…
… links. Working audio file sending and cancellation.
- Loading branch information
1 parent
d2f22c2
commit d882297
Showing
9 changed files
with
273 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// 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 AVFoundation | ||
|
||
protocol AudioRecorderDelegate: AnyObject { | ||
func audioRecorderDidStartRecording(_ audioRecorder: AudioRecorder) | ||
func audioRecorderDidFinishRecording(_ audioRecorder: AudioRecorder) | ||
func audioRecorder(_ audioRecorder: AudioRecorder, didFailWithError: Error) | ||
} | ||
|
||
enum AudioRecorderError: Error { | ||
case genericError | ||
} | ||
|
||
class AudioRecorder: NSObject, AVAudioRecorderDelegate { | ||
|
||
private(set) var isRecording: Bool = false | ||
private(set) var currentTime: TimeInterval = 0 | ||
|
||
var url: URL? { | ||
return audioRecorder?.url | ||
} | ||
private var audioRecorder: AVAudioRecorder? | ||
|
||
weak var delegate: AudioRecorderDelegate? | ||
|
||
func recordWithOuputURL(_ url: URL) { | ||
|
||
let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), | ||
AVSampleRateKey: 12000, | ||
AVNumberOfChannelsKey: 1, | ||
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue] | ||
|
||
do { | ||
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default) | ||
audioRecorder = try AVAudioRecorder(url: url, settings: settings) | ||
audioRecorder?.delegate = self | ||
audioRecorder?.record() | ||
delegate?.audioRecorderDidStartRecording(self) | ||
} catch { | ||
delegate?.audioRecorder(self, didFailWithError: AudioRecorderError.genericError) | ||
} | ||
|
||
} | ||
|
||
func stopRecording() { | ||
audioRecorder?.stop() | ||
} | ||
|
||
// MARK: - AVAudioRecorderDelegate | ||
|
||
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully success: Bool) { | ||
if success { | ||
delegate?.audioRecorderDidFinishRecording(self) | ||
} else { | ||
delegate?.audioRecorder(self, didFailWithError: AudioRecorderError.genericError) | ||
} | ||
} | ||
|
||
func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) { | ||
delegate?.audioRecorder(self, didFailWithError: AudioRecorderError.genericError) | ||
} | ||
} | ||
|
||
extension String: LocalizedError { | ||
public var errorDescription: String? { return self } | ||
} |
109 changes: 109 additions & 0 deletions
109
Riot/Modules/Room/VoiceMessages/VoiceMessageController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// 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 | ||
|
||
@objc public protocol VoiceMessageControllerDelegate: AnyObject { | ||
func voiceMessageController(_ voiceMessageController: VoiceMessageController, didRequestSendForFileAtURL url: URL, completion: @escaping (Bool) -> Void) | ||
} | ||
|
||
public class VoiceMessageController: NSObject, VoiceMessageToolbarViewDelegate, AudioRecorderDelegate { | ||
|
||
private let themeService: ThemeService | ||
private let _voiceMessageToolbarView: VoiceMessageToolbarView | ||
|
||
private var audioRecorder: AudioRecorder? | ||
|
||
@objc public weak var delegate: VoiceMessageControllerDelegate? | ||
|
||
@objc public var voiceMessageToolbarView: UIView { | ||
return _voiceMessageToolbarView | ||
} | ||
|
||
@objc public init(themeService: ThemeService) { | ||
_voiceMessageToolbarView = VoiceMessageToolbarView.instanceFromNib() | ||
self.themeService = themeService | ||
|
||
super.init() | ||
|
||
_voiceMessageToolbarView.delegate = self | ||
|
||
self._voiceMessageToolbarView.update(theme: self.themeService.theme) | ||
NotificationCenter.default.addObserver(self, selector: #selector(handleThemeDidChange), name: .themeServiceDidChangeTheme, object: nil) | ||
} | ||
|
||
// MARK: - VoiceMessageToolbarViewDelegate | ||
|
||
func voiceMessageToolbarViewDidRequestRecordingStart(_ toolbarView: VoiceMessageToolbarView) { | ||
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) | ||
let temporaryFileURL = temporaryDirectoryURL.appendingPathComponent(ProcessInfo().globallyUniqueString) | ||
|
||
audioRecorder = AudioRecorder() | ||
audioRecorder?.delegate = self | ||
audioRecorder?.recordWithOuputURL(temporaryFileURL) | ||
} | ||
|
||
func voiceMessageToolbarViewDidRequestRecordingFinish(_ toolbarView: VoiceMessageToolbarView) { | ||
audioRecorder?.stopRecording() | ||
|
||
guard let url = audioRecorder?.url else { | ||
MXLog.error("Invalid audio recording URL") | ||
return | ||
} | ||
|
||
delegate?.voiceMessageController(self, didRequestSendForFileAtURL: url) { [weak self] success in | ||
self?.deleteRecordingAtURL(url) | ||
} | ||
} | ||
|
||
func voiceMessageToolbarViewDidRequestRecordingCancel(_ toolbarView: VoiceMessageToolbarView) { | ||
audioRecorder?.stopRecording() | ||
deleteRecordingAtURL(audioRecorder?.url) | ||
} | ||
|
||
// MARK: - AudioRecorderDelegate | ||
|
||
func audioRecorderDidStartRecording(_ audioRecorder: AudioRecorder) { | ||
_voiceMessageToolbarView.state = .recording | ||
} | ||
|
||
func audioRecorderDidFinishRecording(_ audioRecorder: AudioRecorder) { | ||
_voiceMessageToolbarView.state = .idle | ||
} | ||
|
||
func audioRecorder(_ audioRecorder: AudioRecorder, didFailWithError: Error) { | ||
MXLog.error("Failed recording voice message.") | ||
_voiceMessageToolbarView.state = .idle | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func deleteRecordingAtURL(_ url: URL?) { | ||
guard let url = url else { | ||
return | ||
} | ||
|
||
do { | ||
try FileManager.default.removeItem(at: url) | ||
} catch { | ||
MXLog.error(error) | ||
} | ||
} | ||
|
||
@objc private func handleThemeDidChange() { | ||
self._voiceMessageToolbarView.update(theme: self.themeService.theme) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.