我很难将音频文件传递给第二个视图控制器。我的代码如下:
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.recordedAudioURL = recordedAudioURL 行引发错误:
'playSoundsViewController'类型的值没有成员'recordedAudioURL“
任何人都可以指出错误吗?
答案 0 :(得分:2)
如果要为其分配值,则必须首先在目标视图控制器中创建recordedAudioURL
属性。因此:
class playSoundsViewController: UIViewController {
var recordedAudioURL: URL?
}
现在你可以给它一个值。