Android Open Source - SurveySDK_android Audio Recorder Fragment






From Project

Back to project page SurveySDK_android.

License

The source code is released under:

Apache License

If you think the Android project SurveySDK_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 com.survey.android.fragment;
/*from  www . j  a  v a2 s . c  om*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.survey.android.R;
import com.survey.android.util.ConstantData;

public class AudioRecorderFragment extends Fragment implements OnInfoListener, OnCompletionListener {
  private static final String TAG = AudioRecorderFragment.class.getSimpleName();
  private String mFileName = null;
  private MediaRecorder mRecorder = null;
  private MediaPlayer mPlayer = null;
  // Whether we have any recorded data.
  private boolean mHasRecorded = false;
  private boolean mIsRecording = false;
  private boolean mIsPlaying = false;
  private int mDurationLimitSeconds;

  private ImageButton getRecordButton() {
    return (ImageButton) getView().findViewById(R.id.button_record);
  }

  private ImageButton getPlayButton() {
    return (ImageButton) getView().findViewById(R.id.button_play);
  }

  private ImageButton getStopButton() {
    return (ImageButton) getView().findViewById(R.id.button_stop);
  }

  private void onStopClicked() {
    if (mIsRecording) {
      stopRecording();
    } else {
      stopPlaying();
    }
  }

  @SuppressWarnings("resource")
  private void startPlaying() {
    if (mIsRecording) {
      stopRecording();
    }

    mPlayer = new MediaPlayer();

    try {
      // Use an FD to get around permission issues here
      File audioFile = new File(mFileName);
      mPlayer.setDataSource(new FileInputStream(audioFile).getFD());
      mPlayer.setOnCompletionListener(this);
      mPlayer.prepare();
      mPlayer.start();
      getRecordButton().setEnabled(false);
      getPlayButton().setEnabled(false);
      getStopButton().setVisibility(View.VISIBLE);
      getStopButton().setEnabled(true);

      Chronometer chronometer = (Chronometer) getView().findViewById(R.id.current_position);
      chronometer.setBase(SystemClock.elapsedRealtime());
      chronometer.start();
      mIsPlaying = true;
    } catch (IOException e) {
      Log.e(TAG, "prepare() failed", e);
      Toast.makeText(getActivity(), R.string.audio_recording_error, Toast.LENGTH_LONG).show();
    }

  }

  private void stopPlaying() {
    if (mPlayer != null) {
      mPlayer.release();
      mPlayer = null;
    }

    getRecordButton().setEnabled(true);
    getStopButton().setEnabled(false);

    if (mHasRecorded) {
      getPlayButton().setVisibility(View.VISIBLE);
      getPlayButton().setEnabled(true);
    }

    Chronometer chronometer = (Chronometer) getView().findViewById(R.id.current_position);
    chronometer.stop();
    mIsPlaying = false;
  }

  @SuppressWarnings("deprecation")
  private void startRecording() {
    if (mIsPlaying) {
      stopPlaying();
    }

    getRecordButton().setEnabled(false);
    getRecordButton().setImageResource(R.drawable.recording_led);
    getPlayButton().setEnabled(false);
    getStopButton().setVisibility(View.VISIBLE);
    getStopButton().setEnabled(true);

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);

    try {
      mRecorder.setOutputFile(mFileName);
      mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      mRecorder.setMaxDuration(mDurationLimitSeconds * 1000);
      mRecorder.setOnInfoListener(this);
      Chronometer chronometer = (Chronometer) getView().findViewById(R.id.current_position);
      chronometer.setBase(SystemClock.elapsedRealtime());
      chronometer.start();
      mIsRecording = true;
    } catch (IllegalStateException e) {
      e.printStackTrace();
    }

    try {
      mRecorder.prepare();
      mRecorder.start();
    } catch (IOException e) {
      Log.e(TAG, "prepare()/start() failed", e);
      Toast.makeText(getActivity(), R.string.audio_recording_error, Toast.LENGTH_LONG).show();
    }

  }

  public void stopRecording() {
    if (mRecorder != null) {
      mRecorder.stop();
      mRecorder.release();
      mRecorder = null;
      mHasRecorded = true;

      getPlayButton().setVisibility(View.VISIBLE);
      getPlayButton().setEnabled(true);
    }

    Chronometer chronometer = (Chronometer) getView().findViewById(R.id.current_position);
    chronometer.stop();
    getRecordButton().setEnabled(true);
    getStopButton().setEnabled(false);
    getRecordButton().setImageResource(R.drawable.record);
    mIsRecording = false;
  }

  @SuppressWarnings("deprecation")
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    if (mFileName == null) {
      File appImagesFolder = getActivity().getDir(
                    "capturedPhotos", Context.MODE_WORLD_WRITEABLE);
      File mFile = new File(appImagesFolder, "audio_answer"
              + SystemClock.elapsedRealtime() + "."
              + ConstantData.AUDIO_QUESTION_FILE_EXTENSION);
      mFileName = mFile.getAbsolutePath();
    }

    View view = inflater.inflate(R.layout.fragment_audio_recorder, container, false);

    final ImageButton recordButton = (ImageButton) view.findViewById(R.id.button_record);

    recordButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        startRecording();
      }
    });

    final ImageButton playButton = (ImageButton) view.findViewById(R.id.button_play);

    playButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        startPlaying();
      }
    });

    playButton.setEnabled(false);

    final ImageButton stopButton = (ImageButton) view.findViewById(R.id.button_stop);

    stopButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        onStopClicked();
      }
    });

    stopButton.setEnabled(false);

    return view;
  }

  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    updateDurationView();
    updateExistingRecordingView();
  }

  @Override
  public void onPause() {
    super.onPause();
    if (mRecorder != null) {
      mRecorder.release();
      mRecorder = null;
    }

    if (mPlayer != null) {
      mPlayer.release();
      mPlayer = null;
    }
  }

  public String getRecordedAudioPath() {
    return mHasRecorded ? mFileName : "";
  }

  public void setDurationLimitSeconds(int durationSeconds) {
    mDurationLimitSeconds = durationSeconds;
    if (getView() != null) {
      updateDurationView();
    }
  }

  private void updateDurationView() {
    TextView length = (TextView) getView().findViewById(R.id.length);
    length.setText(String.format(" / %02d:%02d", mDurationLimitSeconds / 60, mDurationLimitSeconds % 60));
  }

  public void setExistingRecordingFileName(String filename) {
    mFileName = filename;
    mHasRecorded = true;
    if (getView() != null) {
      updateExistingRecordingView();
    }
  }

  private void updateExistingRecordingView() {
    if (mHasRecorded) {
      getStopButton().setVisibility(View.VISIBLE);
      getStopButton().setEnabled(false);
      getPlayButton().setVisibility(View.VISIBLE);
      getPlayButton().setEnabled(true);
    }
  }

  @Override
  public void onInfo(MediaRecorder mr, int what, int extra) {
    // Update the UI when we hit the duration limit to reflect the recording stopping.
    if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
      stopRecording();
    }
  }

  @Override
  public void onCompletion(MediaPlayer mp) {
    stopPlaying();
  }

  public boolean isRecording() {
    return mIsRecording;
  }

  public boolean hasRecorded() {
    return mHasRecorded;
  }
}




Java Source Code List

android.UnusedStub.java
com.google.android.gms.BuildConfig.java
com.google.android.gms.BuildConfig.java
com.survey.android.UnusedStub.java
com.survey.android.c2dm.C2DMRegistrationReceiver.java
com.survey.android.c2dm.C2DMTokenRefresher.java
com.survey.android.c2dm.GcmBroadcastReceiver.java
com.survey.android.c2dm.GcmNotificationReceiver.java
com.survey.android.c2dm.GcmRegistrationService.java
com.survey.android.c2dm.RegService.java
com.survey.android.common.PlacesAutoCompleteAdapter.java
com.survey.android.common.Themes.java
com.survey.android.containers.AppContainer.java
com.survey.android.containers.PollContainer.java
com.survey.android.custom_widgets.FontTextView.java
com.survey.android.custom_widgets.PollRatingsScaleLabeled.java
com.survey.android.custom_widgets.PollRatings.java
com.survey.android.custom_widgets.PollSelectionTable.java
com.survey.android.db.SerializationHelper.java
com.survey.android.db.SerializationManager.java
com.survey.android.fragment.AudioRecorderFragment.java
com.survey.android.geofence.GeofenceRemover.java
com.survey.android.geofence.GeofenceRequester.java
com.survey.android.geofence.GeofenceUtils.java
com.survey.android.geofence.LocationServiceErrorMessages.java
com.survey.android.geofence.ReceiveTransitionsIntentService.java
com.survey.android.geofence.SimpleGeofenceStore.java
com.survey.android.geofence.SimpleGeofence.java
com.survey.android.model.AnswerModel.java
com.survey.android.model.CategoryModel.java
com.survey.android.model.CurrentSectionModel.java
com.survey.android.model.Prefs.java
com.survey.android.model.QuestionModel.java
com.survey.android.model.ResponseModel.java
com.survey.android.model.SurveyModel.java
com.survey.android.model.UserModel.java
com.survey.android.services.BackgroundUploader.java
com.survey.android.services.DataBroadcastReceiver.java
com.survey.android.services.DeviceStartUpReceiver.java
com.survey.android.services.GeoSurveyPollService.java
com.survey.android.services.LocationTesterService.java
com.survey.android.services.ReferrerCatcher.java
com.survey.android.session.Configuration.java
com.survey.android.session.Session.java
com.survey.android.util.Base64.java
com.survey.android.util.ConstantData.java
com.survey.android.util.GeoPush.java
com.survey.android.util.GeoTriggerBroadcastReceiver.java
com.survey.android.util.GeoTrigger.java
com.survey.android.util.LocationLog.java
com.survey.android.util.Log.java
com.survey.android.util.StrToIntMap.java
com.survey.android.util.StrToStrMap.java
com.survey.android.util.Toiler.java
com.survey.android.util.WhiteLabel.java
com.survey.android.view.Dashboard.java
com.survey.android.view.Gallery.java
com.survey.android.view.LocalizedFragmentActivity.java
com.survey.android.view.Main.java
com.survey.android.view.Notification.java
com.survey.android.view.Question.java
com.survey.android.view.Survey.java
com.survey.android.view.ThemeCustomizer.java
com.survey.android.view.themed.DashboardThemed.java
com.survey.android.view.themed.MainThemed.java
com.survey.android.view.themed.NotificationThemed.java
com.survey.android.view.themed.QuestionThemed.java
com.survey.android.view.themed.SurveyThemed.java
com.survey.android.webclient.HttpRequest.java
com.survey.android.webclient.HttpsClient.java
com.survey.android.webclient.RestClient.java
com.survey.android.webclient.SurveyHttpClient.java
com.survey.android.webclient.SurveyRequest.java
com.survey.android.webclient.SurveySSLSocketFactory.java
com.survey.android.webclient.SurveyX509TrustManager.java
com.survey.android.widget.Widget.java
com.survey.androiddemo.AppContainer.java
com.survey.androiddemo.LoginActivity.java
com.survey.androiddemo.MainActivity.java
com.survey.androiddemo.SDKConfigSettings.java