How to record 2 avaudio sessions separately (swift4)

I am trying to record 2 separate audio files. Right now I can record one audio file and record over it. The first of the functions without 2 after it works. So i took what worked one time and added 2 to each object and function and it does not work. What I want to do is take what worked one time and repeated again.

                                                 import UIKit
                                 import AVFoundation

             class ViewController: UIViewController , AVAudioPlayerDelegate , AVAudioRecorderDelegate {
@IBOutlet weak var recordBTN: UIButton!
@IBOutlet weak var playBTN: UIButton!

var soundRecorder : AVAudioRecorder!
var soundPlayer : AVAudioPlayer!

var fileName: String = "audioFile.m4a"
var fileName2: String = "audioFile.m4a"

override func viewDidLoad() {
    super.viewDidLoad()

    setupRecorder()
    setupPlayer2()
    playBTN.isEnabled = false
}

func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    return paths[0]
}

func setupRecorder() {
    let audioFilename = getDocumentsDirectory().appendingPathComponent(fileName)
    let recordSetting = [ AVFormatIDKey : kAudioFormatAppleLossless,
                          AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,
                          AVEncoderBitRateKey : 320000,
                          AVNumberOfChannelsKey : 2,
                          AVSampleRateKey : 44100.2] as [String : Any]

    do {
        soundRecorder = try AVAudioRecorder(url: audioFilename, settings: recordSetting )
        soundRecorder.delegate = self
        soundRecorder.prepareToRecord()
    } catch {
        print(error)
    }
}

func setupPlayer() {
    let audioFilename = getDocumentsDirectory().appendingPathComponent(fileName)
    do {
        soundPlayer = try AVAudioPlayer(contentsOf: audioFilename)
        soundPlayer.delegate = self
        soundPlayer.prepareToPlay()
        soundPlayer.volume = 1.0
    } catch {
        print(error)
    }
}

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
    playBTN.isEnabled = true
}

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    recordBTN.isEnabled = true
    playBTN.setTitle("Play", for: .normal)
}

@IBAction func recordAct(_ sender: Any) {

    if recordBTN.titleLabel?.text == "Record" {
        soundRecorder.record()
        recordBTN.setTitle("Stop", for: .normal)
        playBTN.isEnabled = false
    } else {
        soundRecorder.stop()
        recordBTN.setTitle("Record", for: .normal)
        playBTN.isEnabled = false
    }
}

@IBAction func playAct(_ sender: Any) {

    if playBTN.titleLabel?.text == "Play" {
        playBTN.setTitle("Stop", for: .normal)
        recordBTN.isEnabled = false
        setupPlayer()
        soundPlayer.play()
    } else {
        soundPlayer.stop()
        playBTN.setTitle("Play", for: .normal)
        recordBTN.isEnabled = false
    }
}

///////
func audioRecorderDidFinishRecording2(_ recorder: AVAudioRecorder, successfully flag: Bool) {
    playBTN2.isEnabled = true
}

func audioPlayerDidFinishPlaying2(_ player: AVAudioPlayer, successfully flag: Bool) {
    recordBTN2.isEnabled = true
    playBTN2.setTitle("Play", for: .normal)
}

@IBAction func recordAct2(_ sender: Any) {

    if recordBTN2.titleLabel?.text == "Record" {
        soundRecorder2.record()
        recordBTN2.setTitle("Stop", for: .normal)
        playBTN2.isEnabled = false
    } else {
        soundRecorder2.stop()
        recordBTN2.setTitle("Record", for: .normal)
        playBTN2.isEnabled = false
    }
}

@IBAction func playAct2(_ sender: Any) {

    if playBTN2.titleLabel?.text == "Play" {
        playBTN2.setTitle("Stop", for: .normal)
        recordBTN.isEnabled = false
        setupPlayer2()
        soundPlayer2.play()
    } else {
        soundPlayer2.stop()
        playBTN2.setTitle("Play", for: .normal)
        recordBTN2.isEnabled = false
    }
}

@IBOutlet weak var recordBTN2: UIButton!
@IBOutlet weak var playBTN2: UIButton!
var soundRecorder2 : AVAudioRecorder!
var soundPlayer2 : AVAudioPlayer!

func setupPlayer2() {
    let audioFilename = getDocumentsDirectory().appendingPathComponent(fileName2)
    do {
        soundPlayer2 = try AVAudioPlayer(contentsOf: audioFilename)
        soundPlayer2.delegate = self
        soundPlayer2.prepareToPlay()
        soundPlayer2.volume = 1.0
    } catch {
        print(error)
    }
}

func setupRecorder2() {
    let audioFilename = getDocumentsDirectory().appendingPathComponent(fileName)
    let recordSetting = [ AVFormatIDKey : kAudioFormatAppleLossless,
                          AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,
                          AVEncoderBitRateKey : 320000,
                          AVNumberOfChannelsKey : 2,
                          AVSampleRateKey : 44100.2] as [String : Any]

    do {
        soundRecorder2 = try AVAudioRecorder(url: audioFilename, settings: recordSetting )
        soundRecorder2.delegate = self
        soundRecorder2.prepareToRecord()
    } catch {
        print(error)
    }
}
                     }

@timswift Do you still have issues with this?

Yes I am still awaiting a answer for this.

This topic was automatically closed after 166 days. New replies are no longer allowed.