7 Prepper Apps for Travel and Home Safety

A mobile phone is a powerful tool when loaded with the first aid and prepper applications listed below!

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Create an Android Sound Recorder using Kotlin

The Android multimedia framework includes support for recording and playing audio. In this blog post, we’ll develop a basic Sound Recorder application that is capable of recording audio and saving it into the local storage of an Android device using the MediaRecorder that is provided by the Android SDK.

You will also learn how to request user-permissions in real-time and how to work with the local storage of an Android device.

First, we need to build the user interface of the Audio Recorder. It consists of a simple layout with 3 buttons which will be used to start, pause, resume and stop the recording.

After creating the UI we can almost start using the MediaRecorder to build our app. But first, we need to request the required permissions to record the audio and access the local storage. We can do that with some simple lines of code in our AndroidManifest.xml file.

We also need to check if the user has really enabled the permissions before we can use our MediaRecorder. We can do so in our MainActivity.kt file.

Next, we need to define a path for our output and start configuring our MediaRecorder.

Here, we get the path to the root of our external storage and add our recording name and filetype to it. After that, we create our MediaRecorder object and define the audio source, audio encoder, output format and output file.

As you can see we need to call the prepare function before we can start the recording. We also embed it into a try-catch block to make sure the app won’t crash when the prepares function fails.

The OnClickListeners of the stop button is very similar to the one we defined above.

Here we check if the MediaRecorder is currently running before we actually stop the recording because our app would crash if the stop method is called while the MediaRecorder isn’t recording. After that, we change the state variable to false to prevent the user from pressing the stop button again.

After that, we just need to define the OnClickListener for the pause/resume button.

In these two methods we check if the MediaRecorder is running. If so we pause the recording and change the text of the button to resume. If clicked again the recording will resume from the point it left of.

Finally, we can start recording audio and listen to it by opening the recording.mp3 file that will be saved in our local storage.

Here you can get the complete source code for our basic Sound Recorder app:

After recording you need to go into the local storage of your Android device to listen to your recordings. I’m just including it for the people who aren’t confident navigating in the local Android storage.

Now you should know how the MediaRecorder works, how you can request permission in real-time and why it is important to do so. You have also learned about the local storage of your Android device and how you store data in it.

If you have found this useful, please consider recommending and sharing it with other fellow developers.

If you have any questions or critics, you can reach me in the comment section.

Add a comment

Related posts:

Jeanie In A Bottle

Fuzzy days in trance-like movies The haunting pain still follows you Numbly try to struggle through these The fear of feeling hollows you Shames so dark we cannot claim them Cries so thick we can’t…

10 of The Best Crypto Signal Providers in 2022

Traders with very little knowledge about how the market works, are advised by an expert, who can study the charts and keep the tabs with the latest news. At the moment, there are tons of crypto…