Android 4.4 Audiorecord Use case-recording system built-in sound

Source: Internet
Author: User

The new MediaRecorder.AudioSource.REMOTE_SUBMIX parameter of API 19 allows the system app to record the system's built-in sound, which is the sound of the speaker. Here is a giant simple example of how to record with Audiorecord with the Remote_submix parameter.


1. Compiling apk

Mainactivity.java:

Package Com.example.audiotest;import Java.io.fileoutputstream;import Java.io.ioexception;import Android.app.activity;import Android.media.audioformat;import Android.media.audiorecord;import Android.media.mediarecorder;import Android.os.bundle;import Android.util.log;import Android.view.View;import Android.widget.button;public class Mainactivity extends Activity {private static String TAG = "JZJ"; Audiorecord Mrecord = Null;boolean mreqstop = false; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); init ();(new Thread () {@Overridepublic void Run () {Recordandplay ();}}). Start ();} Private final int ksamplerate = 44100;private final int kchannelmode = audioformat.channel_in_stereo;private final int kEn Codeformat = audioformat.encoding_pcm_16bit;private void init () {int minbuffersize = Audiorecord.getminbuffersize ( Ksamplerate, kchannelmode,kencodeformat); Mrecord = new Audiorecord (MediaRecorder.AudioSource.REMOTE_submix,ksamplerate, Kchannelmode, Kencodeformat, MinBufferSize * 2); Private final int kframesize = 2048;private String FilePath = "/SDCARD/VOICE.PCM";p rivate void Recordandplay () {FileOutput Stream OS = null;mrecord.startrecording (); try {os = new FileOutputStream (FilePath); byte[] buffer = new Byte[kframesize];i NT num = 0;while (!mreqstop) {num = mrecord.read (buffer, 0, kframesize); LOG.D (TAG, "buffer =" + buffer.tostring () + ", num =" + num "), os.write (buffer, 0, num);} LOG.D (TAG, "Exit Loop"); Os.close ();} catch (IOException e) {e.printstacktrace (); LOG.E (TAG, "Dump PCM to file failed");} Mrecord.stop (); Mrecord.release (); mrecord = null; LOG.D (TAG, "clean up");} public void Stop (view view) {Mreqstop = true; Button stopbtn = (button) Findviewbyid (R.ID.STOPBTN); Stopbtn.settext ("Stopped"); stopbtn.setenabled (false);}}

Layout file 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/stopbtn "        android:layout_width=" Wrap_content        " android:layout_height= "Wrap_content"        android:onclick= "Stop"        android:text= "Stop"/></ Relativelayout>

Configuration file Androidmanifest.xml, note the several permissions to add:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.audiotest "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-permi Ssion android:name= "Android.permission.CAPTURE_AUDIO_OUTPUT"/> <uses-permission android:name= " Android.permission.CAPTURE_VIDEO_OUTPUT "/> <uses-permission android:name=" Android.permission.RECORD_AUDIO " /> <uses-sdk android:minsdkversion= "android:targetsdkversion="/> <uses-permission        Android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" &G        T             <activity android:name= "com.example.audiotest.MainActivity" android:label= "@string/app_name" > <intent-filter> <Action android:name= "Android.intent.action.MAIN"/> <category android:name= "android.intent.category.l Auncher "/> </intent-filter> </activity> </application></manifest>

Compile the build test.apk.


2. Add System Signature

Java-jar Signapk.jar Platform.x509.pem platform.pk8./test.apk final.apk


Platform.x509.pem and Platform.pk8 in the Build/target/product/security directory of Android source code.

Signapk.jar can be downloaded from https://code.google.com/p/signapk/.


3. Install the signed apk, run

The system sound data is collected at start-up, and the stop button stops when the end is clicked. The collected data is then exported to the/SDCARD/VOICE.PCM file.


4. Remove the audio sample data and play

ADB PULL/SDCARD/VOICE.PCM

VLC--demux=rawaud--rawaud-channels 2--rawaud-samplerate 44100 VOICE.PCM

This will start playing the sound you just recorded. VLC is a multimedia player (http://www.videolan.org/vlc/index.html) that supports playback of PCM data. It is also possible to use other players that support the playback of the PCM.


In summary, the disadvantage of this method is that the speaker can not be output when recording, the advantage is not to change the system layer. You can refer to this article if you want two simultaneous outputs: http://xzpeter.org/?p=254.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.