ios – 将音频从麦克风通过蓝牙传输到另一个iPhone

我正在尝试将传入的麦克风音频传输到另一个iPhone.基本上一个电话,但通过蓝牙.我有音频通过AVAudioRecorder进来:
func startRecording() {
    audioRecorder = nil
    let audioSession:AVAudioSession = AVAudioSession.sharedInstance()
    audioSession.setCategory(AVAudioSessionCategoryRecord,error: nil)

    var recordSettings:NSMutableDictionary = NSMutableDictionary(capacity: 10)
    recordSettings.setobject(NSNumber(integerLiteral: kAudioFormatLinearPCM),forKey: AVFormatIDKey)
    recordSettings.setobject(NSNumber(float: 44100.0),forKey: AVSampleRateKey)
    recordSettings.setobject(NSNumber(int: 2),forKey: AVNumberOfChannelsKey)
    recordSettings.setobject(NSNumber(int: 16),forKey: AVLinearPCMBitDepthKey)
    recordSettings.setobject(NSNumber(bool: false),forKey: AVLinearPCMIsBigEndianKey)
    recordSettings.setobject(NSNumber(bool: false),forKey: AVLinearPCMIsFloatKey)

    soundpath = documentsDirectory.stringByAppendingPathComponent("record.caf")
    refURL = NSURL(fileURLWithPath: soundpath as String)
    var error:NSError?

    audioRecorder = AVAudioRecorder(URL: refURL,settings: recordSettings as [NSObject : AnyObject],error: &error)

    if audioRecorder.preparetoRecord() == true {
        audioRecorder.meteringEnabled = true
        audioRecorder.record()
    } else {
        println(error?.localizedDescription)
    }
}

然后我尝试从@ martin-r从HERE – StreamReader使用StreamReader

使用:

if let aStreamReader = StreamReader(path: documentsDirectory.stringByAppendingPathComponent("record.caf")) {
                while let line = aStreamReader.nextLine() {
                    let dataz = line.dataUsingEncoding(NSUTF8StringEncoding)
                    println (line)

然后使用以下方式将数据发送到另一台设备:

self.appDelegate.mpcDelegate.session.sendData(data: NSData!,toPeers: [AnyObject]!,withMode: MCSessionSendDataMode,error: NSErrorPointer )

我将线转换为NSData,然后使用dispatch_after 0.5秒不断运行,我通过蓝牙发送到另一个设备.

这似乎不起作用,我不认为这是一个实际的做法.我已经做了大量的搜索,并没有看到太多的流数据通过蓝牙.关键字流(可理解)将我发送到有关服务器流的页面.

我的问题是,如何从麦克风接收音频并通过蓝牙发送到另一个iPhone?我有蓝牙部分全部设置,它的工作非常好.我的问题非常类似于THIS,除了iPhone和Swift – 我想通过蓝牙打电话.

先谢谢你.

解决方法

要同时录制和重定向输出,您需要使用类别AVAudioSessionCategoryMultiRoute.

以下是类别列表的链接
https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/#//apple_ref/doc/constant_group/Audio_Session_Categories

如果其他所有失败都可以使用预制的解决方案:
http://audiob.us

它有一个API,可以将音频流从一个应用程序集成到另一个应用程序:
https://developer.audiob.us/

支持多个输出端点.

希望这可以帮助.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...