任何人帮我在Iphone中同时录制和播放音频。
答案 0 :(得分:16)
您可以使用Audio Unit RemoteIO或Audio Queue API在iOS设备上同时播放和录制(第一代Touch除外)。这些是较低级别的API,您必须自己处理传出和传入PCM样本的传入缓冲区。
请参阅Apple的aurioTouch示例应用程序,例如代码。
答案 1 :(得分:9)
您可以从AVFoundation框架中获取用途。它有AVAudioPlayer播放音频文件和AVAudioRecorder来录制。 您必须记住,录音机将仅使用麦克风进行录音。 因此,同时播放音频文件并录制它取决于麦克风将如何感知播放的音频。
答案 2 :(得分:2)
请同时检查aurioTouch apple示例代码以进行音频录制和播放
答案 3 :(得分:0)
希望这可以帮助一些人......我制作了一个记录音频的应用程序,比如像Pandora这样的应用程序,可以播放音频。运行/播放音频应用程序,运行AudioMic,录制,关闭音频应用程序声音,返回并播放AudioMic录制的音频。耶!
答案 4 :(得分:0)
要在iOS中录制播放音频文件,您可以使用AVFoundation框架。使用下面的快速代码来录制和播放音频。 请记住,录音机将使用麦克风录制音频,因此请在设备上测试此代码。
import UIKit
import AVFoundation
extension String {
func stringByAppendingPathComponent(path: String) -> String {
let nsSt = self as NSString
return nsSt.stringByAppendingPathComponent(path)
}
}
class ViewController: UIViewController, AVAudioPlayerDelegate, AVAudioRecorderDelegate{
var audioPlayer : AVAudioPlayer!
var audioRecorder : AVAudioRecorder!
@IBOutlet var recordButton : UIButton!
@IBOutlet var playButton : UIButton!
@IBOutlet var stopButton : UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.recordButton.enabled = true
self.playButton.enabled = false
self.stopButton.enabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//MARK: UIButton action methods
@IBAction func playButtonClicked(sender : AnyObject){
let dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(dispatchQueue, {
if let data = NSData(contentsOfFile: self.audioFilePath())
{
do{
self.audioPlayer = try AVAudioPlayer(data: data)
self.audioPlayer.delegate = self
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
}
catch{
print("\(error)")
}
}
});
}
@IBAction func stopButtonClicked(sender : AnyObject){
if let player = self.audioPlayer{
player.stop()
}
if let record = self.audioRecorder{
record.stop()
let session = AVAudioSession.sharedInstance()
do{
try session.setActive(false)
}
catch{
print("\(error)")
}
}
}
@IBAction func recordButtonClicked(sender : AnyObject){
let session = AVAudioSession.sharedInstance()
do{
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try session.setActive(true)
session.requestRecordPermission({ (allowed : Bool) -> Void in
if allowed {
self.startRecording()
}
else{
print("We don't have request permission for recording.")
}
})
}
catch{
print("\(error)")
}
}
func startRecording(){
self.playButton.enabled = false
self.recordButton.enabled = false
self.stopButton.enabled = true
do{
let fileURL = NSURL(string: self.audioFilePath())!
self.audioRecorder = try AVAudioRecorder(URL: fileURL, settings: self.audioRecorderSettings() as! [String : AnyObject])
if let recorder = self.audioRecorder{
recorder.delegate = self
if recorder.record() && recorder.prepareToRecord(){
print("Audio recording started successfully")
}
}
}
catch{
print("\(error)")
}
}
func audioFilePath() -> String{
let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
let filePath = path.stringByAppendingPathComponent("test.caf") as String
return filePath
}
func audioRecorderSettings() -> NSDictionary{
let settings = [AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)), AVSampleRateKey : NSNumber(float: Float(16000.0)), AVNumberOfChannelsKey : NSNumber(int: 1), AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Medium.rawValue))]
return settings
}
//MARK: AVAudioPlayerDelegate methods
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
if flag == true{
print("Player stops playing successfully")
}
else{
print("Player interrupted")
}
self.recordButton.enabled = true
self.playButton.enabled = false
self.stopButton.enabled = false
}
//MARK: AVAudioRecorderDelegate methods
func audioRecorderDidFinishRecording(recorder: AVAudioRecorder, successfully flag: Bool) {
if flag == true{
print("Recording stops successfully")
}
else{
print("Stopping recording failed")
}
self.playButton.enabled = true
self.recordButton.enabled = false
self.stopButton.enabled = false
}
}
我在xCode 7.0&上测试了这段代码。 iOS 9。