简体   繁体   English

Android:使用AudioTrack和Socket手动高效地流音频

[英]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. 基本思想是创建一个应用程序,该应用程序可以记录来自一台设备的音频,并使用套接字通过Wlan将其发送到另一台设备上进行播放。 In nutshell a Lan voice chat program. 简而言之,是Lan语音聊天程序。

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. 我正在使用AudioRecord对象记录来自麦克风的现场音频,然后将记录的数据读入字节数组,然后将字节数组写入TCP套接字。 The receiving device then reads that byte array from the socket and writes it to buffer of an AudioTrack object. 然后,接收设备从套接字读取该字节数组,并将其写入AudioTrack对象的缓冲区。

its like 就像是

Audio Record-->byte array-->socket--->LAN--->socket-->byte array-->AudioTrack 音频记录->字节数组->套接字-> LAN --->套接字->字节数组-> AudioTrack

The process is repeated using while loops. 使用while循环重复该过程。

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? 我应该使用哪种方法使它在Skype和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. 您的缓冲区可以是AudioTrack对象中使用的缓冲区大小的x倍。 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. 之后,您可以将音轨缓冲区大小的块输入到音轨对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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