如何分别录制2个AV音频会话(swift4)

时间:2019-01-29 05:54:20

标签: ios swift avaudioplayer avaudiosession avaudiorecorder

我正在尝试录制2个单独的音频文件。现在,我可以录制一个音频文件并对其进行录制。起作用的第一个函数不带2。所以我花了一次的功夫,并向每个对象和函数添加了2个,但它不起作用。我想要做的是一次成功,然后再重复一次。

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)
        }
    }
}

0 个答案:

没有答案