在单独的类中录制和播放语音 (Swift3)

Posted

技术标签:

【中文标题】在单独的类中录制和播放语音 (Swift3)【英文标题】:Record And Play Voice in Separate Class (Swift3) 【发布时间】:2017-05-27 04:53:22 【问题描述】:

我使用了许多用于录制播放声音的代码,但其中大多数不在 swift3 中,并且它们在我的应用程序中不起作用。

This 代码有效,但我想创建一个独立于视图控制器的类,用于录制播放的声音。提到的github代码也很复杂,我正在寻找一个简化的代码。

更新:

录制后,当我检查录制的文件是否存在时,该文件不存在,并在 appDelegate 上引发 EXC_BAD_ACCESS 错误。

怎么了?

任何建议将不胜感激。

【问题讨论】:

【参考方案1】:

尝试用线录音频

let isRec = AudioManager.shared.record(fileName: "rec")

如果 isRec 返回 true,则记录正在发生,否则不会。 完成录制使用:let recordedURL = AudioManager.shared.finishRecording()

要播放录制的文件,请将上面的 url 发送到管理器类中的 setupUpPlayer() 函数

不要忘记在代码 sn-p 下方使用扩展代码 sn-ps,它们是 AVAudioRecorderAVAudioPlayer 的委托函数

import Foundation

import AVFoundation

class AudioManager: NSObject 

static let shared = AudioManager()

var recordingSession: AVAudiosession?
var recorder: AVAudioRecorder?
var meterTimer: Timer?
var recorderApc0: Float = 0
var recorderPeak0: Float = 0
//PLayer
var player: AVAudioPlayer?
var savedFileURL: URL?

func setup() 
    recordingSession = AVAudioSession.sharedInstance()
    do 
        try recordingSession?.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)
        try recordingSession?.setActive(true)
        recordingSession?.requestRecordPermission( (allowed) in
            if allowed 
                print("Mic Authorised")
             else 
                print("Mic not Authorised")
            
        )
     catch 
        print("Failed to set Category", error.localizedDescription)
    


func record(fileName: String) -> Bool 
    setup()
    let url = getUserPath().appendingPathComponent(fileName + ".m4a")
    let audioURL = URL.init(fileURLWithPath: url.path)
    let recordSettings: [String: Any] = [AVFormatIDKey: kAudioFormatMPEG4AAC,
                                         AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue,
                                         AVNumberOfChannelsKey: 2,
                                         AVSampleRateKey: 44100.0]
    do 
        recorder = try AVAudioRecorder.init(url: audioURL, settings: recordSettings)
        recorder?.delegate = self
        recorder?.isMeteringEnabled = true
        recorder?.prepareToRecord()
        recorder?.record()
        self.meterTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block:  (timer: Timer) in
            //Update Recording Meter Values so we can track voice loudness
            if let recorder = self.recorder 
                recorder.updateMeters()
                self.recorderApc0 = recorder.averagePower(forChannel: 0)
                self.recorderPeak0 = recorder.peakPower(forChannel: 0)
            
        )
        savedFileURL = url
        print("Recording")
        return true
     catch 
        print("Error Handling", error.localizedDescription)
        return false
    


func getUserPath() -> URL 
    return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]


func finishRecording() -> String 
    recorder?.stop()
    self.meterTimer?.invalidate()
    var fileURL: String?
    if let url: URL = recorder?.url 
        fileURL = String(describing: url)
    
    return /fileURL

//Player
func setupPlayer(_ url: URL) 
    do 
        try player = AVAudioPlayer.init(contentsOf: url)
     catch 
        print("Error1", error.localizedDescription)
    
    player?.prepareToPlay()
    player?.play()
    player?.volume = 1.0
    player?.delegate = self



    //MARK:- Audio Recorder Delegate

    extension AudioManager: AVAudioRecorderDelegate 

    func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) 

        print("AudioManager Finish Recording")

    
    func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) 
        print("Encoding Error", /error?.localizedDescription)
    



    //MARK:- Audio Player Delegates

    extension AudioManager: AVAudioPlayerDelegate 

       func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, 
   successfully flag: Bool) 

           player.stop()

           print("Finish Playing")

       

       func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, 
    error: Error?) 

            print(/error?.localizedDescription)

        

    

【讨论】:

谢谢你的回答真的帮助了我 我用这个来播放录音文件: let path = URL(string: AudioManager.shared.getUserPath().absoluteURL.description + "rec.m4a" )! AudioManager.shared.setupPlayer(路径)

以上是关于在单独的类中录制和播放语音 (Swift3)的主要内容,如果未能解决你的问题,请参考以下文章

在 android 的视频视图中播放视频时不录制语音

在模拟器中录制和播放声音效果很好,但在我的 iPhone 中却不行

在录制语音时在 Matlab 中播放 wav

录制语音时如何播放背景音乐?

将预先录制的音频播放到语音通话中

屏幕录制时没有声音是怎么回事