IMChatAudioView.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // IMChatAudioView.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/17.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. protocol IMChatAudioViewDelegate {
  11. func sendVoice(path: String, voice: Data, duration: String)
  12. }
  13. class IMChatAudioView: UIView {
  14. @IBOutlet weak var audioViewTitle: UILabel!
  15. @IBOutlet weak var audioRecordBtn: UIButton!
  16. private var isCancel = false
  17. private lazy var recordManager: O2RecordVoiceManager = {
  18. let rm = O2RecordVoiceManager()
  19. rm.delegate = self
  20. return rm
  21. }()
  22. var delegate: IMChatAudioViewDelegate?
  23. override func awakeFromNib() {
  24. audioRecordBtn.addTarget(self, action: #selector(startRecord), for: .touchDown)
  25. audioRecordBtn.addTarget(self, action: #selector(cancelRecord), for: .touchDragExit)
  26. audioRecordBtn.addTarget(self, action: #selector(finishRecord), for: .touchUpInside)
  27. }
  28. @objc private func startRecord() {
  29. DDLogError("startRecord record...................")
  30. self.isCancel = false
  31. self.audioViewTitle.text = "上滑取消发送"
  32. //开始录音
  33. recordManager.stopRecordCompletion = {
  34. DDLogDebug("结束录音!!")
  35. }
  36. recordManager.cancelledDeleteCompletion = {
  37. DDLogDebug("取消录音!")
  38. }
  39. recordManager.startRecordingWithPath(O2IMFileManager.shared.getRecorderPath(type: .Caf)) {
  40. DDLogDebug("开始录音!!!")
  41. }
  42. }
  43. @objc private func cancelRecord() {
  44. DDLogError("cancelRecord record...................")
  45. self.audioViewTitle.text = "按住说话"
  46. self.isCancel = true
  47. //取消录音
  48. recordManager.cancelledDeleteWithCompletion()
  49. }
  50. @objc private func finishRecord() {
  51. DDLogError("finish record...................")
  52. self.audioViewTitle.text = "按住说话"
  53. if !self.isCancel {
  54. //录音结束
  55. recordManager.finishRecordingCompletion()
  56. if (recordManager.recordDuration! as NSString).floatValue < 1 {
  57. DispatchQueue.main.async {
  58. self.chrysan.show(.error, message: "说话时间太短", hideDelay: 1)
  59. }
  60. return
  61. }
  62. let filePath = O2IMFileManager.shared.getRecorderPath(type: .MP3)
  63. recordManager.convertCafToMp3(cafPath: recordManager.recordPath!, mp3Path: filePath)
  64. let data = try! Data(contentsOf: URL(fileURLWithPath: filePath))
  65. delegate?.sendVoice(path: filePath, voice: data, duration: recordManager.recordDuration!)
  66. }
  67. }
  68. }
  69. extension IMChatAudioView: O2RecordVoiceDelegate {
  70. func beyondLimit(_ time: TimeInterval) {
  71. //录音结束
  72. recordManager.finishRecordingCompletion()
  73. if (recordManager.recordDuration! as NSString).floatValue < 1 {
  74. DispatchQueue.main.async {
  75. self.chrysan.show(.error, message: "说话时间太短", hideDelay: 1)
  76. }
  77. return
  78. }
  79. let filePath = O2IMFileManager.shared.getRecorderPath(type: .MP3)
  80. recordManager.convertCafToMp3(cafPath: recordManager.recordPath!, mp3Path: filePath)
  81. let data = try! Data(contentsOf: URL(fileURLWithPath: filePath))
  82. delegate?.sendVoice(path: filePath, voice: data, duration: recordManager.recordDuration!)
  83. }
  84. }