0% found this document useful (0 votes)
21 views

Android App

The document describes an Android app that was designed and developed to record audio and play it back. The app allows the user to press buttons to start and stop recording, and to play back the recorded audio. It also includes code to upload the recorded audio file to a server. The objective was achieved by implementing functions for recording and playback using the MediaRecorder and MediaPlayer classes, and for file handling and network requests using OkHttp. Screenshots of the app interface and a recording of it in use are also provided.

Uploaded by

Gp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Android App

The document describes an Android app that was designed and developed to record audio and play it back. The app allows the user to press buttons to start and stop recording, and to play back the recorded audio. It also includes code to upload the recorded audio file to a server. The objective was achieved by implementing functions for recording and playback using the MediaRecorder and MediaPlayer classes, and for file handling and network requests using OkHttp. Screenshots of the app interface and a recording of it in use are also provided.

Uploaded by

Gp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DSP System Design Lab

Name : Yaram Ektha Suhitha Roll No : 20EE38019

Objective : To design and develop a Basic Android app that will


record audio and play it.

Code :

package com.example.myrecord;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.ContextWrapper;
import android.content.pm.PackageManager;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

private static int MICROPHONE_PERMISSION_CODE = 200;


private static int STORING_PERMISSION_CODE = 300;
private static int READING_PERMISSION_CODE = 300;
private OkHttpClient okHttpClient;
MediaRecorder mediaRecorder;
MediaPlayer mediaPlayer;
Button uploadBtn;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
okHttpClient = new OkHttpClient();

if (isMicrophonePresent()) {
getMicrophonePermission();
getStoringPermission();
getReadingPermission();

}
uploadBtn = findViewById(R.id.demo_upload_btn);
uploadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadToServer();
}
});
}

public void btnRecordPressed(View v) {

try {
mediaRecorder = new MediaRecorder();

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setAudioEncodingBitRate(16*96000);
mediaRecorder.setAudioSamplingRate(96000);
mediaRecorder.setOutputFile(getRecordingFilePath());

mediaRecorder.prepare();
mediaRecorder.start();

Toast.makeText(this, "Recording is started",


Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void btnStopPressed(View v) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
Toast.makeText(this, "Recording Stopped",
Toast.LENGTH_LONG).show();

//String postUrl = "http://192.168.5.135:5000/uploadfile";

}
public void btnPlayPressed(View v) {

try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getRecordingFilePath());
mediaPlayer.prepare();
mediaPlayer.start();

TextView textView = findViewById(R.id.textview);


String msg = "Playing: " + getRecordingFilePath();
textView.setText(msg);
}

catch (Exception e) {
e.printStackTrace();
}
}

private boolean isMicrophonePresent() {


if
(this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MI
CROPHONE)) {
return true;
}
else {
return false;
}
}

private void getMicrophonePermission() {


if(ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO) ==
PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.RECORD_AUDIO}, MICROPHONE_PERMISSION_CODE);
}
}

private void getStoringPermission() {


if(ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.WRITE_EXTERNAL_STORAGE},
STORING_PERMISSION_CODE);
}
}

private void getReadingPermission() {


if(ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.READ_EXTERNAL_STORAGE},
READING_PERMISSION_CODE);
}
}

private String getRecordingFilePath() {


ContextWrapper contextWrapper = new
ContextWrapper(getApplicationContext());
File musicDirectory =
contextWrapper.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
File file = new File(musicDirectory,"testRecordingFile" +
".wav");
return file.getPath();
}

public void uploadToServer() {

File audioPath = new


File(Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/com.example.myrecord/files/Music/");
File sourceFile = new
File(audioPath,"testRecordingFile.wav");

MultipartBody.Builder builder=new MultipartBody.Builder();


builder.setType(MultipartBody.FORM);

builder.addFormDataPart("file",sourceFile.getName().toString(),
RequestBody.create(MediaType.parse("/"),sourceFile));
MultipartBody multipartBody = builder.build();

String urlStr = "http://" + "192.168.5.135" + ":" + 5000 +


"/uploadfile";
Request request = new Request
.Builder()
.post(multipartBody)
.url(urlStr)
.build();

OkHttpClient okHttpClient = new OkHttpClient();

okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull
IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Something
went wrong:" + " " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response
response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Toast.makeText(MainActivity.this,response.body().string(),Toast.LENG
TH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
}

Display of developed App main page


Screen Recording of Recording and playing my voice in the app
developed by using this code

vidma_recorder_1109
2023_212242.mp4

You might also like