Swift 通过segue传递音频文件
我一直在向second view控制器传递音频文件。我的代码如下:Swift 通过segue传递音频文件,swift,Swift,我一直在向second view控制器传递音频文件。我的代码如下: import UIKit import AVFoundation class recordSoundsViewController: UIViewController, AVAudioRecorderDelegate { var audioRecorder: AVAudioRecorder! var recordedAudioURL: URL! @IBOutlet weak var tapToRecord: UILabel
import UIKit
import AVFoundation
class recordSoundsViewController: UIViewController, AVAudioRecorderDelegate {
var audioRecorder: AVAudioRecorder!
var recordedAudioURL: URL!
@IBOutlet weak var tapToRecord: UILabel!
@IBOutlet weak var recording: UIButton!
@IBOutlet weak var stopRecording: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
stopRecording.isEnabled = false
}
// when record audio is pressed
@IBAction func recordAudio(_ sender: Any) {
tapToRecord.text = "Recording in progress"
stopRecording.isEnabled = true
recording.isEnabled = false
// recording audio
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
let recordingName = "recordedVoice.wav"
let pathArray = [dirPath, recordingName]
let filePath = URL(string: pathArray.joined(separator: "/"))
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)
try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
}
// when stop button pressed
@IBAction func stopRecord(_ sender: Any) {
tapToRecord.text = "Tap to Record"
recording.isEnabled = true
stopRecording.isEnabled = false
audioRecorder.stop()
let audiosession = AVAudioSession.sharedInstance()
try! audiosession.setActive(false)
}
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
if flag {
performSegue(withIdentifier: "stopRecording", sender: audioRecorder.url)
} else {
print("recording was not successful")
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "stopRecording" {
var playSoundsVC = segue.destination as! playSoundsViewController
let recordedAudioURL = sender as! URL
playSoundsVC.recordedAudioURL = recordedAudioURL
}
}
行playSoundsVC.recordedaudiour=recordedaudiour抛出错误:
“playSoundsViewController”类型的值没有成员“recordedAudioURL”
有人能指出错误吗?如果要为其赋值,必须首先在目标视图控制器中创建
recordedAudioURL
属性。因此:
class playSoundsViewController: UIViewController {
var recordedAudioURL: URL?
}
现在您可以给它一个值。您需要共享目标视图控制器的代码。没有该代码,您怎么能期望我们提供帮助?
playSoundsViewController
是您的类。请修复它,使其具有所需的属性。