Android Basics
TUTORIALS LIBRARY
Android - Home
Android - Overview
ODING GROUND
Android - Environment Setup
TOR CONNECT
Android - Architecture
Android - Application Components
Android - Hello World Example
Android - Resources
Android - Activities
Android - Services
Android - Broadcast Receivers
Android - Content Providers
Android - Fragments
Android - Intents/Filters
Android - User Interface
Android - UI Layouts
Android - UI Controls
Android - Event Handling
Android - Styles and Themes
Android - Custom Components
Android Advanced Concepts
Android - Drag and Drop
Android - Notifications
Location Based Services
Android - Sending Email
Android - Sending SMS
Android - Phone Calls
Publishing Android Application
Android Useful Examples
Android - Alert Dialoges
Android - Animations
Android - Audio Capture
Android - AudioManager
Android - Auto Complete
Android - Best Practices
Android - Bluetooth
Android - Camera
Android - Clipboard
Android - Custom Fonts
Android - Data Backup
Android - Developer Tools
Android - Emulator
Android - Facebook Integration
Android - Gestures
Android - Google Maps
Android - Image Effects
Android - ImageSwitcher
Android - Internal Storage
Android - JetPlayer
Android - JSON Parser
Android - Linkedin Integration
Android - Loading Spinner
Android - Localization
Android - Login Screen
Android - MediaPlayer
Android - Multitouch
Android - Navigation
Android - Network Connection
Android - NFC Guide
Android - PHP/MySQL
Android - Progress Circle
Android - ProgressBar
Android - Push Notification
Android - RenderScript
Android - RSS Reader
Android - Screen Cast
Android - SDK Manager
Android - Sensors
Android - Session Management
Android - Shared Preferences
Android - SIP Protocol
Android - Spelling Checker
Android - SQLite Database
Android - Support Library
Android - Testing
Android - Text to Speech
Android - TextureView
Android - Twitter Integration
Android - UI Design
Android - UI Patterns
Android - UI Testing
Android - WebView Layout
Android - Wi-Fi
Android - Widgets
Android - XML Parsers
Android Useful Resources
Android - Questions and Answers
Android - Useful Resources
Android - Discussion
eu
rC
a
p
toA
u
d
i -do
iA
n
d
r
Previous Next Page
Page
Android has a built in microphone through which you can capture audio and store it , or play it in your
phone. There are many ways to do that but the most common way is through MediaRecorder class.
Android provides MediaRecorder class to record audio or video. In order to use MediaRecorder class
,you will first create an instance of MediaRecorder class. Its syntax is given below.
MediaRecorder myAudioRecorder = new MediaRecorder();
Now you will set the source , output and encoding format and output file. Their syntax is given below.
[Link]([Link]);
[Link]([Link].THREE_GPP);
[Link]([Link].AMR_NB);
[Link](outputFile);
After specifying the audio source and format and its output file, we can then call the two basic methods
prepare and start to start recording the audio.
[Link]();
[Link]();
Apart from these methods , there are other methods listed in the MediaRecorder class that allows you
more control over audio and video recording.
[Link] Method & description
1 setAudioSource()
This method specifies the source of audio to be recorded
2 setVideoSource()
This method specifies the source of video to be recorded
3 setOutputFormat()
This method specifies the audio format in which audio to be stored
4 setAudioEncoder()
This method specifies the audio encoder to be used
5 setOutputFile()
This method configures the path to the file into which the recorded audio is to be stored
6 stop()
This method stops the recording process.
7 release()
This method should be called when the recorder instance is needed.
em
aE
lxp
This example provides demonstration of MediaRecorder class to capture audio and then MediaPlayer
class to play that recorded audio.
To experiment with this example , you need to run this on an actual device.
Steps Description
1 You will use Android studio IDE to create an Android application and name it as
AudioCapture under a package [Link].
2 Modify src/[Link] file to add AudioCapture code
3 Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
4 Modify [Link] to add necessary permissions.
5 Run the application and choose a running android device and install the application on it and
verify the results.
Here is the content of src/[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import static [Link].RECORD_AUDIO;
import static [Link].WRITE_EXTERNAL_STORAGE;
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
Button buttonStart, buttonStop, buttonPlayLastRecordAudio,
buttonStopPlayingRecording ;
String AudioSavePathInDevice = null;
MediaRecorder mediaRecorder ;
Random random ;
String RandomAudioFileName = "ABCDEFGHIJKLMNOP";
public static final int RequestPermissionCode = 1;
MediaPlayer mediaPlayer ;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
buttonStart = (Button) findViewById([Link]);
buttonStop = (Button) findViewById([Link].button2);
buttonPlayLastRecordAudio = (Button) findViewById([Link].button3);
buttonStopPlayingRecording = (Button)findViewById([Link].button4);
[Link](false);
[Link](false);
[Link](false);
random = new Random();
[Link](new [Link]() {
[Link](new [Link]() {
@Override
public void onClick(View view) {
if(checkPermission()) {
AudioSavePathInDevice =
[Link]().getAbsolutePath() + "/" +
CreateRandomAudioFileName(5) + "AudioRecording.3gp";
MediaRecorderReady();
try {
[Link]();
[Link]();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
[Link]();
} catch (IOException e) {
// TODO Auto-generated catch block
[Link]();
}
[Link](false);
[Link](true);
[Link]([Link], "Recording started",
Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
[Link]();
[Link](false);
[Link](true);
[Link](true);
[Link](false);
[Link]([Link], "Recording Completed",
Toast.LENGTH_LONG).show();
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) throws IllegalArgumentException,
SecurityException, IllegalStateException {
[Link](false);
[Link](false);
[Link](true);
mediaPlayer = new MediaPlayer();
try {
[Link](AudioSavePathInDevice);
[Link]();
} catch (IOException e) {
[Link]();
}
}
[Link]();
[Link]([Link], "Recording Playing",
Toast.LENGTH_LONG).show();
}
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
[Link](false);
[Link](true);
[Link](false);
[Link](true);
if(mediaPlayer != null){
[Link]();
[Link]();
MediaRecorderReady();
}
}
});
public void MediaRecorderReady(){
mediaRecorder=new MediaRecorder();
[Link]([Link]);
[Link]([Link].THREE_GPP);
[Link]([Link].AMR_NB);
[Link](AudioSavePathInDevice);
}
public String CreateRandomAudioFileName(int string){
StringBuilder stringBuilder = new StringBuilder( string );
int i = 0 ;
while(i < string ) {
[Link](RandomAudioFileName.
charAt([Link]([Link]())));
i++ ;
}
return [Link]();
}
private void requestPermission() {
[Link]([Link], new
String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO}, RequestPermissionCode);
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if ([Link]> 0) {
boolean StoragePermission = grantResults[0] ==
PackageManager.PERMISSION_GRANTED;
boolean RecordPermission = grantResults[1] ==
PackageManager.PERMISSION_GRANTED;
if (StoragePermission && RecordPermission) {
[Link]([Link], "Permission Granted",
Toast.LENGTH_LONG).show();
Toast.LENGTH_LONG).show();
} else {
[Link]([Link],"Permission
Denied",Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean checkPermission() {
int result = [Link](getApplicationContext(),
WRITE_EXTERNAL_STORAGE);
int result1 = [Link](getApplicationContext(),
RECORD_AUDIO);
return result == PackageManager.PERMISSION_GRANTED &&
result1 == PackageManager.PERMISSION_GRANTED;
}
}
Here is the content of activity_main.xml
In the below code abc indicates the logo of tutorialspoint
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/abc"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_marginTop="37dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOP"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_centerHorizontal="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="@+id/button3"
android:layout_alignTop="@+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOP PLAYING RECORDING "
android:id="@+id/button4"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
Here is the content of [Link]
<resources>
<string name="app_name">My Application</string>
</resources>
Here is the content of [Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link]" >
<uses-permission android:name="[Link].WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="[Link].RECORD_AUDIO" />
<uses-permission android:name="[Link]" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="[Link]"
android:label="@string/app_name" >
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with
your computer. To run the app from Android studio, open one of your project's activity files and click
Run icon from the toolbar. Before starting your application, Android studio will display following
images.
Now by default you will see stop and play button disable. Just press the Record button and your
application will start recording the audio. It will display the following screen.
Now just press stop button and it will save the recorded audio to external sd card. When you click on
stop button , the following screen would appear.
Now just press the play button and and recorded audio will just start playing on the device. The
following message appears when you click on play button.
Previous Next Page
Page
Advertisements
Write for us FAQ's Helping Contact
Copyright 2017. All Rights Reserved.
Enter email for newsletter
go