بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
تشخیص میزان صدای محیط با میکروفون ؟
سلام دوستان من می خوام یک لیسنر طراحی کنم _یا یک ترد_که هر وقت صدای محیط از یک مقداری کمتر شد یک کاری رو انجام بده کسی در این مورد اطلاعی داره؟
هرچی باشه حتی یک ایده ممنون می شم کمکم کنید
یک نمونه کد پیدا کردم که با صحبت کردن باید برنامه عکس یک لب رو حرکت بده اما کار نمی کنه و من هم نمی دونم اشکال کدش از کجاست:
public class MainActivity extends Activity {
private static final int sampleRate = 8000;
private AudioRecord audio;
private int bufferSize;
private double lastLevel = 0;
private Thread thread;
private static final int SAMPLE_DELAY = 75;
private ImageView mouthImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mouthImage = (ImageView) findViewById(R.id.mounthHolder);
mouthImage.setKeepScreenOn(true);
try {
bufferSize = AudioRecord
.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
}
catch (Exception e) {
android.util.Log.e("TrackingFlow", "Exception", e);
}
}
@Override
protected void onResume() {
super.onResume();
audio = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);
audio.startRecording();
thread = new Thread(new Runnable() {
@Override
public void run() {
while (thread != null && !thread.isInterrupted()) {
//Let's make the thread sleep for a the approximate sampling time
try {
Thread.sleep(SAMPLE_DELAY);
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
readAudioBuffer();//After this call we can get the last value assigned to the lastLevel variable
runOnUiThread(new Runnable() {
@Override
public void run() {
if (lastLevel > 0 && lastLevel <= 50) {
mouthImage.setImageResource(R.drawable.mouth4);
} else
if (lastLevel > 50 && lastLevel <= 100) {
mouthImage.setImageResource(R.drawable.mouth3);
} else
if (lastLevel > 100 && lastLevel <= 170) {
mouthImage.setImageResource(R.drawable.mouth2);
}
if (lastLevel > 170) {
mouthImage.setImageResource(R.drawable.mouth1);
}
}
});
}
}
});
thread.start();
}
/**
* Functionality that gets the sound level out of the sample
*/
private void readAudioBuffer() {
try {
short[] buffer = new short[bufferSize];
int bufferReadResult = 1;
if (audio != null) {
// Sense the voice...
bufferReadResult = audio.read(buffer, 0, bufferSize);
double sumLevel = 0;
for (int i = 0; i < bufferReadResult; i++) {
sumLevel += buffer[i];
}
lastLevel = Math.abs((sumLevel / bufferReadResult));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onPause() {
super.onPause();
thread.interrupt();
thread = null;
try {
if (audio != null) {
audio.stop();
audio.release();
audio = null;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
0
0
دوستان ایا کسی هست که کمک کنه؟ (10 سال پیش)
0
0
لینک و لینک (10 سال پیش)
0
0
سلام به همه دوستان کسی اگر ایده ای در مورد سوالم داشته باشه یا لینکی که کمک کنه ممنون می شم (10 سال پیش)
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .