ios - AVAudioRecorder 录制流式音频

标签 ios core-audio avplayer avaudiorecorder

我有一个播放流音频的应用程序。如何使用 AVAudioRecorder(或其他东西?)从 AVPlayer 录制流式音频。谢谢。

最佳答案

swift 3+ 这是将 Audio Session 保存到文档目录 (audio.aac) 的简单 VC

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var audioRecorder: AVAudioRecorder?

    override func viewDidLoad() {
        super.viewDidLoad()
        verifyRecordPermission()
    }

    @IBAction func recordButonAction(_ sender: UIButton) {
        if audioRecorder?.isRecording == true {
            audioRecorder?.stop()
            sender.setTitle("Record", for: .normal)
        }
        else {
            startRecording()
            sender.setTitle("Stop", for: .normal)
        }
    }

    func verifyRecordPermission() {
        let permission = AVAudioSession.sharedInstance().recordPermission()
        switch permission {
        case .denied:
            print("recording not allowed")
        case .granted:
            print("recording allowed")
        default:
            AVAudioSession.sharedInstance().requestRecordPermission() { (granted) -> Void in
                print("recording granted:\(granted)")
            }
        }
    }

    func startRecording() {
        guard AVAudioSession.sharedInstance().recordPermission() == .granted else {
            return
        }

        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let audioUrl = URL(fileURLWithPath: "\(documentsPath)/audio.aac")
        let session = AVAudioSession.sharedInstance()
        do {
            try session.setCategory(AVAudioSessionCategoryRecord)
            try session.setActive(true)
            try audioRecorder = AVAudioRecorder(url: audioUrl,
                                                settings: [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                                                           AVSampleRateKey: 24000.0,
                                                           AVNumberOfChannelsKey: 1 as NSNumber,
                                                           AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue])
        } catch {
            // handle error...
            return
        }

        guard let audioRecorder = audioRecorder else {
            return

        }
        audioRecorder.prepareToRecord()
        audioRecorder.record()
    }

    func stop(session: AVAudioSession = AVAudioSession.sharedInstance()) {
        audioRecorder?.stop()
        audioRecorder = nil
        do {
            try session.setActive(false)
        } catch {
            // handle session errors
        }
    }
}

关于ios - AVAudioRecorder 录制流式音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18297522/

相关文章:

objective-c - 如何将 NSData 加载到 AVPlayerItem 中?

ios - 带有 AVPlayer 的多个视频

objective-c - 在 swift3.2/4 中正确解除分配 MTAudioProcessingTapRef

ios - 重用 UITableViewHeaderFooterView 时出现 NSAutoresizingMaskLayoutConstraint 错误

iphone - iPhone 的翻译?

ios - UITableView 索引

iphone - iPhone 上的 AVAudioPlayer 声音意外停止循环 : How to debug?

Objective-C -- NSObject isEqual, vs. == 比较?

iOS AudioUnits 通过

ios - 网络事件期间音频播放问题