2

I am trying to write an audio file on a datagram socket and play through the Android media player while the file is being written. I cannot find any way to play it, as the media player needs a complete file to play or a file URL. how should I play my file?

dda
  • 6,030
  • 2
  • 25
  • 34
amit
  • 55
  • 9
  • I think you are trying to implement something like audio calling? – Biraj Zalavadia May 09 '14 at 06:38
  • yes recording sound from one device and sending to another and playing it to device2. – amit May 09 '14 at 06:40
  • What format are you in? In PCM, you can use the answer here: http://stackoverflow.com/questions/11267687/pcm-raw-bytes-to-audio-on-android If wav try http://stackoverflow.com/questions/7372813/android-audiotrack-playing-wav-file-getting-only-white-noise MediaPlayer may be too high level for your needs – Gabe Sechan May 09 '14 at 06:52
  • data is being sent in the format of bytes and it is being received at destination successfully but until it gets written fully into a file it is not being played. – amit May 09 '14 at 07:49

2 Answers2

2

I had the same requirement but is is not possible at all because when you want to play something mediapalyer needs to read header information which stores length,bitrate etc.

And the problem is Header is write at the end of file so that is not possible to pass audio/video between two devices using socket.

Finally after long research I found the technology WEBRTC. This is the opensource project by google and which is intended for the audio/video calling and screen sharing by implementing peer connection.

I am working on it and got success to transfer audio/video between

android-to-android,

android-to-browser

browser-to-browser

You can find complete solution Here

Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • is that possible to record a audio for a sec and then transmit it and play it ?? and repeating the process so that with a small lag audio could be played ??? – amit May 09 '14 at 07:46
  • you mean kind of voice message like in wechat – Biraj Zalavadia May 09 '14 at 08:48
  • i have done the recording process and playing it on another device but working on live streaming through sockets. – amit May 09 '14 at 09:36
  • That what I answered live streaming is not possible through socket because to play any music file mediaplayer must know the header information to get the length and bitrate of the music and until you stop recording it will not write to file. – Biraj Zalavadia May 09 '14 at 09:39
  • http://stackoverflow.com/questions/7372813/android-audiotrack-playing-wav-file-getting-only-white-noise – amit May 09 '14 at 15:14
  • Hello @BirajZalavadia, the above link that you gave is dead. I want the same functionality to stream Audio/Video between devices. Please help me. – Ankit Kamboj Jun 19 '18 at 06:56
2

Use audio Recorder class to record media and you can stream that to the other device before it is completely written and the Audio Player on the next device can play it whether it is completely written or not.

amit
  • 96
  • 5
  • Finally done using the audio recorder class the problem was media player class which requires a complete written file to play. – amit Feb 04 '15 at 05:16