android - AudioTrack buffer underrun when streaming from bluetooth socket -
i testing concept of android application allows stream bluetooth on rfcomm (from pc phone). able transfer audio computer phone no problem , start streaming audio. problem audio begins stutter , buffer underrun error message audiotrack. reading socket taking time. when timed it, underruns occurred when takes >= 1000 milliseconds read return, when on average takes few hundred return. here code below:
public connectedthread(bluetoothsocket socket) { this.setpriority(max_priority); mmsocket = socket; inputstream tmpin = null; outputstream tmpout = null; // input , output streams; using temp objects because // member streams final. try { tmpin = socket.getinputstream(); } catch (ioexception e) { log.e(tag, "error occurred when creating input stream", e); } try { tmpout = socket.getoutputstream(); } catch (ioexception e) { log.e(tag, "error occurred when creating output stream", e); } minbuffsize = audiotrack.getminbuffersize(48000, channel_out_mono, audioformat.encoding_pcm_16bit); m_audiotrack = new audiotrack(audiomanager.stream_music, 48000, channel_out_stereo, audioformat.encoding_pcm_16bit, minbuffsize /* 1 second buffer */, audiotrack.mode_stream); mmbuffer = new byte[minbuffsize * 2]; mminstream = tmpin; mmoutstream = tmpout; // distream = new datainputstream(new bufferedinputstream(mminstream, 1000000)); distream = new datainputstream(mminstream); } public void run() { int numbytes = 0; // bytes returned read() // keep listening inputstream until exception occurs. boolean firstrun = true; while (true) { try { final long starttime = system.currenttimemillis(); distream.readfully(mmbuffer); final long endtime = system.currenttimemillis(); int temp = m_audiotrack.write(mmbuffer, 0, mmbuffer.length); system.out.println("total execution time: " + (endtime - starttime) ); if (firstrun) { m_audiotrack.play(); firstrun = false; } } catch (ioexception e) { log.d(tag, "input stream disconnected", e); break; } }
any suggestions on how can speed reads socket or prevent underruns in general?
Comments
Post a Comment