我尝试记录,在这里可以找到一个很好的答案:
Recording audio in Swift
我可以使它工作。但是现在我想知道如何播放录制的音频。从记录中,我已经有了通用的var audioRecorder,并定义了URL路径。所以我尝试了audioRecorder.play(),但它不起作用。
我想问题出在以下事实:全局var audioRecorder是AVAudioRecorder的一个实例,要播放它,它应该是AVAudioPlayer的一个实例吗?这两者之间有何关系?
我不想复制粘贴我想了解的代码。这就是为什么我在这里简化代码。请解释为什么它在此特定代码中不起作用以及如何解决。
(我完成了很多相关的教程。问题是丢失了太多代码。我的问题是要了解这一特定部分的工作原理)
import AVFoundation
var audioRecorder:AVAudioRecorder!
@IBAction func record(sender: AnyObject) {
var audioSession:AVAudioSession = AVAudioSession.sharedInstance()
audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
audioSession.setActive(true, error: nil)
var documents: AnyObject = NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
var str = documents.stringByAppendingPathComponent("recordTest.caf")
var url = NSURL.fileURLWithPath(str as String)
println(url)
audioRecorder = AVAudioRecorder(URL:url, settings: nil, error: nil)
audioRecorder.record()
}
@IBAction func play(sender: AnyObject) {
// this gives the error 'AVAudioRecorder' does not have a member named 'play'
// audioRecorder.play()
}
最佳答案
这是录制音频,然后将其存储到文件然后播放的完整工作代码:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate, AVAudioRecorderDelegate {
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var stopButton: UIButton!
@IBOutlet weak var playButton: UIButton!
var audioPlayer : AVAudioPlayer?
var audioRecorder : AVAudioRecorder?
override func viewDidLoad() {
super.viewDidLoad()
playButton.enabled = false
stopButton.enabled = false
// getting URL path for audio
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docDir = dirPath[0] as! String
let soundFilePath = docDir.stringByAppendingPathComponent("sound.caf")
let soundFileURL = NSURL(fileURLWithPath: soundFilePath)
println(soundFilePath)
//Setting for recorder
let recordSettings = [AVEncoderAudioQualityKey: AVAudioQuality.Min.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey : 2,
AVSampleRateKey: 44100.0]
var error : NSError?
let audioSession = AVAudioSession.sharedInstance()
audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: &error)
if let err = error{
println("audioSession error: \(err.localizedDescription)")
}
audioRecorder = AVAudioRecorder(URL: soundFileURL, settings: recordSettings as [NSObject : AnyObject], error: &error)
if let err = error{
println("audioSession error: \(err.localizedDescription)")
}else{
audioRecorder?.prepareToRecord()
}
}
//record audio
@IBAction func recordAudio(sender: AnyObject) {
if audioRecorder?.recording == false{
playButton.enabled = false
stopButton.enabled = true
audioRecorder?.record()
}
}
//stop recording audio
@IBAction func stopAudio(sender: AnyObject) {
stopButton.enabled = false
playButton.enabled = true
recordButton.enabled = true
if audioRecorder?.recording == true{
audioRecorder?.stop()
}else{
audioPlayer?.stop()
}
}
//play your recorded audio
@IBAction func playAudio(sender: AnyObject) {
if audioRecorder?.recording == false{
stopButton.enabled = true
recordButton.enabled = false
var error : NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: audioRecorder?.url, error: &error)
audioPlayer?.delegate = self
if let err = error{
println("audioPlayer error: \(err.localizedDescription)")
}else{
audioPlayer?.play()
}
}
}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
recordButton.enabled = true
stopButton.enabled = false
}
func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer!, error: NSError!) {
println("Audio Play Decode Error")
}
func audioRecorderDidFinishRecording(recorder: AVAudioRecorder!, successfully flag: Bool) {
}
func audioRecorderEncodeErrorDidOccur(recorder: AVAudioRecorder!, error: NSError!) {
println("Audio Record Encode Error")
}
}
检查THIS示例项目以获取更多信息。在您的代码中,
audioRecorder
用于录制音频,audioPlayer
用于播放音频。那就是为什么audioRecorder
没有属性play()
的原因因此,您不能使用
audioRecorder.play()
。