【问题标题】:Why is the duration of recorded audio negative? (iOS / Swift)为什么录制的音频的持续时间是负数? (iOS / 斯威夫特)
【发布时间】:2016-05-20 09:16:42
【问题描述】:

我想用我的应用录制 M4A 格式的音频文件,然后播放。 我第一次尝试录制时,我的应用程序没有这样做,但在第二次、第三次和进一步的尝试中却神秘地完美无缺。 打印 audioPlayer.duration 的值表明第一个录音总是有一个负值!为什么??正如预期的那样,所有其他音频记录都具有以秒为单位的正持续时间值。 我总是对所有记录使用相同的文件名,覆盖以前的记录。我做错了什么?

    func initializeRecorder(){
    let recordSettings = [AVSampleRateKey : NSNumber(float: Float(32000.0)),
                                  AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)), 
                AVNumberOfChannelsKey : NSNumber(int: 1),
                AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Low.rawValue)) ]

            var error : NSError?
            let audioSession = AVAudioSession.sharedInstance()
            do {
                try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
                try audioRecorder = AVAudioRecorder(URL: self.directoryURL(),
                                                    settings: recordSettings)
                audioRecorder.delegate = self
                audioRecorder.meteringEnabled = true
                audioRecorder.prepareToRecord()
                print("M4a Recorder Initialized - OK...")
            } catch {
                print("Init Error")
            }
        }

    func recordSpeechM4A(){
        if !audioRecorder.recording {
            isCapioRecording=true
            let audioSession = AVAudioSession.sharedInstance()
            do {
                try audioSession.setActive(true)
                print("**** RECORDING ****")
                audioRecorder.record()
            } catch {
                print("RECORDING ERROR")
            }
        }
    }

func stopRecording(){
        audioRecorder.stop()
}
func playRecordedAudio(){
        if (!audioRecorder.recording){
            do {
                try audioPlayer = AVAudioPlayer(contentsOfURL: audioRecorder.url)
                audioPlayer.play()

                print("PLAYING AUDIO...: "+audioRecorder.url.description)
                print("Audio duration: "+audioPlayer.duration.description)
                //DURATION OF THE 1st RECORDING IS ALWAYS NEGATIVE!!!
            } catch {
                print("AUDIO PLAYBACK ERROR")
            }
        }
    }

// I ALWAYS USE THE SAME FILENAME!
func directoryURL() -> NSURL {
        let fileManager = NSFileManager.defaultManager()
        let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
        filepath = urls[0]
        let documentDirectory = urls[0] as NSURL
        print("STORAGE DIR: "+documentDirectory.description)
        let soundURL = documentDirectory.URLByAppendingPathComponent("sound.m4a") //.m4a
        print("SAVING FILE: "+soundURL.description)
        return soundURL
    }

【问题讨论】:

  • 你有解决办法吗?
  • 嗨@BhargavB.Bajani 自从这个问题以来我已经完成了大量的编码,如果我的记忆在这里失败,很抱歉。我记得 iOS 对它可以处理的音频格式非常敏感。一个小小的变化,一切都崩溃了。深入研究我的项目代码,我注意到我的代码在这个问题上只有一个区别:从 32000 更改采样率,并使用 16000。让我知道它是否有效。祝你好运。
  • 嗨@josh ...实际上我没有应用任何设置。我也通过应用设置进行检查,但无法正常工作。同样的问题
  • @BhargavB.Bajani 您应该应用设置,否则,将使用默认值并且可能会失败。
  • 也通过应用值进行检查,但问题未解决

标签: ios swift avfoundation avaudioplayer avaudiorecorder


【解决方案1】:

我有一个类似的问题,并已通过以下 sn-p 修复它:

audioRecorder.record()
audioRecorder.record()

通过调用此函数两次,尽管这可能不是理想的解决方案。
不知何故,它修复了它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多