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

后端 未结 1 1926
你的背包
你的背包 2021-02-03 13:48

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.

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-03 13:57

    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]];
    }
    

    0 讨论(0)
提交回复
热议问题