gpt4 book ai didi

ios - 快速录制音频并将录制的音频传递到下一个 View Controller

转载 作者:行者123 更新时间:2023-12-03 02:02:47 24 4
gpt4 key购买 nike

我正在尝试记录音频,并将记录的音频传递给下一个 View Controller 。这是我录制音频的代码

class RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate {

@IBOutlet weak var recording: UILabel!
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var stopButton: UIButton!

var audioRecorder:AVAudioRecorder!

var recordedAudio : RecordedAudio!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {
// enables record button
// hides the stop button
recordButton.enabled = true
stopButton.hidden = true

}
@IBAction func recordAudio(sender: UIButton) {
//Shows recording label
recording.hidden = false

//diabling record button
recordButton.enabled = false
stopButton.hidden = false

//Filepath Creation
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String

let currentDateTime = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "ddMMyyyy-HHmmss"
let recordingName = formatter.stringFromDate(currentDateTime)+".wav"
let pathArray = [dirPath, recordingName]
let filePath = NSURL.fileURLWithPathComponents(pathArray)
println(filePath)

// Recording Session

var session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)

audioRecorder = AVAudioRecorder(URL: filePath, settings: nil, error: nil)

audioRecorder.delegate = self
audioRecorder.meteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()

}



func audioRecorderDidFinishRecording(recorder: AVAudioRecorder!, successfully flag: Bool) {
// ToDo create recorded audio file
if(flag)
{ recordedAudio = RecordedAudio()

recordedAudio.filepathURL = recorder.url

recordedAudio.title = recorder.url.lastPathComponent

// ToDo Perform segue

self.performSegueWithIdentifier("stopRecording", sender: recordedAudio)
}
else {
println("Recording was unsuccessfull")
stopButton.hidden = true
recordButton.enabled = true

}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue == "stopRecording") {

let PlaySoundsVC:PlaySoundsViewController = segue.destinationViewController as! PlaySoundsViewController
let data = sender as! RecordedAudio

PlaySoundsVC.receivedAudio = data
}
}


@IBAction func stopAudio(sender: UIButton) {
// Hides recording
recording.hidden = true
audioRecorder.stop()
var audioSession = AVAudioSession.sharedInstance()
audioSession.setActive(false, error: nil)

}
}

我的Model课是
import Foundation

class RecordedAudio : NSObject{

var filepathURL :NSURL!
var title : String!
}

这是我的第二个ViewController捕获数据并使用它的方式,
    class PlaySoundsViewController: UIViewController {

var audioPlayer: AVAudioPlayer!
var receivedAudio: RecordedAudio!

func rateplay (rtt : Float32) {
audioPlayer.stop()

audioPlayer.rate = rtt

audioPlayer.currentTime = 0.0
audioPlayer.play()

}

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
// if var filePath = NSBundle.mainBundle().pathForResource("movie_quote", ofType: "mp3")
// {
// // if path is there for mp3
// let filepathurl = NSURL.fileURLWithPath(filePath)
//
// // println(receivedAudio.title)
//
// }
// else {
// println("Path is empty")
//
// }
audioPlayer = AVAudioPlayer(contentsOfURL: receivedAudio.filepathURL, error: nil)
audioPlayer.enableRate = true




}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



@IBAction func playSlow(sender: UIButton) {
// play sloooowllyyyyy
audioPlayer.stop()

audioPlayer.rate = 0.5

audioPlayer.currentTime = 0.0

audioPlayer.play()

}
@IBAction func playFast(sender: UIButton) {
rateplay(1.5)

}

@IBAction func stopAudio(sender: UIButton) {
audioPlayer.stop()
}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

直到我添加以下代码,
audioPlayer = AVAudioPlayer(contentsOfURL: receivedAudio.filepathURL, error: nil)
audioPlayer.enableRate = true

我能够移动到第二个场景,这意味着音频已成功录制。但是,一旦我访问“receivedAudio.filepathURL”之类的数据,就会收到错误消息,
fatal error: unexpectedly found nil while unwrapping an Optional value

最佳答案

RecordSoundsViewController的prepareForSegue函数中,您需要编写segue.identifier == "stopRecording"作为条件。

目前您有segue == "stopRecording"

编码愉快!

关于ios - 快速录制音频并将录制的音频传递到下一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30147459/

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