daily pastebin goal
7%
SHARE
TWEET

Untitled

a guest Dec 12th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.saintandreas.audiotest;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.content.pm.PackageManager;
  6. import android.media.MediaRecorder;
  7. import android.opengl.GLES32;
  8. import android.opengl.GLSurfaceView;
  9. import android.os.Bundle;
  10. import android.support.annotation.NonNull;
  11. import android.util.Log;
  12.  
  13. import java.io.IOException;
  14. import java.util.Timer;
  15. import java.util.TimerTask;
  16.  
  17. import javax.microedition.khronos.egl.EGLConfig;
  18. import javax.microedition.khronos.opengles.GL10;
  19.  
  20. public class MainActivity extends Activity implements GLSurfaceView.Renderer {
  21.     private final String TAG = MainActivity.class.getName();
  22.     private boolean permissionToRecordAccepted = false;
  23.     private String [] permissions = {Manifest.permission.RECORD_AUDIO};
  24.     private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
  25.     private float maxAudio = 1.0f;
  26.     private float lastAudio = 0.0f;
  27.     private GLSurfaceView mGLView;
  28.     private MediaRecorder mRecorder = null;
  29.  
  30.     //private native void nativeOnCreate();
  31.  
  32.     @Override
  33.     public void onSurfaceCreated(GL10 unused, EGLConfig config) {
  34.     }
  35.  
  36.     @Override
  37.     public void onDrawFrame(GL10 unused) {
  38.         float audioLevel = lastAudio / maxAudio;
  39.         GLES32.glClearColor(audioLevel, audioLevel > 0.2f ? 1.0f :0.0f, 0.0f, 1.0f);
  40.         GLES32.glClear(GLES32.GL_COLOR_BUFFER_BIT);
  41.     }
  42.  
  43.     @Override
  44.     public void onSurfaceChanged(GL10 unused, int width, int height) {
  45.         GLES32.glViewport(0, 0, width, height);
  46.     }
  47.  
  48.  
  49.     @Override
  50.     public void onCreate(Bundle savedInstanceState) {
  51.         System.loadLibrary("audioTest");
  52.         super.onCreate(savedInstanceState);
  53.         requestPermissions(permissions, REQUEST_RECORD_AUDIO_PERMISSION);
  54.         mGLView = new GLSurfaceView(this);
  55.         mGLView.setEGLContextClientVersion(3);
  56.         mGLView.setRenderer(MainActivity.this);
  57.         setContentView(mGLView);
  58.         //nativeOnCreate();
  59.     }
  60.  
  61.     public void monitorAudio() {
  62.         if (mRecorder != null) {
  63.             lastAudio = mRecorder.getMaxAmplitude();
  64.             maxAudio = Math.max(lastAudio, maxAudio);
  65.         }
  66.     }
  67.  
  68.     @Override
  69.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  70.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  71.         switch (requestCode){
  72.             case REQUEST_RECORD_AUDIO_PERMISSION:
  73.                 permissionToRecordAccepted  = grantResults[0] == PackageManager.PERMISSION_GRANTED;
  74.                 break;
  75.         }
  76.         if (!permissionToRecordAccepted ) {
  77.             finish();
  78.         } else {
  79.             new Timer().schedule(new TimerTask() { @Override public void run() { startRecording(); }}, 1000);
  80.         }
  81.     }
  82.  
  83.     Timer mMonitorTimer = null;
  84.  
  85.     private void startRecording() {
  86.         mRecorder = new MediaRecorder();
  87.         mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
  88.         mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  89.         mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  90.         mRecorder.setOutputFile("/dev/null");
  91.         try {
  92.             mRecorder.prepare();
  93.         } catch (IOException e) {
  94.             Log.e(TAG, "prepare() failed");
  95.         }
  96.  
  97.         mRecorder.start();
  98.         mMonitorTimer = new Timer();
  99.         mMonitorTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run(){ monitorAudio(); } }, 100, 100);
  100.     }
  101.  
  102.     @Override
  103.     public void onStop() {
  104.         super.onStop();
  105.         if (mRecorder != null) {
  106.             mMonitorTimer.cancel();
  107.             mMonitorTimer = null;
  108.             mRecorder.release();
  109.             mRecorder = null;
  110.         }
  111.     }
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top