在iOS Playground上使用AVFoundation的Recorder,但recorder.record()始终返回false

时间:2019-02-12 06:48:30

标签: ios swift avfoundation swift-playground avaudiorecorder

反正可以在iOS Playground上使用记录器吗?运行时,它会征得我使用我的麦克风的许可,并且录音机实例似乎可以运行,但是,我不能使用record()函数。 我当前的环境是iOS 12.1.4和Swift 4.2。

import UIKit
import PlaygroundSupport
import AVFoundation

class VolumnMeterViewController : UIViewController, AVAudioRecorderDelegate {

    let temporaryDirectoryToKeepRecords = FileManager.default.temporaryDirectory
    var recordButton = UIButton()
    let backgroundView = UIView()
    var recordNumber = 1

    //    var recordingSession: AVAudioSession!
    var recorder: AVAudioRecorder!

    override func loadView() {

        backgroundView.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

        recordButton.setTitle("Record", for: [])
        recordButton.setTitleColor(#colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1), for: [])
        recordButton.addTarget(self, action: #selector(recordButtonTapped), for: .touchUpInside)
        recordButton.translatesAutoresizingMaskIntoConstraints = false
        backgroundView.addSubview(recordButton)

        temporaryDirectoryToKeepRecords

        self.view = backgroundView

        NSLayoutConstraint.activate([
            recordButton.widthAnchor.constraint(equalToConstant: 200),
            recordButton.heightAnchor.constraint(equalToConstant: 60),
            recordButton.centerXAnchor.constraint(equalTo: backgroundView.centerXAnchor),
            recordButton.centerYAnchor.constraint(equalTo: backgroundView.centerYAnchor)
            ])
    }

    @objc func recordButtonTapped() {
        if recorder == nil {

            recordNumber += 1

            let recordFileName = temporaryDirectoryToKeepRecords.appendingPathComponent("record\(recordNumber).m4a")

            recordButton.setTitle("Stop", for: [])
            let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 44100, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue]

            do {
                let recorder = try AVAudioRecorder(url: recordFileName, settings: settings)

                // ❗️❗️❗️ Here's the problem. It always return false, assuming to be true.
                recorder.record()
            } catch {
                print(error)
            }
        } else {
            recordButton.setTitle("Record", for: [])
            recorder.stop()
            recorder = nil
        }
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = VolumnMeterViewController()

或者是否有某种方式不通过录音来跟踪麦克风的音量,而只是实时检测而不存储录音文件? 非常感谢!

1 个答案:

答案 0 :(得分:0)

这终于是iOS Playground的工作代码,非常感谢AtulParmar!

String _generateSignature(String method, String url, Map<String, dynamic> params) {
    // Sort the parameters
    var sortedParams = SplayTreeMap.from(params);

    // Concatenate the sortedParams with '&'
    String concatenatedParams = sortedParams.keys.map((key) {
      return '$key=${sortedParams[key]}';
    }).join('&');

    // encode the sorted and concatenated params string
    var encodedParams = Uri.encodeComponent(concatenatedParams);

    var encodedUrl = Uri.encodeComponent(url);

    String baseString = '$method&$encodedUrl&$encodedParams';

    String signingKey = '${Uri.encodeComponent(SHARED_SECRET)}&';

    var hmac = Hmac(sha1, signingKey.codeUnits);

    return Uri.encodeComponent(base64Encode(hmac.convert(baseString.codeUnits).bytes));
  }