本文主要是介绍Android 4.4中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:
- <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,运行
启动时即开始采集系统声音数据,结束时点击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的播放器。
总结来说,这种方法的缺点是录制时扬声器就不能输出,优点是不用改系统层。如果要两个同时输出可以参考这篇文章:http://xzpeter.org/?p=254。
这篇关于Android 4.4中AudioRecord用例 - 录制系统内置声音的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!