导航到另一个屏幕时继续录制音频

时间:2021-02-03 20:14:24

标签: ios swift audio avfoundation avaudiosession

我想继续我在一个屏幕中开始的 AVAudioSession(使用 AVAudioRecorder 录制),同时导航到另一个屏幕并返回。可以吗?
因为我总是得到一个较短的文件。

会话开始/录制开始

  func startRecording() {
    let session = AVAudioSession.sharedInstance()
    do {
        try session.setCategory(AVAudioSession.Category.playAndRecord, mode: .default)
        try session.setActive(true)
        let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                        AVSampleRateKey: 44100,
                        AVNumberOfChannelsKey: 2,
                        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue]
        audioRecorder = try AVAudioRecorder(url: getAudioFileUrl(), settings: settings)
        audioRecorder?.delegate = self
        audioRecorder?.record()
        isRecording = true
    }
    catch let error {
        print(error)
    }
}

录音机(录音机是其中的一部分:

class RecorderManager: NSObject {

static let shared = RecorderManager()

private var recordTimer : Timer?
private var recordTimerValue: Double?
private var audioRecorder = AudioRecordedManager()
private var videoRecorder = ScreenRecorderManager()

记录就是这样开始的。在屏幕 A 中:

 func startRecord(view: UIView){
    self.recordTimerValue = Double(0)
    
    if (self.recordTimer != nil){
        self.recordTimer?.invalidate()
        self.recordTimer = nil
    }
    
    self.showCounterAnimation(view: view, doneCallback: {
        
        self.videoRecorder.recordingSetup()
        self.audioRecorder.startRecording()
        self.videoRecorder.startRecording {

            if (self.recordTimer == nil){
                self.recordTimer = Timer.scheduledTimer(timeInterval: 1.0,
                                                        target: self,
                                                        selector: #selector(self.onTimerFires),
                                                        userInfo: nil,
                                                        repeats: true)
            }
        }
    })
    
}

这是到屏幕 B 的过渡:

   if let item = self.videoItem{
        if let listItem = self.videoListItem{
            if let videoLOcalUrl = self.videoLocalUrl{
                let vc:SplitViewController  = SplitViewController()
                vc.currentTimeValue = currentTimeValue
                playVideoViewModel.pause()
                vc.videoModel = SplitViewModel(videoItem: item, videoListItem: listItem, videoLocalUrl: videoLOcalUrl)
                vc.modalPresentationStyle = .fullScreen
                self.present(vc, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:0)

当然有可能。不要在视图控制器中编写录音代码,而是创建一个录音类。使其成为单例。从你的视图控制器触发录音,当你移动到不同的屏幕时让它继续运行。