许多录音类 APP 都提供录音回放功能,大家在做这类 APP 的时候也经常会遇到这个需求。当大家用以前的套路在 iOS 8 上录音的时候,在模拟器上跑得挺好的,但是一上真机就跪了,为什么?因为真机底层是真实的硬件,跟模拟器还是有一些差别的,例如真机支持硬件解码等等。
在 iOS 8 上,录音并播放需要在录音前就申请“录音并播放”的 session(swift):
func setSessionPlayAndRecord() {
let session:AVAudioSession = AVAudioSession.sharedInstance()
var error: NSError?
if !session.setCategory("AVAudioSessionCategoryPlayAndRecord", withOptions: AVAudioSessionCategoryOptions.DefaultToSpeaker, error:&error) {
println("could not set session category")
if let e = error {
println(e.localizedDescription)
}
}
if !session.setActive(true, error: &error) {
println("could not make session active")
if let e = error {
println(e.localizedDescription)
}
}
}
之后再开始录音,等录音结束后就可以正常播放啦。
转载于:https://www.cnblogs.com/Cheetah-yang/p/4669907.html