Android通过AudioRecord录制系统内置声音

转自:http://blog.csdn.net/jinzhuojun/article/details/33748031

通过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:

  
  
    

配置文件AndroidManifest.xml,注意要加上的几个权限:

  
  
  
      
      
      
      
      
      
      
          
              
                  
  
                  
              
          
      
  
  

编译生成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,运行

启动时即开始采集系统声音数据,结束时点击Stop按钮停止。这时采集数据会导出到/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的播放器。


总结来说,这种方法的缺点是录制时扬声器就不能输出,优点是不用改系统层。

你可能感兴趣的:(android应用)