My Hexaville
Spead the Word
Android Sending Audio File

Android Sending Audio

Hey there, let’s continue writing our Walkie-Talkie app. In the previous post I covered recording, playing audio and some visuals for displaying playing progress/duration. As a final part of three step process: Record, Send, Receive, today we’re going to send and receive audio files. So let’s get started!

This kind of topic might be the most frustrating. There’s plenty of simple REST/TO-DO’s apps up there on GitHub, but almost nothing about audio/video calls. And if you Google it, you won’t get a clear step-to-step guides about implementing it. Most likely you’ll end up on some payed service with their API. Although it might be perfect for a company who wants to get results fast and cheap, as a programmers we’re eager to get to the low level stuff. Understand the basics and make the whole thing by yourself. This series of articles is exactly about it.

Sending Audio

If you’ve made an audio calling app or sent audio files over the internet it might be a bit frustrating. Where to start,  do I need a server…? And at the times like this, when there’s so much stuff to learn,  I always get to the very basics. It always helps, and from the basics you build it up. In this app, for example, the basics are: everything is just bytes. Doesn’t matter how complex the thing you’re sending over the internet, it’s all just bytes!

Starting from there you realize that partially you’ve already done it before. Simple chat application, posting anything to the server… So it sound manageable now.

App side

First of all, let’s start with simple approach – record audio, save to file, send it to server, send it to everybody else who’s listening. That’s not audio streaming yet, but we’ll get there. We know how to record and save audio to file, let’s see how we send it to server.

What I’m using here is OkHttp WebSocketListener, when connection is open I keep the reference to WebSocket object. When user recorded a message, the following is going:

  • open an InputStream from recorded file
  • send first message to tell that’s the new file is coming
  • send bytes form InputStream
  • send massage indicating the end of file

The code behind it

We clear buffer since we reuse it and you can find the flip method explanation in here

Server side

And now let’s take a look at what we have on a server side. It simply serves as a middle man, sending audio file to everybody connected,except for sender. We’ll implement streaming in the future posts. Start with simple things.

I decided to go with Spark Framework for a server. Doesn’t really matter which one choose. For me it’s easier not to switch languages, hence Java. Plus there isn’t much code to receive and send files. That’s why I won’t put server side code in here, since there’s so many different ways to implement it and most likely you’ll be using some different language/framework. But if you want to get the source code, you can get it in this repo.


Finally, what’s left is to receive it on other device. Since I’m using OkHttp WebSocketListener, there’s a method I override – onMessage. It has a String argument and this String would look like this:

I take the value part of it and convert to ByteString class from which I can get byte array. We stack those arrays in a list and when app receives an ‘end’ message, it means we can build a file from those arrays. And we know that that is a 3gp. That’s the code

I removed try/catches for readability purposes.



So as you see we successfully managed to send audio file over the internet to other devices. And as a result got some rather incomplete Walkie-Talkie app which sends recorded file to other devices. But what about making a calling app and streaming, you might say? Yep, we’re going to implement streaming audio like you would expect in the future articles.


Ok, thank’s for reading. Android app repo you can get here and server here. You can just clone the Android app and use my backend. But it doesn’t handle multiple users talking simultaneously. Once again the coolest part is coming in the next posts where we will make a real calling app and then build a cool UI for it!

If you like the article, subscribe to get future posts reminders, leave your comments if you have any questions and share with your friends if you think they will benefit from this stuff as well!


About the Author Ihor Klimov

Formerly an Android developer, lately picked up some Flutter. This blog is everything that I find exciting about Android and Flutter development. Stay tuned and hope to see you again!