WebRTC没有任何内部API可以启动或停止记录。我们可以尝试使用AVAudioSession
代替。我正在开发具有接收器录制功能的音频流应用程序。
我被困在用户想要在接收方录制音频流的位置。
下面是我的代码初始化
var engine = AVAudioEngine()
var recordingFile: AVAudioFile?
var audioPlayer: AVAudioPlayer?
let player = AVAudioPlayerNode()
var isRecording: Bool = false
Initialise AudioEngine] >>
] >>func initializeAudioEngine() { let input = self.engine.inputNode let format = input.inputFormat(forBus: 0) self.engine.attach(self.player) let mainMixerNode = self.engine.mainMixerNode self.engine.connect(input, to:mainMixerNode, format: format) self.engine.prepare() do { try self.engine.start() self.startRecording() } catch (let error) { print("START FAILED", error) } }
开始录制
] >>func startRecording() { self.createRecordingFile() self.engine.mainMixerNode.installTap(onBus: 0, bufferSize: 1024, format: self.engine.mainMixerNode.outputFormat(forBus: 0)) { (buffer, time) -> Void in do { self.isRecording = true try self.recordingFile?.write(from: buffer) } catch (let error) { print("RECORD ERROR", error); } return } }
创建缓冲区
private func createBuffer(forFileNamed fileName: String) -> AVAudioPCMBuffer? { var res: AVAudioPCMBuffer? if let fileURL = Bundle.main.url(forResource: fileName, withExtension: "caf") { do { let file = try AVAudioFile(forReading: fileURL) res = AVAudioPCMBuffer(pcmFormat: file.processingFormat, frameCapacity:AVAudioFrameCount(file.length)) if let _ = res { do { try file.read(into: res!) } catch (let error) { print("ERROR read file", error) } } } catch (let error) { print("ERROR file creation", error) } } return res }
停止记录
] >>func stopRecording() { self.engine.mainMixerNode.removeTap(onBus: 0) }
我正在尝试使用耳机进行录制,但是不起作用
我正在开发具有接收器录制功能的音频流应用程序。我被困在用户想要在接收器端录制音频流的地方。下面是我的代码...
WebRTC没有任何内部API可以启动或停止记录。我们可以尝试使用AVAudioSession
代替。首次设置音频会话
func setUPAudioSession() -> Bool { let audiosession = AVAudioSession() do { try audiosession.setCategory(AVAudioSessionCategoryPlayAndRecord) } catch(let error) { print("--> \(error.localizedDescription)") } do { try audiosession.setActive(true) } catch (let error) { print("--> \(error.localizedDescription)") } return audiosession.isInputAvailable; }
[Setup之后,音频会话现在开始如下记录
func startRecording() -> Bool { var settings: [String: Any] = [String: String]() settings[AVFormatIDKey] = kAudioFormatLinearPCM settings[AVSampleRateKey] = 8000.0 settings[AVNumberOfChannelsKey] = 1 settings[AVLinearPCMBitDepthKey] = 16 settings[AVLinearPCMIsBigEndianKey] = false settings[AVLinearPCMIsFloatKey] = false settings[AVAudioQualityMax] = AVEncoderAudioQualityKey //Create device directory where recorded file will be save automatically let searchPaths: [String] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .allDomainsMask, true) let documentPath_ = searchPaths.first let pathToSave = "\(documentPath_)/\(dateString)" let url: URL = URL(pathToSave) recorder = try? AVAudioRecorder(url: url, settings: settings) // Initialize degate, metering, etc. recorder.delegate = self; recorder.meteringEnabled = true; recorder?.prepareToRecord() if let recordIs = recorder { return recordIs.record() } return false }
播放录制的文件
func playrecodingFile() { //Get the path of recorded file saved in previous method let searchPaths: [String] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .allDomainsMask, true) let documentPath_ = searchPaths.first let fileManager = FileManager.default let arrayListOfRecordSound: [String] if fileManager.fileExists(atPath: recordingFolder()) { let arrayListOfRecordSound = try? fileManager.contentsOfDirectory(atPath: documentPath_) } let selectedSound = "\(documentPath_)/\(arrayListOfRecordSound.first)" let url = URL.init(fileURLWithPath: selectedSound) let player = try? AVAudioPlayer(contentsOf: url) player?.delegate = self; try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) player?.prepareToPlay() player?.play() }
停止记录
func stopRecording() { recorder?.stop() }
pauseRecording
func pauseRecording() { recorder?.pause() }
停止记录
func stopRecording() { recorder?.stop() }
WebRTC没有任何内部API可以启动或停止记录。我们可以尝试使用AVAudioSession
代替。它可以工作,因为一旦设置
let audiosession = AVAudioSession()