9.4 Using MediaRecord for Audio Recording
Category Android Basic Tutorial
Introduction to This Section
This section is the final part of the basic API calls for Android multimedia, introducing the simple use of MediaRecord. The usage is very straightforward; let's write an example to get familiar with it.
1. Recording Audio with MediaRecord
Running Result :
Implementation Code :
Layout Code: activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_control"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Recording" />
</RelativeLayout>
MainActivity.java :
public class MainActivity extends AppCompatActivity {
private Button btn_control;
private boolean isStart = false;
private MediaRecorder mr = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_control = (Button) findViewById(R.id.btn_control);
btn_control.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isStart){
startRecord();
btn_control.setText("Stop Recording");
isStart = true;
}else{
stopRecord();
btn_control.setText("Start Recording");
isStart = false;
}
}
});
}
// Start recording
private void startRecord(){
if(mr == null){
File dir = new File(Environment.getExternalStorageDirectory(),"sounds");
if(!dir.exists()){
dir.mkdirs();
}
File soundFile = new File(dir,System.currentTimeMillis()+".amr");
if(!soundFile.exists()){
try {
soundFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
mr = new MediaRecorder();
mr.setAudioSource(MediaRecorder.AudioSource.MIC); // Audio source
mr.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB); // Set output format
mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB); // Set encoding format
mr.setOutputFile(soundFile.getAbsolutePath());
try {
mr.prepare();
mr.start(); // Start recording
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Stop recording and release resources
private void stopRecord(){
if(mr != null){
mr.stop();
mr.release();
mr = null;
}
}
}
Don't forget to add the following permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
That's it, it's that simple!
2. Download the Example Code for This Section
Summary of This Section:
>
Well, the content of this section is very simple, it's just the use of MediaRecorder, probably the most concise section of the entire tutorial.
-1.0.1 Latest Android Basic Tutorial Catalog for 2015
-1.1 Background and System Architecture Analysis
-1.2 Development Environment Setup
-1.2.1 Developing Android APPs with Eclipse + ADT + SDK
-1.2.2 Developing Android APPs with Android Studio
-1.3 Solving SDK Update Issues
-1.4 Genymotion Emulator Installation
-1.5.1 Git Tutorial on Basic Operations of Local Repositories
-1.5.2 Git: Using GitHub to Set Up a Remote Repository
-1.6 How to Play with 9 (Jiu Mei) Images
-1.7 Interface Prototype Design
-1.8 Project-Related Analysis (Various Files, Resource Access)
2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)
2.5.8 Detailed Explanation of Notification (Status Bar Notification)
2.6.4 Simple Usage of DrawerLayout (Official Side-Slide Menu)
3.6 Responding to System Setting Events (Configuration Class)
4.4.2 Further Exploration of ContentProvider - Document Provider
[5.2.1 Detailed Explanation of Fragment
8.3.4 Paint API - In-depth Explanation of Xfermode and PorterDuff (Part 1)
8.3.5 Paint API - In-depth Explanation of Xfermode and PorterDuff (Part 2)
8.3.6 Paint API - In-depth Explanation of Xfermode and PorterDuff (Part 3)
8.3.7 Paint API - In-depth Explanation of Xfermode and PorterDuff (Part 4)
8.3.8 Paint API - In-depth Explanation of Xfermode and PorterDuff (Part 5)
8.3.14 Paint - Several Enums/Constants and ShadowLayer Shadow Effects
8.3.17 Detailed Explanation of Canvas API (Part 2) - Collection of Clipping Methods
8.3.18 Detailed Explanation of Canvas API (Part 3) - Matrix and drawBitmapMesh
8.4.3 Android Animation Collection - Property Animation - First Encounter
8.4.4 Android Animation Collection - Property Animation - Revisited
9.4 Recording with MediaRecord
[11.0 "2015 Latest Android Basic Tutorial" Completion Celebration~]