From 6a53f7d95362691458ef62da1494a5f927460950 Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Wed, 15 Jul 2020 09:37:33 +0300 Subject: [PATCH] Some audio recording fixes --- AutoCat.xcodeproj/project.pbxproj | 4 ++-- AutoCat/AppDelegate.swift | 2 -- AutoCat/Controllers/CheckController.swift | 4 +++- AutoCat/Controllers/RecordsController.swift | 5 ++++- AutoCat/SceneDelegate.swift | 7 +++++++ AutoCat/Utils/AudioPlayer.swift | 2 +- AutoCat/Utils/Recorder.swift | 2 +- 7 files changed, 18 insertions(+), 8 deletions(-) diff --git a/AutoCat.xcodeproj/project.pbxproj b/AutoCat.xcodeproj/project.pbxproj index 7fd2fc1..aa067c1 100644 --- a/AutoCat.xcodeproj/project.pbxproj +++ b/AutoCat.xcodeproj/project.pbxproj @@ -683,7 +683,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AutoCat/AutoCat.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 19; + CURRENT_PROJECT_VERSION = 20; DEVELOPMENT_TEAM = 46DTTB8X4S; INFOPLIST_FILE = AutoCat/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 13.0; @@ -705,7 +705,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AutoCat/AutoCat.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 19; + CURRENT_PROJECT_VERSION = 20; DEVELOPMENT_TEAM = 46DTTB8X4S; INFOPLIST_FILE = AutoCat/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 13.0; diff --git a/AutoCat/AppDelegate.swift b/AutoCat/AppDelegate.swift index 6f1df69..d859c51 100644 --- a/AutoCat/AppDelegate.swift +++ b/AutoCat/AppDelegate.swift @@ -75,7 +75,5 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } - - } diff --git a/AutoCat/Controllers/CheckController.swift b/AutoCat/Controllers/CheckController.swift index 5a8682b..6857674 100644 --- a/AutoCat/Controllers/CheckController.swift +++ b/AutoCat/Controllers/CheckController.swift @@ -173,6 +173,8 @@ class CheckController: UIViewController, MaskedTextFieldDelegateListener, UITabl } updateAction.image = UIImage(systemName: "arrow.2.circlepath") updateAction.backgroundColor = .systemBlue - return UISwipeActionsConfiguration(actions: [updateAction]) + let configuration = UISwipeActionsConfiguration(actions: [updateAction]) + configuration.performsFirstActionWithFullSwipe = false + return configuration } } diff --git a/AutoCat/Controllers/RecordsController.swift b/AutoCat/Controllers/RecordsController.swift index 0089fed..3612276 100644 --- a/AutoCat/Controllers/RecordsController.swift +++ b/AutoCat/Controllers/RecordsController.swift @@ -115,6 +115,7 @@ class RecordsController: UIViewController, UITableViewDelegate { do { let alert = UIAlertController(title: "Recording...", message: nil, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in self.recorder?.cancelRecording() })) + alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { _ in self.recorder?.stopRecording() })) self.present(alert, animated: true) let date = Date() @@ -235,7 +236,9 @@ class RecordsController: UIViewController, UITableViewDelegate { delete.image = UIImage(systemName: "trash") let actions = record.number == nil ? [delete, action] : [delete, check, action] - return UISwipeActionsConfiguration(actions: actions) + let configuration = UISwipeActionsConfiguration(actions: actions) + configuration.performsFirstActionWithFullSwipe = false + return configuration } func moreActions(for record: AudioRecord, cell: UITableViewCell) { diff --git a/AutoCat/SceneDelegate.swift b/AutoCat/SceneDelegate.swift index 9891540..2f7c5b6 100644 --- a/AutoCat/SceneDelegate.swift +++ b/AutoCat/SceneDelegate.swift @@ -1,5 +1,6 @@ import UIKit import os.log +import AVFoundation class SceneDelegate: UIResponder, UIWindowSceneDelegate { @@ -56,6 +57,12 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { // Called as the scene transitions from the foreground to the background. // Use this method to save data, release shared resources, and store enough scene-specific state information // to restore the scene back to its current state. + + do { + try AVAudioSession.sharedInstance().setActive(false) + } catch { + print("sceneDidEnterBackground failed to deactivate audio session: \(error.localizedDescription)") + } } func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { diff --git a/AutoCat/Utils/AudioPlayer.swift b/AutoCat/Utils/AudioPlayer.swift index 750a052..40b54ee 100644 --- a/AutoCat/Utils/AudioPlayer.swift +++ b/AutoCat/Utils/AudioPlayer.swift @@ -37,7 +37,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate { try AVAudioSession.sharedInstance().setActive(false) self.state.accept(.paused) } else { - try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker]) + try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.duckOthers]) try AVAudioSession.sharedInstance().setActive(true) player.play() self.state.accept(.playing) diff --git a/AutoCat/Utils/Recorder.swift b/AutoCat/Utils/Recorder.swift index 62426a4..5a83567 100644 --- a/AutoCat/Utils/Recorder.swift +++ b/AutoCat/Utils/Recorder.swift @@ -59,7 +59,6 @@ class Recorder { } func startRecording(to file: URL, completion: @escaping (String) -> Void) throws { - let inFormat = self.engine.inputNode.outputFormat(forBus: 0) guard let aac = AVAudioFormat(settings: self.recordingSettings) else { throw CocoaError.error("Recording error", suggestion: "Format not supported") } @@ -72,6 +71,7 @@ class Recorder { try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: []) try AVAudioSession.sharedInstance().setActive(true) + let inFormat = self.engine.inputNode.outputFormat(forBus: 0) ExtAudioFileSetProperty(fileRef, kExtAudioFileProperty_ClientDataFormat, UInt32(MemoryLayout.size), inFormat.streamDescription) self.engine.inputNode.installTap(onBus: 0, bufferSize: 1024, format: inFormat) { buffer, time in