-1

I successfully stream audio over TCP from python running on raspberry pi to an android application and I am trying to use compression but it's only working with strings and not working with data from audio stream (only noise is heard) here is my python code:

    while True:
      data = stream.read(CHUNKS)
      outData = StringIO()
      d = GzipFile( mode="w", fileobj = outData)
      d.write(data)
      d.close()
      conn.send((outData.getvalue()))

and my android code:

    speaker = newAudioTrack(AudioManager.STREAM_VOICE_CALL,sampleRate,channelConfig,audioFormat,minBufSize,AudioTrack.MODE_STREAM);
    speaker.play();
    DataInputStream ds = new DataInputStream(connectionSocket.getInputStream());
    GZIPInputStream gzip = new GZIPInputStream(ds);
    while (true) {
    gzip.read(buffer, 0, buffer.length);
    speaker.write(buffer, 0, buffer.length);}

Does anybody have an idea why this is not working? Also, are there alternative ways to achieve what I'm trying to?

1 Answers1

0

The problem might not be in GZIP at all:

AudioTrack used only for uncompressed audio as mentioned here:

It allows streaming of PCM audio buffers to the audio sink

You might give the MediaPlayer a try, as an example:

MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://...");
player.prepare();
player.start();

Yet this might not be what you need as you are trying to use Socket

In this case you might use Temporary file as mentioned in this post

OR use a Local HTTP server to stream an InputStream to MediaPlayer on Android

Hope this can help, best of luck :)

Community
  • 1
  • 1
Aboalnaga
  • 602
  • 6
  • 16