IMChatAudioView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. func showAudioRecordingView() //录音状态
  13. func hideAudioRecordingView() //关闭录音状态
  14. func changeRecordingView2uplide() //取消发送的状态
  15. func changeRecordingView2down()// 可以发送的状态
  16. }
  17. class IMChatAudioView: UIView {
  18. @IBOutlet weak var audioViewTitle: UILabel!
  19. @IBOutlet weak var audioRecordBtn: UIButton!
  20. @IBOutlet weak var audioBtnContainer: UIView!
  21. private var touchBtn: AudioTouchButton?
  22. private var isCancel = false
  23. private lazy var recordManager: O2RecordVoiceManager = {
  24. let rm = O2RecordVoiceManager()
  25. rm.delegate = self
  26. return rm
  27. }()
  28. var delegate: IMChatAudioViewDelegate?
  29. override func awakeFromNib() {
  30. // audioRecordBtn.addTarget(self, action: #selector(startRecord), for: .touchDown)
  31. // audioRecordBtn.addTarget(self, action: #selector(cancelRecord), for: .touchDragExit)
  32. // audioRecordBtn.addTarget(self, action: #selector(finishRecord), for: .touchUpInside)
  33. let audioTouchBtn = AudioTouchButton(frame: CGRect(x: 0, y: 0, width: 98, height: 98))
  34. self.audioBtnContainer.addSubview(audioTouchBtn)
  35. self.touchBtn = audioTouchBtn
  36. self.touchBtn?.touchBegan = {
  37. DDLogDebug("touchBegan 点击............这里开始录音.......")
  38. self.delegate?.showAudioRecordingView()
  39. self.startRecord()
  40. }
  41. self.touchBtn?.upglide = {
  42. self.isCancel = true
  43. self.delegate?.changeRecordingView2uplide()
  44. }
  45. self.touchBtn?.down = {
  46. self.isCancel = false
  47. self.delegate?.changeRecordingView2down()
  48. }
  49. self.touchBtn?.touchEnd = {
  50. DDLogDebug("touchEnd 点击.........这里结束录音..........")
  51. self.delegate?.hideAudioRecordingView()
  52. self.finishRecord()
  53. }
  54. // self.touchBtn?.voiceButton?.setTitle("按住说话", for: .normal)
  55. self.touchBtn?.voiceButton?.setImage(UIImage(named: "chat_mic"), for: .normal)
  56. self.touchBtn?.backgroundColor = UIColor(hex: "#F3F3F3")
  57. self.touchBtn?.layer.cornerRadius = 49
  58. self.touchBtn?.layer.masksToBounds = true
  59. }
  60. @objc private func startRecord() {
  61. DDLogError("startRecord record...................")
  62. self.isCancel = false
  63. self.audioViewTitle.text = "上滑取消发送"
  64. //开始录音
  65. recordManager.stopRecordCompletion = {
  66. DDLogDebug("结束录音!!")
  67. }
  68. recordManager.cancelledDeleteCompletion = {
  69. DDLogDebug("取消录音!")
  70. }
  71. recordManager.startRecordingWithPath(O2IMFileManager.shared.getRecorderPath(type: .Caf)) {
  72. DDLogDebug("开始录音!!!")
  73. }
  74. }
  75. // @objc private func cancelRecord() {
  76. // DDLogError("cancelRecord record...................")
  77. // self.audioViewTitle.text = "按住说话"
  78. // self.isCancel = true
  79. // //取消录音
  80. // recordManager.cancelledDeleteWithCompletion()
  81. // }
  82. @objc private func finishRecord() {
  83. DDLogError("finish record...................")
  84. self.audioViewTitle.text = "按住说话"
  85. if !self.isCancel {
  86. //录音结束
  87. recordManager.finishRecordingCompletion()
  88. if (recordManager.recordDuration! as NSString).floatValue < 1 {
  89. DispatchQueue.main.async {
  90. self.chrysan.show(.error, message: "说话时间太短", hideDelay: 1)
  91. }
  92. return
  93. }
  94. let filePath = O2IMFileManager.shared.getRecorderPath(type: .MP3)
  95. recordManager.convertCafToMp3(cafPath: recordManager.recordPath!, mp3Path: filePath)
  96. let data = try! Data(contentsOf: URL(fileURLWithPath: filePath))
  97. delegate?.sendVoice(path: filePath, voice: data, duration: recordManager.recordDuration!)
  98. }else {
  99. //取消录音
  100. recordManager.cancelledDeleteWithCompletion()
  101. }
  102. }
  103. }
  104. extension IMChatAudioView: O2RecordVoiceDelegate {
  105. func beyondLimit(_ time: TimeInterval) {
  106. //录音结束
  107. recordManager.finishRecordingCompletion()
  108. if (recordManager.recordDuration! as NSString).floatValue < 1 {
  109. DispatchQueue.main.async {
  110. self.chrysan.show(.error, message: "说话时间太短", hideDelay: 1)
  111. }
  112. return
  113. }
  114. let filePath = O2IMFileManager.shared.getRecorderPath(type: .MP3)
  115. recordManager.convertCafToMp3(cafPath: recordManager.recordPath!, mp3Path: filePath)
  116. let data = try! Data(contentsOf: URL(fileURLWithPath: filePath))
  117. delegate?.sendVoice(path: filePath, voice: data, duration: recordManager.recordDuration!)
  118. }
  119. }