Commit 1daaf8c8 authored by zhangzhe's avatar zhangzhe

修改bug

parent 6f7db351
......@@ -37,6 +37,13 @@ class SHInputController: SHBaseViewController
/// 播放计时器
let playTimer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global())
/// 播放计算器是否正在计时
var playTimerCounting = false
/// 播放完成
var playFinished = false
/// 是否是编辑
var isEdited = false
......@@ -81,21 +88,27 @@ class SHInputController: SHBaseViewController
}
// 初始化播放计时器
self.playTimer.schedule(deadline: .now(), repeating: 0.03)
self.playTimer.setEventHandler {
DispatchQueue.main.sync {
self.shInputView.bottomView.progressView.slider.value = Float(self.audioPlayer?.currentTime ?? 0) / Float(self.audioPlayer?.duration ?? 0)
self.shInputView.bottomView.progressView.timeLabel.text = self.viewModel.getTimeStringWithSeconds(seconds: NSInteger(self.audioPlayer?.currentTime ?? 0))
}
}
self.initPlayTimer()
self.addSubviews()
self.setUpSubviewsLocation()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// 禁用右滑返回
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// 恢复右滑返回
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
......@@ -195,7 +208,23 @@ class SHInputController: SHBaseViewController
// MARK: 点击navigationBar左侧按钮
override func goback()
{
self.navigationController?.popViewController(animated: true)
let alert = UIAlertController(title: "确认退出编辑?", message: nil, preferredStyle: .alert)
let leftAction = UIAlertAction(title: "直接退出", style: .destructive) { (action) in
self.navigationController?.popViewController(animated: true)
}
alert.addAction(leftAction)
let rightAction = UIAlertAction(title: "保存并退出", style: .default) { (action) in
self.didSelectedNavRightButton()
}
alert.addAction(rightAction)
self.present(alert, animated: true, completion: nil)
}
// MARK: 点击navigationBar右侧按钮
......@@ -394,6 +423,20 @@ class SHInputController: SHBaseViewController
extension SHInputController: UITextViewDelegate, SHInputTextViewDelegate
{
// MARK: - UITextView代理
// MARK: 滑动
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let vel = scrollView.panGestureRecognizer.velocity(in: scrollView)
if vel.y > 20
{
if self.shInputView.textView.canResignFirstResponder
{
self.shInputView.textView.resignFirstResponder()
}
}
}
// MARK: 文字发生了变化
func textViewDidChange(_ textView: UITextView)
{
......@@ -768,8 +811,69 @@ extension SHInputController: UIImagePickerControllerDelegate, UINavigationContro
// MARK: - 录音
extension SHInputController
{
// MARK: 点击添加录音按钮
// MARK: 点击添加录音按钮, 先判断麦克风权限
@objc func didSelectedAddAudioButton(sender : SHInputAudioButton)
{
let status : AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .audio)
switch status
{
// MARK: 此应用程序未被授权, 可能是家长控制权限。
case AVAuthorizationStatus.restricted:
let alert = UIAlertController(title: nil, message: "此应用程序未被授权, 可能是家长控制权限。", preferredStyle: UIAlertController.Style.alert)
let sureAction = UIAlertAction(title: "确定", style: UIAlertAction.Style.default, handler: nil)
alert.addAction(sureAction)
self.present(alert, animated: true, completion: nil)
break
// MARK: 用户拒绝当前应用访问麦克风
case AVAuthorizationStatus.denied:
let alert = UIAlertController(title: "此功能需要麦克风授权", message: "请您在设置系统中打开授权开关", preferredStyle: UIAlertController.Style.alert)
let cancelAction = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel, handler: nil)
alert.addAction(cancelAction)
let settingAction = UIAlertAction(title: "前往设置", style: UIAlertAction.Style.default) { (action) in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
}
alert.addAction(settingAction)
self.present(alert, animated: true, completion: nil)
break
// MARK: 用户允许当前应用访问麦克风
case AVAuthorizationStatus.authorized:
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera)
{
self.beginAddAudio()
}
break
// MARK: 用户还没有做出选择
case AVAuthorizationStatus.notDetermined:
AVCaptureDevice.requestAccess(for: .audio) { (flag) in
if flag
{
DispatchQueue.main.async {
self.beginAddAudio()
}
}
}
break
default:
break
}
}
// MARK: 开始录音
func beginAddAudio()
{
self.shInputView.bottomView.addAudioButton.isHidden = true
self.shInputView.bottomView.audioTimeBgView.isHidden = false
......@@ -836,44 +940,110 @@ extension SHInputController
// MARK: 点击结束录制按钮
@objc func didSelectedStopAudioButton(sender : SHInputAudioButton)
{
self.shInputView.bottomView.addAudioButton.isHidden = true
self.shInputView.bottomView.audioTimeBgView.isHidden = true
self.shInputView.bottomView.pauseAudioButton.isHidden = true
self.shInputView.bottomView.goOnAudioButton.isHidden = true
self.shInputView.bottomView.stopAudioButton.isHidden = true
self.shInputView.bottomView.playView.isHidden = false
self.shInputView.bottomView.setUpSubviewsLocation()
recorder_mp3.stop()
self.timer.cancel()
self.shInputView.bottomView.timer.cancel()
recorder_mp3.pause()
self.initPlayer()
self.timer.suspend()
self.shInputView.bottomView.playView.timeLabel.text = self.viewModel.getTimeStringWithSeconds(seconds: NSInteger(self.audioPlayer?.duration ?? 0))
self.shInputView.bottomView.audioTimePointViewAnimation(animation: false)
self.shInputView.setNeedsUpdateConstraints()
let alert = UIAlertController(title: "提示", message: "结束录音后,不能再进行续录。", preferredStyle: .alert)
UIView.animate(withDuration: 0.3) {
let leftAction = UIAlertAction(title: "结束录音", style: .destructive) { (action) in
self.shInputView.bottomView.addAudioButton.isHidden = true
self.shInputView.bottomView.audioTimeBgView.isHidden = true
self.shInputView.bottomView.pauseAudioButton.isHidden = true
self.shInputView.bottomView.goOnAudioButton.isHidden = true
self.shInputView.bottomView.stopAudioButton.isHidden = true
self.shInputView.bottomView.playView.isHidden = false
self.shInputView.bottomView.setUpSubviewsLocation()
self.recorder_mp3.stop()
self.shInputView.bottomView.mas_updateConstraints { (make) in
self.timer.cancel()
self.shInputView.bottomView.timer.cancel()
self.initPlayer()
self.shInputView.bottomView.playView.timeLabel.text = self.viewModel.getTimeStringWithSeconds(seconds: NSInteger(self.audioPlayer?.duration ?? 0))
self.shInputView.setNeedsUpdateConstraints()
UIView.animate(withDuration: 0.3) {
self.shInputView.bottomView.mas_updateConstraints { (make) in
make?.height.mas_equalTo()(100 + SafeAreaBottomHeight())
}
make?.height.mas_equalTo()(100 + SafeAreaBottomHeight())
self.shInputView.bottomView.progressView.alpha = 1;
self.shInputView.bottomView.superview!.layoutIfNeeded()
}
self.shInputView.bottomView.progressView.alpha = 1;
self.shInputView.bottomView.superview!.layoutIfNeeded()
}
alert.addAction(leftAction)
let rightAction = UIAlertAction(title: "暂不结束", style: .default) { (action) in
}
alert.addAction(rightAction)
self.present(alert, animated: true, completion: nil)
}
}
// MARK: - 播放
extension SHInputController: AVAudioPlayerDelegate {
// MARK: 初始化播放计时器
func initPlayTimer()
{
self.playTimer.schedule(deadline: .now(), repeating: 0.03)
self.playTimer.setEventHandler {
DispatchQueue.main.sync {
print("计时中" + String(self.audioPlayer?.currentTime ?? 0))
if Float(self.audioPlayer?.duration ?? 0) - Float(self.audioPlayer?.currentTime ?? 0) <= 0.1
{
self.playFinished = true
}
if !self.playFinished
{
self.shInputView.bottomView.progressView.slider.value = min(Float(self.audioPlayer?.currentTime ?? 0) / Float(self.audioPlayer?.duration ?? 0), 1)
self.shInputView.bottomView.progressView.timeLabel.text = self.viewModel.getTimeStringWithSeconds(seconds: NSInteger(self.audioPlayer?.currentTime ?? 0))
}
}
}
}
// MARK: 播放计时器开始计时
func startPlayTimer()
{
if !playTimerCounting
{
self.playTimer.resume()
playTimerCounting = true
}
}
// MARK: 播放计时器暂停计时
func pausePlayTimer()
{
if playTimerCounting
{
self.playTimer.suspend()
playTimerCounting = false
}
}
// MARK: 初始化播放器
func initPlayer()
{
......@@ -894,6 +1064,8 @@ extension SHInputController: AVAudioPlayerDelegate {
self.audioPlayer?.enableRate = true // 允许变速播放
self.audioPlayer?.delegate = self
self.dataModel.during = NSInteger(self.audioPlayer?.duration ?? 0)
// 后台播放
let session = AVAudioSession.sharedInstance()
try? session.setCategory(AVAudioSession.Category.playAndRecord, options: .defaultToSpeaker)
......@@ -907,17 +1079,24 @@ extension SHInputController: AVAudioPlayerDelegate {
// 暂停
self.audioPlayer?.pause()
self.playTimer.suspend()
self.pausePlayTimer()
sender.setImage(UIImage(named: "input_audio_play"), for: .normal)
}
else
{
// 播放
self.playTimer.resume()
if self.playFinished
{
self.audioPlayer?.currentTime = 0;
self.playFinished = false
}
self.audioPlayer?.play()
self.startPlayTimer()
sender.setImage(UIImage(named: "input_audio_pause"), for: .normal)
}
}
......@@ -925,33 +1104,38 @@ extension SHInputController: AVAudioPlayerDelegate {
// MARK: 后退10s
@objc func didSelectedBeforeButton(sender: UIButton)
{
// self.playTimer.suspend()
//
// self.audioPlayer.pause()
//
self.playFinished = false
self.audioPlayer?.pause()
self.pausePlayTimer()
self.audioPlayer?.currentTime = max((self.audioPlayer?.currentTime ?? 0) - 10, 0)
// self.playTimer.resume()
//
// self.audioPlayer.play()
//
// self.shInputView.bottomView.playView.playButton.setImage(UIImage(named: "input_audio_play"), for: .normal)
self.audioPlayer?.play()
self.startPlayTimer()
self.shInputView.bottomView.playView.playButton.setImage(UIImage(named: "input_audio_pause"), for: .normal)
}
// MARK: 前进10s
@objc func didSelectedLaterButton(sender: UIButton)
{
// self.playTimer.suspend()
//
// self.audioPlayer.pause()
//
self.audioPlayer?.currentTime = min((self.audioPlayer?.currentTime ?? 0) + 10, TimeInterval(self.audioPlayer?.duration ?? 0))
self.playFinished = false
// self.playTimer.resume()
//
// self.audioPlayer.play()
//
// self.shInputView.bottomView.playView.playButton.setImage(UIImage(named: "input_audio_play"), for: .normal)
self.audioPlayer?.pause()
self.pausePlayTimer()
// 最大时间直接等于self.audioPlayer?.duration音频会从第1s重新开始播放
self.audioPlayer?.currentTime = min((self.audioPlayer?.currentTime ?? 0) + 10, TimeInterval(self.audioPlayer?.duration ?? 0) - 0.03)
self.audioPlayer?.play()
self.startPlayTimer()
self.shInputView.bottomView.playView.playButton.setImage(UIImage(named: "input_audio_pause"), for: .normal)
}
// MARK: 开始拖动进度条
......@@ -960,7 +1144,7 @@ extension SHInputController: AVAudioPlayerDelegate {
// 暂停
self.audioPlayer?.pause()
self.playTimer.suspend()
self.pausePlayTimer()
self.shInputView.bottomView.playView.playButton.setImage(UIImage(named: "input_audio_play"), for: .normal)
}
......@@ -976,33 +1160,36 @@ extension SHInputController: AVAudioPlayerDelegate {
// MARK: 结束拖动进度条
@objc func sliderTouchEnd(sender: UISlider)
{
// 播放
self.audioPlayer?.currentTime = min(TimeInterval(Double(sender.value) * Double(self.audioPlayer?.duration ?? 0)), Double(self.audioPlayer?.duration ?? 0))
self.playFinished = false
self.playTimer.resume()
// 播放
// 最大时间直接等于self.audioPlayer?.duration音频会从第1s重新开始播放
self.audioPlayer?.currentTime = min(TimeInterval(Double(sender.value) * Double(self.audioPlayer?.duration ?? 0)), Double(self.audioPlayer?.duration ?? 0) - 0.03)
self.audioPlayer?.play()
self.startPlayTimer()
self.shInputView.bottomView.playView.playButton.setImage(UIImage(named: "input_audio_pause"), for: .normal)
}
// MARK: 切换倍速
@objc func didSelectedSpeedButton(sender: UIButton)
{
if sender.titleLabel!.text!.isEqualTo("x1.5")
if sender.titleLabel!.text!.isEqualTo("x1")
{
self.audioPlayer?.rate = 1.5
sender.setTitle("x2", for: .normal)
sender.setTitle("x1.5", for: .normal)
}
else if sender.titleLabel!.text!.isEqualTo("x2")
else if sender.titleLabel!.text!.isEqualTo("x1.5")
{
self.audioPlayer?.rate = 2
sender.setTitle("x1", for: .normal)
sender.setTitle("x2", for: .normal)
}
else if sender.titleLabel!.text!.isEqualTo("x1")
else if sender.titleLabel!.text!.isEqualTo("x2")
{
self.audioPlayer?.rate = 1
sender.setTitle("x1.5", for: .normal)
sender.setTitle("x1", for: .normal)
}
}
......@@ -1010,10 +1197,12 @@ extension SHInputController: AVAudioPlayerDelegate {
// MARK: 播放完成
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
print("播放完成")
player.currentTime = player.duration
self.shInputView.bottomView.playView.playButton.setImage(UIImage(named: "input_audio_play"), for: .normal)
self.shInputView.bottomView.progressView.slider.value = 1;
self.shInputView.bottomView.progressView.timeLabel.text = self.viewModel.getTimeStringWithSeconds(seconds: NSInteger(self.audioPlayer?.duration ?? 0))
self.playTimer.suspend()
self.pausePlayTimer()
}
}
......@@ -64,7 +64,7 @@ class SHInputAudioPlayView: UIView {
// 播放速度按钮
self.speedButton = UIButton(type: .system)
self.speedButton.setTitle("x1.5", for: .normal)
self.speedButton.setTitle("x1", for: .normal)
self.speedButton.setTitleColor(UIColor(r: 65, g: 131, b: 244), for: .normal)
self.speedButton.titleLabel?.font = UIFont(name: SHPingFangMedium, size: 14)
self.addSubview(self.speedButton)
......
......@@ -198,7 +198,7 @@ class SHInputBottomView: UIView {
// 箭头图片
self.directionImageView = UIImageView()
self.directionImageView.image = UIImage(named: "input_down")
self.directionImageView.image = UIImage(named: "input_up")
self.addSubview(self.directionImageView)
self.directionImageView.isHidden = self.isEdited
......
......@@ -103,7 +103,7 @@ class SHInputView: UIView {
// MARK: 监听键盘出现
@objc func keyboardWillShow(note : Notification)
{
self.bottomView.directionImageView.image = UIImage(named: "input_up")
self.bottomView.directionImageView.image = UIImage(named: "input_down")
let info : Dictionary = note.userInfo! as Dictionary
let value = info[UIResponder.keyboardFrameEndUserInfoKey]
......@@ -132,7 +132,7 @@ class SHInputView: UIView {
// MARK: 监听键盘消失
@objc func keyboardWillHide(note : Notification)
{
self.bottomView.directionImageView.image = UIImage(named: "input_down")
self.bottomView.directionImageView.image = UIImage(named: "input_up")
weak var weakSelf = self
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment