简体   繁体   中英

Android : manually streaming audio effeciently using AudioTrack and Socket

The basic Idea is to create an application that can record audio from one device and send it over Wlan using sockets to another device that will play it. In nutshell a Lan voice chat program.

I am recording live audio from mic using a AudioRecord object and then read the recorded data into byte array ,then write the byte array to a TCP socket. The receiving device then reads that byte array from the socket and writes it to buffer of an AudioTrack object.

its like

Audio Record-->byte array-->socket--->LAN--->socket-->byte array-->AudioTrack

The process is repeated using while loops.

Although the audio is playing there its lagging between frames. ie when I say Hello the receiver hears He--ll--O. Although the audio is complete but there is lag between the buffer blocks.

As far as I know the lag is due to delay in Lan transmission

How do I improve it? What approach should I use so it is smooth as it is in commercial online chat applications like skype and gtalk?

Sounds like you need a longer buffer somewhere to deal with the variance of the audio transmission over lan. To deal with this you could create an intermediary buffer between the socket byte array and the audio track. Your buffer can be x times the size of the buffer used in the AudioTrack object. So something like this:

Socket bytes -> Audio Buffer -> Buffer to get fed to Audio Track -> Audio Track

When audio starts recording, don't play anything back until it completely fills up the longer buffer. And after that you can feed blocks of the size of your Audio Track buffer to your Audio Track object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM