title: Casting audio with local server socket
body: <p>I'm trying to fix an existing project which is casting video and audio to the web. 
  I need to create local socket:</p>
  
  <pre><code>socketId = "my.application.media." + suffix + "-" + new 
  Random().nextInt();
  localServerSocket = new LocalServerSocket(socketId);
  
  receiver = new LocalSocket();
  receiver.connect(new LocalSocketAddress(socketId));
  receiver.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
  receiver.setSendBufferSize(SOCKET_BUFFER_SIZE);
  
  sender = localServerSocket.accept();
  sender.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
  sender.setSendBufferSize(SOCKET_BUFFER_SIZE);
  </code></pre>
  
  <p>and creating media recorder:</p>
  
  <pre><code>mMediaRecorder = new MediaRecorder();
  
  mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
  mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  mMediaRecorder.setAudioEncodingBitRate((int) 7.95 * 1024);
  mMediaRecorder.setAudioSamplingRate(8000);
  mMediaRecorder.setAudioChannels(1);
  mMediaRecorder.setOutputFile(sender.getFileDescriptor());
  
  mMediaRecorder.prepare();
  </code></pre>
  
  <p>But I'm getting <code>java.lang.IllegalStateException</code> after calling <code>start</code> on <code>mMediaRecorder</code>. What am I missing? When I'm not using  <code>sender.getFileDescriptor()</code> everything is working correctly so probably that's the problem. I know that there are many libraries which are providing this functionality but I prefer to fix this one. Casting the only video is working correctly and the only problem is with the audio. Thanks a lot for help.
  Order of executed methods:
  added logs to check the order of methods and thread:</p>
  
  <pre><code>creating sockets: Socket opening thread
  creating receiver: Socket opening thread
  creating sender: Socket opening thread
  setting audio source: Socket opening thread
  setting properties: Socket opening thread
  creating file descriptor: Socket opening thread
  preparing media recorder: Socket opening thread
  starting media recorder: Socket opening thread
  </code></pre>
  
  <p>I found that I'm also receiving errors:</p>
  
  <pre><code>2019-02-13 18:15:49.701 6176-13833/? E/StagefrightRecorder: Output file descriptor is invalid
  2019-02-13 18:15:49.701 7851-9780/my.application E/MediaRecorder: start failed: -38
  </code></pre>