Android Open Source - jpstrack.android Voice Note Activity






From Project

Back to project page jpstrack.android.

License

The source code is released under:

/* * Copyright (c) 2006-2014 Ian F. Darwin, http://darwinsys.com * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following co...

If you think the Android project jpstrack.android 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 jpstrack.android;
//from w w  w . j av  a2  s. c  o  m
import java.io.File;

import android.app.Activity;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * Offer a "voice note" facility so the user can record comments.
 * We use this instead of the default Sound Record app since unlike
 * the standard Video Recorder, the standard Voice Recorder app
 * blatantly ignores Intent extra "Extra Output" for filename.
 * 
 * onCreate() calls startRecording(), since the user already pressed a button
 * with "start recording" function; we offer discard and save buttons only.
 * @author Ian Darwin
 */
public class VoiceNoteActivity extends Activity implements OnClickListener {
  MediaRecorder recorder  = null;
  private String soundFile;
  private boolean recording;
  
  class Wrapper {
    MediaRecorder recorder;
    String soundFile;
    @Override
    public String toString() {
      return "Wrapper[" + recorder + "; " + soundFile + "]";
    }
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!MainActivity.isSdWritable()) {
      Toast.makeText(this, "SD Card not writable", Toast.LENGTH_LONG).show();
      finish();
      return;
    }
    
    // View has only Save and Discard buttons, 
    setContentView(R.layout.voicenote);
    View saveButton = findViewById(R.id.voicenote_save_button);
    saveButton.setOnClickListener(this);
    View discardButton = findViewById(R.id.voicenote_discard_button);
    discardButton.setOnClickListener(this);
    Wrapper w = (Wrapper) getLastNonConfigurationInstance();
    Log.d(MainActivity.TAG, "LastConConfigurationInstance gave us " + w);
    if (w == null) {
      startRecording();  // Start immediately - already pressed Voice Note button
    } else {
      continueRecording(w);
    }
  }
  
  @Override
  public void onClick(View v) {
    int source = v.getId();
    switch(source) {
    case R.id.voicenote_save_button:
      saveRecording();
      Toast.makeText(this, "Saved voice note into " + soundFile, Toast.LENGTH_SHORT).show();
      break;
    case R.id.voicenote_discard_button:
      discardRecording();
      break;
    default:
      Log.e(MainActivity.TAG, "Unexpected click");
    }
    this.finish();    // Back to main!
  }
  
  @Override
  public void onBackPressed() {
    if (recording) {
      return;      // Can't back out, must save or cancel.
    }
    super.onBackPressed();
  }

  protected void startRecording() {
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    try {
      Uri soundUri = getIntent().getParcelableExtra(MediaStore.EXTRA_OUTPUT);      
      soundFile = soundUri.getPath();
      recorder.setOutputFile(soundFile);
      Log.d(MainActivity.TAG, "outputting to " + soundUri.getPath());
      recorder.prepare();
      recorder.start();
      recording = true;
    } catch (Exception e) {
      final String message = "Could not create file:" + e;
      Log.e(MainActivity.TAG, message);
      Toast.makeText(this, message, Toast.LENGTH_LONG).show();
      this.finish();
    }
  }
  
  /**
   * Restore state after configuration change (rotation)
   * @param w
   */
  private void continueRecording(Wrapper w) {
    soundFile = w.soundFile;
    recorder = w.recorder;
  }

  
  protected void discardRecording() {
    recorder.stop();
    recording = false;
    recorder.release();
    new File(soundFile).delete();
  }

  protected void saveRecording() {
    recorder.stop();
    recording = false;
    recorder.release();
    // We don't tell the MediaStore about it as it's not music!
  }
  
  @Override
  public Object onRetainNonConfigurationInstance() {
    Wrapper w = new Wrapper();
    w.soundFile = soundFile;
    w.recorder = recorder;
    return w;
  }
}




Java Source Code List

jpstrack.android.ActionBarActivity.java
jpstrack.android.ActionBarHelperBase.java
jpstrack.android.ActionBarHelperHoneycomb.java
jpstrack.android.ActionBarHelperICS.java
jpstrack.android.ActionBarHelper.java
jpstrack.android.AndroidApplication.java
jpstrack.android.ButtonSnazzler.java
jpstrack.android.MainActivity.java
jpstrack.android.OnboardingActivity.java
jpstrack.android.SettingsActivity.java
jpstrack.android.ShowWelcomePage.java
jpstrack.android.SimpleMenuItem.java
jpstrack.android.SimpleMenu.java
jpstrack.android.TextNoteActivity.java
jpstrack.android.ThreadUtils.java
jpstrack.android.TrackerName.java
jpstrack.android.VoiceNoteActivity.java