0

I'm trying to fix an existing project which is casting video and audio to the web. I need to create local socket:

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);

and creating media recorder:

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();

But I'm getting java.lang.IllegalStateException after calling start on mMediaRecorder. What am I missing? When I'm not using sender.getFileDescriptor() 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:

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

I found that I'm also receiving errors:

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
falsetto
  • 721
  • 2
  • 8
  • 34

2 Answers2

0

As stated here this error java.lang.IllegalStateException occurring when

a method has been invoked at an illegal or inappropriate time.

So with that in mind and with this article in mind of how to use sockets, You should put your socket related staff inside AsyncTask (separated thread) and use try catch.
AsyncTask Documentation , and Socket Documentation if you want to expand your knowledge.

Guy Luz
  • 2,184
  • 9
  • 26
  • I forgot to mention that everything is running on a separated thread. The video is working properly so the architecture is done in the right way, the only problem is with the audio. – falsetto Feb 12 '19 at 20:10
  • What kind of object is sender? – Guy Luz Feb 12 '19 at 20:14
  • sender is the `LocalSocket` and everything is done in the try catch and the only error which I get is IllegalStateException without any explenation. – falsetto Feb 12 '19 at 20:18
0

As it seems you are trying to use getFileDescriptor before (or after if it close) sender have the data to pull it out. Try extracting the data at earlier location in the code to a variable and than use this variable instead.

Another possibility could be; MediaRecorder documentation says

You must specify a file descriptor that represents an actual file

so be sure that the type that sender.getFileDescriptor() return is the right type that mMediaRecorder.setAudioChannels can get.

Guy Luz
  • 2,184
  • 9
  • 26
  • I tried to create the variable and the use it but nothing changes. I also run `valid()` on tris FileDescription and it said `true` so the problem seems to be somewhere else. I've added logs to check the order of methods and thread - added in the question. – falsetto Feb 12 '19 at 20:56
  • 1
    https://stackoverflow.com/questions/26990816/mediarecorder-issue-on-android-lollipop – Guy Luz Feb 13 '19 at 17:33