main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:gravity="center_vertical|center_horizontal"> <TextView android:text="dB" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="40dp" android:id="@+id/tv1" android:gravity="center_vertical|center_horizontal" android:textStyle="bold" /> 

Manifest:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication" > <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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> 

MainActivity:

 import android.app.Activity; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.Bundle; import android.widget.TextView; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends Activity { public static TextView tv1; public int max = 0; public static Timer mTimer; public static MyTimerTask mMyTimerTask; public int minSize; public AudioRecord ar; public short[] buffer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); minSize = AudioRecord.getMinBufferSize(16000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 16000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minSize); tv1 =(TextView) findViewById(R.id.tv1); mTimer = new Timer(); mMyTimerTask = new MyTimerTask(); mTimer.schedule(mMyTimerTask, 0, 400); } class MyTimerTask extends TimerTask { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { if (ar.getState() == AudioRecord.RECORDSTATE_STOPPED) { buffer = new short[minSize]; ar.startRecording(); ar.read(buffer, 0, minSize); max = 0; for (int i = 0; i < minSize; i++) { short b = buffer[i]; if (b < 0) b = (short) -b; if (b > max) max = b; } tv1.setText("" + max); ar.stop(); } } }); } } @Override public void onBackPressed() { if (mTimer != null) { mTimer.cancel(); mTimer = null; } finish(); } } 

This is a working version for Android OS 4.1. Tell me, pliz, why does not work for the 5th version? And is there an alternative solution? Thanks in advance...

  • And what specifically does not work in version 5? An error occurs or what? - Andrew Bystrov
  • Always shows zero. No errors. You can copy all 3 files respectively into your new project and see directly! - LEO
  • Debazh not tried? - Andrew Bystrov
  • I tried. I do not find! So I decided, maybe there is an alternative, simpler solution? - LEO
  • one
    I mean that no one will debug / look for a mistake for you. If you say that "such a mistake is in this place, what could be the reason," then they will help you - Andrew Bystrov

1 answer 1

 class MyThread extends Thread { private boolean stop; @Override public void run() { AudioRecord ar; short[] audioBuffer; int MHz = 16000; int ms = 1; int minSize = AudioRecord.getMinBufferSize(MHz, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); ar = new AudioRecord(MediaRecorder.AudioSource.MIC, MHz, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minSize); audioBuffer = new short[minSize]; ar.startRecording(); stop = false; int delay = 0; while (!stop) { final int readSize = ar.read(audioBuffer, 0, minSize); delay += readSize; double amplitude = 0; double sum=0; for (int i = 0; i < readSize; i++) { sum += audioBuffer[i] * audioBuffer[i]; } amplitude = sum / readSize / 10000.; if(delay > MHz*ms/10) { // ms*100 миллисекунд delay = 0; final int finalAmplitude = (int) amplitude; runOnUiThread(new Runnable() { @Override public void run() { tv1.setText(""+finalAmplitude); } }); } } ar.stop(); } public void stopRecording() {stop = true;} } @Override public void onBackPressed() { if (thread != null) thread.stopRecording(); finish(); } 

}