Shiva
Shiva

Reputation: 31

AudioStreaming over Sockets in android

I am trying out for audio streaming over socket in android. I am using AudioRecord for obtaining recorded audio at client side and AudioTrack classes for playing raw data at server side. But I am unable to do it. Is there any other procedure to do this?

audiosender file

public void run()
{

  BufferedOutputStream bufferedStreamInstance = null;
  try
  {
    Socket socket = new Socket("192.168.1.25", 1234);
    bufferedStreamInstance = new BufferedOutputStream(
        socket.getOutputStream());
  } catch (FileNotFoundException e)
  {
    throw new IllegalStateException("Cannot Open File", e);
  } catch (UnknownHostException e)
  {
    e.printStackTrace();
  } catch (IOException e)
  {
    e.printStackTrace();
  }
  DataOutputStream dataOutputStreamInstance = new DataOutputStream(
      bufferedStreamInstance);

  int bufferRead = 0;
  int bufferSize = AudioRecord
      .getMinBufferSize(this.getFrequency(), this.getChannelConfiguration(), this.getAudioEncoding());
  AudioRecord recordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, this.getFrequency(), this
      .getChannelConfiguration(), this.getAudioEncoding(), bufferSize);
  short[] tempBuffer = new short[bufferSize];

  recordInstance.startRecording();
  while (this.isRecording)
  {
    bufferRead = recordInstance.read(tempBuffer, 0, bufferSize);
    if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION)
    {
      throw new IllegalStateException(
          "read() returned AudioRecord.ERROR_INVALID_OPERATION");
    }
    else if (bufferRead == AudioRecord.ERROR_BAD_VALUE)
    {
      throw new IllegalStateException(
          "read() returned AudioRecord.ERROR_BAD_VALUE");
    }
    else if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION)
    {
      throw new IllegalStateException(
          "read() returned AudioRecord.ERROR_INVALID_OPERATION");
    }
    try
    {
      for (int idxBuffer = 0; idxBuffer < bufferRead; ++idxBuffer)
      {
        dataOutputStreamInstance.writeShort(tempBuffer[idxBuffer]);
      }
    } catch (IOException e)
    {
      throw new IllegalStateException(
          "dataOutputStreamInstance.writeShort(curVal)");
    }

  }
  recordInstance.stop();
  try
  {
    dataOutputStreamInstance.close();
    bufferedStreamInstance.close();

  } catch (IOException e)
  {
    throw new IllegalStateException("Cannot close buffered writer.");
  }
}

audioreceiver

public void run()
{

  musicLength = 66535;
  music = new short[musicLength];
  try
  {
    ServerSocket serverSocket = new ServerSocket(1234);
    Socket socket = serverSocket.accept();
    InputStream is = socket.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);

    dis = new DataInputStream(bis);
    while (dis.available() > 0)
    {
      music[musicLength - 1 - i] = dis.readShort();
      i++;
    }
    try
    {
      dis.close();
    } catch (IOException e)
    {
      e.printStackTrace();
    }
    audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025,
        AudioFormat.CHANNEL_CONFIGURATION_MONO,
        AudioFormat.ENCODING_PCM_16BIT, musicLength,
        AudioTrack.MODE_STREAM);
    audioTrack.play();
    audioTrack.write(music, 0, musicLength);
  } catch (Throwable t)
  {
    Log.e("AudioTrack", "Playback Failed");
  }
}

Upvotes: 2

Views: 2031

Answers (1)

Brian
Brian

Reputation: 149

Are you unable to send the data? Or unable to play it properly on the receiver side? As with the MediaRecorder class, the AudioRecorder class probably leaves some space for the header, then performs a seek to fill in the header after recording is completed. Because it is not possible to seek on a socket, the header is written to the head of the file. I think your best course of action would be to write the file to the SD card, then use hexeditor to compare the bytes against a working audio file. You should then be able to determine the format of the header, and when it is received you can seek to the start of the file (or beginning of the buffer) and write the header in the proper location.

Upvotes: 2

Related Questions