How to add Live Audio to your Android Application

How to add Live Audio to your Android Application

Integrating live audio communication and other modern messaging tools into your own Android application is easier than ever with Voxer SDK. Within minutes you can send audio and text messages to a group of users and enable Walkie talkie mode so that audio messages can be sent any time, even when your app is the background.

Using the Voxer SDK, the complexity of adding synchronous and asynchronous communication is dramatically simplified. Here’s how you can add communication easily to your iOS application.

Import dependencies and add permissions

Lets start by adding the most recent Voxer SDK to the gradle.properties file by adding the authToken found in the Developer Portal.

In your module’s gradle file add following to the dependencies:

implementation 'com.github.Voxer:voxer_io_android_sdk:b847af72c4'
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.4.31'
implementation 'androidx.core:core-ktx:1.3.2'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9"

In your app level gradle file, add following under repositories:

maven { url '<https://jitpack.io>' }

In your app level gradle file add following under allprojects -> repositories

maven {
                url '<https://jitpack.io>'
                credentials { username 'jp_6025ebstlo9iolugpmh8vj0ki9' }
            }

Next initialize the Voxer SDK by adding the following snippet:

Voxer.getInstance(application)

Enable record permission to allow Voxer to record audio:

uses-permission android:name="android.permission.RECORD_AUDIO"

Now we can add authentication by logging into the Voxer network with:

Voxer.getInstance(application).login (username,appId, appSecret) 
                { success ->
                  if (success) { //handle successfull login }
                  else         { //handle login failure }
                }

 

Start a new conversation and send messages

Now that you have initialized the Voxer SDK and logged into the Voxer network, you can now start a chat and send text and audio messages.

First start by creating a new chat with a unique list of users by adding the following:

voxer.startConversationWith(otherUserIds, lifecycle, conversationStarterCallback)

Now you can send a text message with the following snippet:

conversation.sendTextMessage(text, messageCallback)

To use the above API with an UI element you can use the following example:

btnSendMessage.setOnClickListener {
                val messageBody = etStartTyping.text.toString()
                sendTextMessage(messageBody) {
                btnSendMessage.hideKeyboard()
                }
                etStartTyping.setText("")
                }

To send an audio message use the following:

conversation.startRecording( onRecordingStarted = { // refresh the list of messages },
              messageCallbacks = MessageCallbacks())

To stop sending audio message add:

conversation.stopRecording()

Conclusion

Congratulations! You have now added live audio and text messaging to your application with a few lines of code using the Voxer SDK. If you don’t have access to a Voxer Developer Portal you can sign up to join the beta program here: https://get.voxer.com/sdk/

Learn more about integrating live audio into your iOS applications here.

Spread the love