Android Open Source - AndroidRecording Audio Recording Activity






From Project

Back to project page AndroidRecording.

License

The source code is released under:

Apache License

If you think the Android project AndroidRecording listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.skd.androidrecordingtest;
//  w  w  w  . jav a  2s.  co m
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.skd.androidrecording.audio.AudioRecordingHandler;
import com.skd.androidrecording.audio.AudioRecordingThread;
import com.skd.androidrecording.visualizer.VisualizerView;
import com.skd.androidrecording.visualizer.renderer.BarGraphRenderer;
import com.skd.androidrecordingtest.utils.NotificationUtils;
import com.skd.androidrecordingtest.utils.StorageUtils;

public class AudioRecordingActivity extends Activity {
  private static String fileName = null;
    
  private Button recordBtn, playBtn;
  private VisualizerView visualizerView;
  
  private AudioRecordingThread recordingThread;
  private boolean startRecording = true;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.audio_rec);
    
    if (!StorageUtils.checkExternalStorageAvailable()) {
      NotificationUtils.showInfoDialog(this, getString(R.string.noExtStorageAvailable));
      return;
    }
    fileName = StorageUtils.getFileName(true);
    
    recordBtn = (Button) findViewById(R.id.recordBtn);
    recordBtn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        record();
      }
    });
    
    playBtn = (Button) findViewById(R.id.playBtn);
    playBtn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        play();
      }
    });
    
    visualizerView = (VisualizerView) findViewById(R.id.visualizerView);
    setupVisualizer();
  }
  
  @Override
  protected void onPause() {
    super.onPause();
    
    recordStop();
  }
  
  @Override
  protected void onDestroy() {
    recordStop();
    releaseVisualizer();
    
    super.onDestroy();
  }
  
  private void setupVisualizer() {
    Paint paint = new Paint();
        paint.setStrokeWidth(5f);
        paint.setAntiAlias(true);
        paint.setColor(Color.argb(200, 227, 69, 53));
        BarGraphRenderer barGraphRendererBottom = new BarGraphRenderer(2, paint, false);
        visualizerView.addRenderer(barGraphRendererBottom);
  }
  
  private void releaseVisualizer() {
    visualizerView.release();
    visualizerView = null;
  }
  
  private void record() {
        if (startRecording) {
          recordStart();
        }
        else {
          recordStop();
        }
  }
  
  private void recordStart() {
    startRecording();
      startRecording = false;
      recordBtn.setText(R.string.stopRecordBtn);
      playBtn.setEnabled(false);
  }
  
  private void recordStop() {
    stopRecording();
    startRecording = true;
      recordBtn.setText(R.string.recordBtn);
      playBtn.setEnabled(true);
  }
  
  private void startRecording() {
      recordingThread = new AudioRecordingThread(fileName, new AudioRecordingHandler() {
      @Override
      public void onFftDataCapture(final byte[] bytes) {
        runOnUiThread(new Runnable() {
          public void run() {
            if (visualizerView != null) {
              visualizerView.updateVisualizerFFT(bytes);
            }
          }
        });
      }

      @Override
      public void onRecordSuccess() {}

      @Override
      public void onRecordingError() {
        runOnUiThread(new Runnable() {
          public void run() {
            recordStop();
            NotificationUtils.showInfoDialog(AudioRecordingActivity.this, getString(R.string.recordingError));
          }
        });
      }

      @Override
      public void onRecordSaveError() {
        runOnUiThread(new Runnable() {
          public void run() {
            recordStop();
            NotificationUtils.showInfoDialog(AudioRecordingActivity.this, getString(R.string.saveRecordError));
          }
        });
      }
    });
      recordingThread.start();
    }
    
    private void stopRecording() {
      if (recordingThread != null) {
        recordingThread.stopRecording();
        recordingThread = null;
      }
    }

    private void play() {
    Intent i = new Intent(AudioRecordingActivity.this, AudioPlaybackActivity.class);
    i.putExtra(VideoPlaybackActivity.FileNameArg, fileName);
    startActivityForResult(i, 0);
  }
}




Java Source Code List

com.skd.androidrecording.audio.AudioPlaybackManager.java
com.skd.androidrecording.audio.AudioRecordingHandler.java
com.skd.androidrecording.audio.AudioRecordingThread.java
com.skd.androidrecording.fft.Complex.java
com.skd.androidrecording.fft.FFT.java
com.skd.androidrecording.video.AdaptiveSurfaceView.java
com.skd.androidrecording.video.CameraHelper.java
com.skd.androidrecording.video.CameraManager.java
com.skd.androidrecording.video.MediaPlayerManager.java
com.skd.androidrecording.video.MediaRecorderManager.java
com.skd.androidrecording.video.PlaybackHandler.java
com.skd.androidrecording.video.VideoPlaybackManager.java
com.skd.androidrecording.video.VideoRecordingHandler.java
com.skd.androidrecording.video.VideoRecordingManager.java
com.skd.androidrecording.visualizer.AudioData.java
com.skd.androidrecording.visualizer.FFTData.java
com.skd.androidrecording.visualizer.VisualizerView.java
com.skd.androidrecording.visualizer.renderer.BarGraphRenderer.java
com.skd.androidrecording.visualizer.renderer.Renderer.java
com.skd.androidrecordingtest.AudioPlaybackActivity.java
com.skd.androidrecordingtest.AudioRecordingActivity.java
com.skd.androidrecordingtest.MainActivity.java
com.skd.androidrecordingtest.SizeAdapter.java
com.skd.androidrecordingtest.VideoPlaybackActivity.java
com.skd.androidrecordingtest.VideoRecordingActivity.java
com.skd.androidrecordingtest.utils.NotificationUtils.java
com.skd.androidrecordingtest.utils.StorageUtils.java