我是Android新手,我正在開發Android Studio上的聽力計。其中一個步驟是查看話筒是否收到我從耳機發出的聲音,以檢查它們是否正常工作。Android:檢查話筒是否收到任何聲音
我在同一個活動做兩件事,發送聲音,檢查是否有在未來的任何聲音。
我能夠發送頻率爲1kHz的基調2秒,使用AudioTrack類,下一步是檢查話筒是否正在接近該頻率。由於我甚至無法使麥克風工作,我正在降低目標以檢查麥克風是否接收到任何東西。
我檢查了幾個環節,並沒有幫助我,還是因爲我不熟悉Android或因爲那不是我所需要的,其中包括: Detect sound level,How to detect when a user stops talking into the microphone和Detect 'Whistle' sound in android
我已經穿上清單的權限:
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
而且我CalibrationActivity.java是:
import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.io.IOException;
public class CalibrationActivity extends AppCompatActivity {
private MediaRecorder myRecorder;
private String outputFile = null;
private final int duration = 2; // seconds
private final int sampleRate = 4000;
private final int numSamples = duration * sampleRate;
private final double sample[] = new double[numSamples];
private final double freqOfTone = 1000; // hz
private final byte generatedSnd[] = new byte[2 * numSamples];
Handler handler = new Handler();
int getAmplitude = myRecorder.getMaxAmplitude();
public int result(){
if (getAmplitude != 0) {
return 1;
}else {
return 0;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calibration);
outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/teste.3gpp";
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myRecorder.setOutputFile(outputFile);
Intent intent;
if(result()==1){
intent = new Intent(this, FirstTestActivity.class);
}else{
intent = new Intent(this, End1Activity.class);
}
}
void start_recording() {
try {
myRecorder.prepare();
myRecorder.start();
} catch (IllegalStateException e) {
// start:it is called before prepare()
// prepare: it is called after start() or before setOutputFormat()
e.printStackTrace();
} catch (IOException e) {
// prepare() fails
e.printStackTrace();
}
}
void stop_recording(){
try {
myRecorder.stop();
myRecorder.release();
myRecorder = null;
} catch (IllegalStateException e) {
// it is called before start()
e.printStackTrace();
} catch (RuntimeException e) {
// no valid audio/video data has been received
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
// Use a new tread as this can take a while
final Thread thread = new Thread(new Runnable() {
public void run() {
genTone();
handler.post(new Runnable() {
public void run() {
playSound();
}
});
}
});
thread.start();
start_recording();
SystemClock.sleep(3000);
stop_recording();
}
void genTone() {
// fill out the array
for (int i = 0; i < numSamples; ++i) {
sample[i] = Math.sin(2 * Math.PI * i/(sampleRate/freqOfTone));
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
}
void playSound() {
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
}
我寫的基於我在網上找到的例子,主要是從here,here和here,所以有一些代碼的部分我不是很懂。
這裏的想法是發送來自耳機的聲音,並且將通知用戶將他們的耳機靠近麥克風。然後,代碼應該讓麥克風錄音3秒鐘,然後檢查聲音的幅度是否與0不同,如果情況如此,則應用程序轉到FirstTestActivity,否則轉到End1Activity。但是,一旦我嘗試運行代碼,應用程序突然崩潰,我不知道爲什麼。我一直在爲此工作數星期,但我找不到可能非常簡單的解決方案。預先感謝。