简体   繁体   English

无明显原因的音量降低

[英]Sound volume decreasing for no apparent reason

I have an iOS app using SwiftUI.我有一个使用 SwiftUI 的 iOS 应用程序。 It handles a few sound files and performs some audio recording.它处理一些声音文件并执行一些录音。 This is the function doing the recording work:这是 function 做录音工作:

func recordAudio(to file: String, for duration: TimeInterval) {
    let audioSession:AVAudioSession = AVAudioSession.sharedInstance()
    
    do {try audioSession.setCategory(.playAndRecord, mode: .default)
        try audioSession.setActive(true)
        
        let audioFilename = getDocumentsDirectory().appendingPathComponent(file+".m4a"),
            audioURL = URL(fileURLWithPath: audioFilename),
            settings = [
                AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                AVSampleRateKey: 44100,
                AVNumberOfChannelsKey: 2,
                AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
                ]

        audioRecorder = try AVAudioRecorder(url: audioURL, settings: settings)
        audioRecorder.delegate = self
        audioRecorder.record(forDuration: TimeInterval(2.0))
    } catch let error as NSError {
        print("Failed -- Recording !!! -> \(error)")
    }
}

At this point, it basically works, but there is a strange behaviour that I neither understand nor like.至此,它基本上可以工作了,但是有一个我既不理解也不喜欢的奇怪行为。

Here is the problem:这是问题所在:

When I start the app and play a sound file, the volume is right for my taste.当我启动应用程序并播放声音文件时,音量适合我的口味。 Then without ever adjusting the volume I perform some recording (using the function above).然后在不调整音量的情况下进行一些录音(使用上面的 function)。 Finally after the recording is done, I go back to the file I played just before and play it again;最后录制完成后,我将go 回到我刚才播放的文件,再次播放; the volume has mysteriously gone down, without me knowing why.音量神秘地下降了,我不知道为什么。

Is there something in my function that could explain that?我的 function 中有什么可以解释的吗? Or some other cause that someone could think of?还是有人能想到的其他原因?

If I restart the app, the volume automatically goes back to normal.如果我重新启动应用程序,音量会自动恢复正常。 For information, I am using iOS 14.4.2 and Xcode 12.4.有关信息,我正在使用 iOS 14.4.2 和 Xcode 12.4。

The audio session will be a decreased volume during playback after recording in .playAndRecord mode..playAndRecord模式录制后,音频 session 将在播放期间降低音量。 After recording, explicitly set to something like .playback to get the volume you're expecting.录制后,明确设置为类似.playback以获得您期望的音量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM