草庐IT

ios - Swift + AVAudioRecorder 录音很安静

coder 2023-09-10 原文

我用 Swift 构建了一个 iOS 应用程序来记录音频剪辑,然后将这些剪辑发送到服务器。我做的每一个录音都很安静。

最初我认为我的问题与 Stack Overflow 上的这个问题类似 - 但在尝试这个解决方案后,我的录音仍然非常安静。

通过扬声器路由音频不会使录音声音更大,如这里所建议的:

https://stackoverflow.com/a/5662478/1037617

请注意,它不能在设备上播放是个问题。问题是录音太安静了。我已经测试了麦克风本身,没问题。

这是我能产生的最佳波形,这几乎是对着麦克风大喊大叫。可以看到,录制的波形非常安静:

有什么方法可以让我的 iOS 应用程序以更大的音量录音吗?

这是我用来录制音频剪辑的代码:

let recorderSettings=[
    AVFormatIDKey            : kAudioFormatLinearPCM,
    AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
    AVEncoderBitRateKey      : 128000,
    AVNumberOfChannelsKey    : 1,
    AVSampleRateKey          : 44100.0
]

let session: AVAudioSession = AVAudioSession.sharedInstance()
var error: NSError?
if session.respondsToSelector("requestRecordPermission:") {
    AVAudioSession.sharedInstance().requestRecordPermission( { (granted:Bool) -> Void in
        if !granted {
            println("permission not granted")
        }else{
            println("permission granted")

            if !session.setCategory(AVAudioSessionCategoryRecord, error: &error) { // also tried PlaybackAndRecord
                println("could not set sesssion category")
                if let e = error {
                    println(e.localizedDescription)
                }
            }

            if !session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error: &error) {
                println("could not override output audio")
                if let e = error {
                    println(e.localizedDescription)
                }
            }

            if !session.setActive(true, error: &error) {
                println("could not make active")
                if let e = error {
                    println(e.localizedDescription)
                }
            }

            self.currentFilename = "xxxx.wav"
            let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
            let docsDir: AnyObject=dirPaths[0]
            self.recordedFilePath = docsDir.stringByAppendingPathComponent(self.currentFilename)
            self.recordedFileURL = NSURL(fileURLWithPath: self.recordedFilePath)

            self.recorder = AVAudioRecorder(URL:self.recordedFileURL, settings: self.recorderSettings, error: &error)

            if let e = error {
                println(e)
                println(e.localizedDescription)
                var err = NSError(domain: NSOSStatusErrorDomain, code: e.code, userInfo: nil)
                println(err.description)
            }else{

                self.recorder?.delegate = self
                self.recorder?.meteringEnabled = true
                self.recorder?.prepareToRecord()

                self.recorder?.record()

                self.meterTimer = NSTimer.scheduledTimerWithTimeInterval(0.1,
                    target: self,
                    selector: "updateRecordAudioMeter:",
                    userInfo: nil,
                    repeats: true)

            }
        }
    })
}

最佳答案

录制音频时,将 Audio Session 设置为AVAudioSessionCategoryPlayAndRecord 或只是AVAudioSessionCategoryRecord。完成后,将其设置回 AVAudioSessionCategoryPlayback

关于ios - Swift + AVAudioRecorder 录音很安静,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28070773/

有关ios - Swift + AVAudioRecorder 录音很安静的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  7. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  8. arrays - Ruby 数组 += vs 推送 - 2

    我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“

  9. += 的 Ruby 方法 - 2

    有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=

  10. ruby - Sinatra + Heroku + Datamapper 使用 dm-sqlite-adapter 部署问题 - 2

    出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t

随机推荐