gpt4 book ai didi

ios - 使用模拟器显示保存的路径,但在使用实际智能 watch 设备(录音机)进行测试时不显示

转载 作者:行者123 更新时间:2023-11-29 05:44:21 29 4
gpt4 key购买 nike

希望你能帮帮我。我尝试编写一个简单的小程序,录制 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)

但是,当我在实际的智能 watch 上运行它时,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)
}
}

}

最佳答案

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

关于ios - 使用模拟器显示保存的路径,但在使用实际智能 watch 设备(录音机)进行测试时不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288040/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com