我正在尝试从麦克风获取音频数据。我通过使用AudioRecord
类来实现这一点,该类填充了类型为short的缓冲区。
最终我想绘制这个缓冲区的图形,以便得到一个像显示器(实时信息)的示波器。问题是,如果我想显示一个值(比如文本),那么我需要一个不同的线程来更新UI。目前,我正在使用AsyncTask
并使用AsyncTasks.publishProgress()
更新用户界面。到目前为止,我还不是很成功,想知道我是否走在正确的轨道上?手柄是更好的方法吗?之前是否有人做过类似的事情,如果有的话,哪种方法适合你?此外,是否可以简单地轮询麦克风?
这是我的代码。它意味着从MIC输出每个读取样本。它似乎以可接受的速率执行此操作,但偶尔会显示零。为什么呢?
package com.ss.audioacquireapp3;
import android.app.Activity;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class AudioAcquireApp3Activity extends Activity
{
//Properties (AsyncTask)
protected TextView _percentField;
protected InitTask _initTask;
//Properties (MIC)
public AudioRecord audioRecord;
public int mSamplesRead; //how many samples read
public int recordingState;
public int buffersizebytes;
public int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
public static short[] buffer; //+-32767
public static final int SAMPPERSEC = 44100; //samp per sec 8000, 11025, 22050 44100 or 48000
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView( R.layout.main );
_percentField = ( TextView ) findViewById( R.id.percent_field );
buffersizebytes = AudioRecord.getMinBufferSize(SAMPPERSEC,channelConfiguration,audioEncoding); //4096 on ion
buffer = new short[buffersizebytes];
audioRecord = new AudioRecord(android.media.MediaRecorder.AudioSource.MIC,SAMPPERSEC,channelConfiguration,audioEncoding,buffersizebytes); //constructor
_initTask = new InitTask();
_initTask.execute( this );
}
/**
* sub-class of AsyncTask
*/
protected class InitTask extends AsyncTask<Context, Integer, String>
{
// -- run intensive processes here
// -- notice that the datatype of the first param in the class definition matches the param passed to this method
// -- and that the datatype of the last param in the class definition matches the return type of this method
@Override
protected String doInBackground( Context... params )
{
//-- on every iteration
//-- runs a while loop that causes the thread to sleep for 50 milliseconds
//-- publishes the progress - calls the onProgressUpdate handler defined below
//-- and increments the counter variable i by one
//int i = 0;
audioRecord.startRecording();
while( true )
{
try{
mSamplesRead = audioRecord.read(buffer, 0, buffersizebytes);
int amp;
for(int i = 0; i < buffersizebytes - 1; i++){
amp = (int)buffer[i];
publishProgress( amp );
}
} catch( Exception e ){
}
}
}
// -- gets called just before thread begins
@Override
protected void onPreExecute()
{
//Log.i( "makemachine", "onPreExecute()" );
super.onPreExecute();
}
// -- called from the publish progress
// -- notice that the datatype of the second param gets passed to this method
@Override
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
//Log.i( "makemachine", "onProgressUpdate(): " + String.valueOf( values[0] ) );
_percentField.setText( String.valueOf(values[0]) );
}
// -- called as soon as doInBackground method completes
// -- notice that the third param gets passed to this method
@Override
protected void onPostExecute( String result )
{
super.onPostExecute(result);
//Log.i( "makemachine", "onPostExecute(): " + result );
}
}
}
这是main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
>
<TextView android:id="@+id/percent_field"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>
请注意,您需要将其添加到AndroidManifest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
我在LG Optimus Black上运行它。请帮助我尽可能高效地使用此代码。
答案 0 :(得分:1)
Activity.runOnUiThread(Runnable action)
。有关详细信息,请参阅Activity
class javadoc。AudioRecord
是录制微型电话的好方法。以下是示例实现的链接,它实时读取录制的音频的频率:http://www.anddev.org/novice-tutorials-f8/get-frequency-data-from-microphone-in-real-time-t16774.html 希望这有帮助。
答案 1 :(得分:1)
这是一个迟到的答案,但也许有人需要对同一个问题的答案。这是开源Android示波器(OsciPrime)链接(http://android.serverbox.ch/?p=268)。源代码使用Thread而不是AsyncTask。如果你看到源代码,你可以弄清楚Thread如何使用Looper和Handler处理AudioRecord。我希望它对其他人有用:)