通过API 19新加的MediaRecorder.AudioSource.REMOTE_SUBMIX參数能够让系统App录制系统内置的声音,也就是扬声器的声音。以下是一个巨简单的样例来演示样例怎样通过AudioRecord配合REMOTE_SUBMIX參数进行录制。
1. 编译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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
(new Thread() {
@Override
public void run() {
recordAndPlay();
}
}).start();
}
private final int kSampleRate = 44100;
private final int kChannelMode = AudioFormat.CHANNEL_IN_STEREO;
private final int kEncodeFormat = 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";
private void recordAndPlay() {
FileOutputStream os = null;
mRecord.startRecording();
try {
os = new FileOutputStream(filePath);
byte[] buffer = new byte[kFrameSize];
int 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);
}
}
布局文件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>
配置文件AndroidManifest.xml,注意要加上的几个权限:
<?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-permission 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="19" android:targetSdkVersion="19" /> <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" > <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.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
编译生成Test.apk。
2. 加系统签名
java -jar signapk.jar platform.x509.pem platform.pk8 ./Test.apk final.apk
platform.x509.pem和platform.pk8在Android源代码的build/target/product/security文件夹下。
signapk.jar能够从https://code.google.com/p/signapk/下载。
3. 安装签名好的apk,执行
启动时即開始採集系统声音数据,结束时点击Stopbutton停止。这时採集数据会导出到/sdcard/voice.pcm文件。
4. 取出音频採样数据,播放
adb pull /sdcard/voice.pcm
vlc --demux=rawaud --rawaud-channels 2 --rawaud-samplerate 44100 voice.pcm
这样就開始播放刚才录制的声音了。vlc是一个多媒体播放器(http://www.videolan.org/vlc/index.html)。支持播放PCM数据。当然也能够用其他支持播放PCM的播放器。
总结来说。这样的方法的缺点是录制时扬声器就不能输出,长处是不用改系统层。假设要两个同一时候输出能够參考这篇文章:http://xzpeter.org/?p=254。