Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid play wave stream through tcp socket
    text
    copied!<p>I don't know how to play live wave audio stream through socket. Now I have got the socket audio stream. the stream format : </p> <pre><code>wav format header +pcm data wav format header +pcm data wav format header +pcm data </code></pre> <p>So how do i parse the live audio stream to play in the AudioTrack class in android. Thanks. Here is my code :</p> <pre><code>private void PlayAudio(int mode) { if(AudioTrack.MODE_STATIC != mode &amp;&amp; AudioTrack.MODE_STREAM != mode) throw new InvalidParameterException(); long bytesWritten = 0; int bytesRead = 0; int bufferSize = 0; byte[] buffer; AudioTrack track; Socket socket=null; DataInputStream dIn=null; bufferSize = 55584; // i donnt know how much the buffer size should be. 55584 is the size that i got first from the socket stream. maybe the buffer size is setted wrong. //sample rate 16khz,channel: mono sample bits:16 bits channel:1 bufferSize = AudioTrack.getMinBufferSize(16000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); buffer = new byte[bufferSize]; track = new AudioTrack(AudioManager.STREAM_MUSIC, 16000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, mode); // in stream mode, // 1. start track playback // 2. write data to track if(AudioTrack.MODE_STREAM == mode) track.play(); try { socket = new Socket("192.168.11.123", 8081); dIn = new DataInputStream(socket.getInputStream()); // dIn.skipBytes(44); } catch (Exception e) { e.printStackTrace(); } try { do { long t0 = SystemClock.elapsedRealtime(); try { bytesRead = dIn.read(buffer, 0, buffer.length); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NullPointerException e) { // TODO Auto-generated catch block e.printStackTrace(); } bytesWritten += track.write(buffer, 0, bytesRead); Log.e("debug", "WritesBytes "+bytesRead); } while (dIn.read() != -1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } </code></pre> <p>I got mute when i running an activity, but i can hear some music intermittently in debug mode but it is noisy. Could you please help me ? the server send the stream 100ms interval: audio format : //sample rate 16khz,channel: mono sample bits:16 bits channel:1</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload