android - Read continues audio stream from socket -
i making app need read continues stream of sound sent in form of byte array. server side records sound (based on example here on so):
// minimum buffer size required successful creation of audiorecord object. int buffersizeinbytes = audiorecord.getminbuffersize(recorder_samplerate, recorder_channels, recorder_audio_encoding); buffersizeinbytes = 30000; // initialize audio recorder. _audio_recorder = new audiorecord(mediarecorder.audiosource.mic, recorder_samplerate, recorder_channels, recorder_audio_encoding, buffersizeinbytes); // start recording. _audio_recorder.startrecording(); int numberofreadbytes = 0; byte audiobuffer[] = new byte[buffersizeinbytes]; boolean recording = false; float tempfloatbuffer[] = new float[3]; int tempindex = 0; byte totalbytebuffer[] = new byte[60 * 44100 * 2]; while (true) { float totalabsvalue = 0.0f; short sample = 0; numberofreadbytes = _audio_recorder.read(audiobuffer, 0, buffersizeinbytes); (int = 0; < buffersizeinbytes; += 2) { sample = (short) ((audiobuffer[i]) | audiobuffer[i + 1] << 8); totalabsvalue += math.abs(sample) / (numberofreadbytes / 2); } tempfloatbuffer[tempindex % 3] = totalabsvalue; float temp = 0.0f; (int = 0; < 3; ++i) temp += tempfloatbuffer[i]; if ((temp >= 0 && temp <= _sensitivity) && recording == false) { log.i("tag", "1"); tempindex++; continue; } if (temp > _sensitivity && recording == false) { log.i("tag", "2"); recording = true; } if(temp < _sensitivity && recording == true) { recording = false; continue; } (int = 0; < numberofreadbytes; i++) totalbytebuffer[i] = audiobuffer[i]; if (prepare_sound(totalbytebuffer, numberofreadbytes)) { totalbytebuffer = new byte[60 * 44100 * 2]; tempindex++; } }
the example taken recording sound , saves file when there no more sound record. goal on other hand record sound when there sound , send sound on fly when still recording. hence, want send sounds right way , not store file when there no more sound record. far taking byte[] data , stores in object sends client using objectoutputstream. client create temp sound file , play using mediaplayer. feel not effect way achieve goal. so, there more efficient way respect send continues stream of data media player not support playing pure byte[] of data?
thanks , tips!
found out best solutions me record sound , when buffer full sent client side. client uses audiotrack instance play byte[] contains data this:
public void onsoundreceived(byte[] sound) { _audio_input_stream.write(sound, 0, sound.length); }
this makes sound more "non-lagging" not mediaplayer instance stop sound after each time data done playing.
Comments
Post a Comment