0

我希望你能帮助我。我尝试编写一个简单的小程序,将音频录制 5 秒并将其保存在文档目录中。

我已经用模拟器对其进行了测试,我可以在输出中看到路径,还可以看到sound.wav创建的文件。我可以打开那个sound.wav文件。

这是我使用模拟器获得的路径:

(file:///Users/Username/Library/Developer/CoreSimulator/Devices/3721A435-C765-4C8D-BA59-04477B070F80/data/Containers/Data/PluginKitPlugin/476CD646-D534-46C0-B5D2-88D7429DFF8F/Documents/sound.wav)

但是,当我在实际的智能手表上运行它时,Xcode 的输出中没有任何内容。即使我将打印命令放入代码中,它们也没有显示出来。我不知道问题是什么。

我还想获取音频文件并将其发送到服务器(mysql)。如何获得实际的音频文件,wav在这种情况下是文件?以及如何从应该保存音频文件的文档目录中获取路径。

这是我的代码的样子:

我希望你能帮助我。非常感谢。

import WatchKit
import Foundation
import AVFoundation

class InterfaceController: WKInterfaceController, AVAudioRecorderDelegate{
    @IBOutlet weak var btn: WKInterfaceButton!
    var recordingSession : AVAudioSession!
    var audioRecorder : AVAudioRecorder!
    var settings = [String : Any]()

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)
        recordingSession = AVAudioSession.sharedInstance()

        do{
            try recordingSession.setCategory(AVAudioSession.Category.playAndRecord)
            try recordingSession.setActive(true)
            recordingSession.requestRecordPermission(){[unowned self] allowed in
            DispatchQueue.main.async {
                if allowed{
                    print("Allow")
                } else{
                    print("Don't Allow")
                }
            }
        }
    }
        catch{
            print("failed to record!")
        }
        // Configure interface objects here.

   // Audio Settings

        settings = [
            AVFormatIDKey:Int(kAudioFormatLinearPCM),
            AVSampleRateKey:44100.0,
            AVNumberOfChannelsKey:1,
            AVLinearPCMBitDepthKey:8,
            AVLinearPCMIsFloatKey:false,
            AVLinearPCMIsBigEndianKey:false,
            AVEncoderAudioQualityKey:AVAudioQuality.max.rawValue

            ]

    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }


    func directoryURL() -> NSURL? {
        let fileManager = FileManager.default
        let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
        let documentDirectory = urls[0] as NSURL
        let soundUrl = documentDirectory.appendingPathComponent("sound.wav")
        print(soundUrl)
        return soundUrl as NSURL?

    }

    func startRecording(){
        let audioSession = AVAudioSession.sharedInstance()

        do{
            audioRecorder = try AVAudioRecorder(url: self.directoryURL()! as URL,
            settings: settings)
            audioRecorder.delegate = self
            audioRecorder.prepareToRecord()
            audioRecorder.record(forDuration: 5.0)

        }
        catch {
            finishRecording(success: false)
        }

        do {
            try audioSession.setActive(true)
            audioRecorder.record()
        } catch {
        }
    }

    func finishRecording(success: Bool) {
        audioRecorder.stop()
        if success {
            print(success)
        } else {
            audioRecorder = nil
            print("Somthing Wrong.")
        }
    }


    @IBAction func recordAudio() {
        if audioRecorder == nil {
            print("Pressed")
            self.btn.setTitle("Stop")
            self.btn.setBackgroundColor(UIColor(red: 119.0/255.0, green: 119.0/255.0, blue: 119.0/255.0, alpha: 1.0))
            self.startRecording()


        } else {
            self.btn.setTitle("Record")
            print("Pressed2")
            self.btn.setBackgroundColor(UIColor(red: 221.0/255.0, green: 27.0/255.0, blue: 50.0/255.0, alpha: 1.0))
            self.finishRecording(success: true)

        }
    }

    func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
        if !flag {
            finishRecording(success: false)
        }
    }

}
4

2 回答 2

0

只是出于好奇,我想看看输出控制台中是否会根据另一个应用程序打印某些内容。另一个应用程序将语句打印到输出控制台。所以,我猜这个问题与xcode无关。在此处输入图像描述

于 2019-05-24T09:25:22.957 回答
0

模拟器和实际设备的文件路径不同。对于模拟器,它使用您的 MacOS 文件存储,而在实际设备中,它将使用设备的存储。

于 2019-05-24T07:53:24.323 回答