How can I stream audio from the microphone of an iPhone to a Mac/PC via sockets or a framework?

余生长醉 提交于 2021-02-05 20:34:15

问题


How can I stream audio from the microphone of an iPhone to a Mac/PC? Is there already some framework for this, or can I just send audio over sockets. I'm new to sockets though. Basically, I want to be able to speak into the iPhone, and the computer will receive the iPhone's mic input as its own mic input for computers that do not have microphones. I already have an app that makes a bonjour connection to a Mac, which runs a very simple server, and the iPhone can send text to the computer, but how could the iPhone send audio, live audio from the mic, to it?


回答1:


You'll need a combination of AVCaptureSession and AVCaptureDevice to read from the microphone - see the AV Foundation Programming Guide. http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVCaptureAudioDataOutput_Class/Reference/Reference.html#//apple_ref/occ/cl/AVCaptureAudioDataOutput

For link to use sokets

@interface Client : NSObject {
    NSInputStream *_inputStream;
    NSOutputStream *_outputStream;
}

@implementation Client

- (void)initNetworkCommunication {
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 50000, &readStream, &writeStream);

    _inputStream = (__bridge NSInputStream *)readStream;
    _outputStream = (__bridge NSOutputStream *)writeStream;

    [_inputStream setDelegate:self];
    [_outputStream setDelegate:self];

    [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [_inputStream open];
    [_outputStream open];
}

// send data to server


- (IBAction)onSendButtonTapped:(id)sender {
    NSString *command = self.commandField.text;
    NSData *data = [[NSData alloc] initWithData:[command dataUsingEncoding:NSUTF8StringEncoding]];
    [_outputStream write:[data bytes] maxLength:[data length]];
}


来源:https://stackoverflow.com/questions/9815717/how-can-i-stream-audio-from-the-microphone-of-an-iphone-to-a-mac-pc-via-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!