Android Open Source - sana Sound Element






From Project

Back to project page sana.

License

The source code is released under:

Copyright (c) 2010, Moca All rights reserved. The source code for Moca is licensed under the BSD license as follows: Redistribution and use in source and binary forms, with or without modification, ...

If you think the Android project sana 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 org.moca.procedure;
/*from   w  w w  .  j av  a2  s  .  c  om*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.moca.Constants;
import org.moca.db.MocaDB.SoundSQLFormat;
import org.w3c.dom.Node;

import android.content.ContentValues;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

/**
 * SoundElement is a ProcedureElement that asks a question and allows a user to record 
 * a response. This sound is saved to a temp file on the phone's SD card and then sent 
 * to a database on the phone. Once a recording is made, the user has the option of 
 * re-recording.
 * 
 * This element is useful in several clinical scenarios such as recording extra notes or
 * recording the patient cough, etc.
 */
public class SoundElement extends ProcedureElement implements OnClickListener {

    private Button startRecButt;
    private Button endRecButt;
    private TextView textViewSound;
    private ImageView imageViewSound;
    private MediaRecorder recorder;
    private String path;
    private File tempSoundFile;
    
    @Override
    public ElementType getType() {
        return ElementType.SOUND;
    }

    protected View createView(Context c) {
        LinearLayout soundContainer = new LinearLayout(c);
        soundContainer.setOrientation(LinearLayout.VERTICAL);
        
        path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/testRecording.3gp";
        
        if(question == null) {
            question = "Record audio:";
        }

        //Set display question
        textViewSound = new TextView(c);
        textViewSound.setText(question);
        textViewSound.setGravity(Gravity.CENTER);
        textViewSound.setTextAppearance(c, android.R.style.TextAppearance_Medium);
        
        //Set accompanying figure
        imageViewSound = new ImageView(c);
        if(!figure.equals("")){    
          try{
            String imagePath = c.getPackageName() + ":" + figure;
            int resID = c.getResources().getIdentifier(imagePath, null, null);
            imageViewSound.setImageResource(resID);
            imageViewSound.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
            imageViewSound.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            imageViewSound.setPadding(10,10,10,10);
          }
          catch(Exception e){
            Log.e(TAG, "Couldn't find resource figure " + e.toString());
          }
        }
        
        //Add to page
        soundContainer.addView(textViewSound, new LinearLayout.LayoutParams(-1, -1, 0.1f));
        soundContainer.addView(imageViewSound, new LinearLayout.LayoutParams(-1, -1, 0.1f));
        
        //Initialize audio control buttons
        //Start Record Button
        startRecButt = new Button(c);
        startRecButt.setText("Start Recording");
        startRecButt.setOnClickListener(this);
        
        //End Record Button (disabled initially)
        endRecButt = new Button(c);
        endRecButt.setText("Stop Recording");
        endRecButt.setEnabled(false);
        endRecButt.setOnClickListener(this);
        
        //Add to page
        soundContainer.addView(startRecButt, new LinearLayout.LayoutParams(-1, -1, 0.1f));
        soundContainer.addView(endRecButt, new LinearLayout.LayoutParams(-1, -1, 0.1f));
        
        return soundContainer;
    }
    
    public void onClick(View v) {
      //Start Record button clicked
        if (v == startRecButt) {
          
          textViewSound.setText(question);
          
            //toggle record buttons
            startRecButt.setEnabled(false);
            endRecButt.setEnabled(true);
            
            String state = android.os.Environment.getExternalStorageState();
            if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
              Log.e(TAG, "SD Card is not mounted.  It is " + state + ".");
            }

            // make sure the directory we plan to store the recording in exists
            File directory = new File(path).getParentFile();
            if (!directory.exists() && !directory.mkdirs()) {
              Log.e(TAG, "Path to file could not be created.");
            }
              
            //create new recorder
            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(path);
            
            //start recording
            try {
        recorder.prepare();
        recorder.start();
        endRecButt.setText("Stop Recording");
      } 
            catch (Exception e) {
        endRecButt.setText("Recording Error");
        Log.e(TAG, "Couldn't setup audio recorder: " + e);
      }
        } 
        //End Record button clicked
        else if (v == endRecButt) {
          
          startRecButt.setEnabled(true);
          
          // if we are still in the recording state
          // then allow the user to stop recording
          if (!(endRecButt.getText().equals("Play Back Recording"))) {
            
            // stop recording
            textViewSound.setText("Recording Complete!");
                recorder.stop();
                recorder.release();
                endRecButt.setText("Play Back Recording");
                
                
                Thread t = new Thread() {
          public void run() {
            
              // now write the file to the database
                    ContentValues values = new ContentValues();
                    String procedureId = getProcedure().getInstanceUri().getPathSegments().get(1);
                    values.put(SoundSQLFormat.SAVED_PROCEDURE_ID, procedureId);
                    values.put(SoundSQLFormat.ELEMENT_ID, getId());
                    Uri recording = getContext().getContentResolver().insert(SoundSQLFormat.CONTENT_URI, values);
           
                    // Make this the answer we return 
                    setAnswer(recording.getPathSegments().get(1));
                    
            try {
              byte[] buffer = new byte[1024];
              
              OutputStream os =  getContext().getContentResolver().openOutputStream(recording);
              
              InputStream is = new FileInputStream(path);
                      os = getContext().getContentResolver().openOutputStream(recording);
    
                        int bytesRemaining = is.available();
                        while(bytesRemaining > 0) {
                          int read = is.read(buffer);
                          os.write(buffer, 0, read);
                          bytesRemaining -= read;
                        }
    
                        is.close();
              os.flush();
              os.close();
              Log.i(TAG, "Successfully saved audio");
              
            } catch (FileNotFoundException e) {
              Log.e(TAG, "While storing the audio, got an exception: " + e.toString());
            } catch (IOException e) {
              Log.e(TAG, "While storing the audio, got an exception: " + e.toString());
              Log.i(TAG, e.getStackTrace().toString());
            } 
          }
              
        };
        t.start();
                
                
//                
//                InputStream is = null;
//                OutputStream os = null;
//                try {
//                  byte[] buffer = new byte[1024];
//                  
//                  //new stuff
//                  tempSoundFile = new File(Environment.getExternalStorageDirectory(), "testRecording.3gp");
//            Uri tempSoundUri = Uri.fromFile(tempSoundFile);
//                  is = new FileInputStream(path);
//                  os = getContext().getContentResolver().openOutputStream(recording);
//
//                    int bytesRemaining = is.available();
//                    while(bytesRemaining > 0) {
//                      int read = is.read(buffer);
//                      os.write(buffer, 0, read);
//                      bytesRemaining -= read;
//                    }
//
//                    is.close();
//                    os.close();
//                } catch (FileNotFoundException e) {
//          Log.i(TAG, "could not find audio temp file");
//        } catch (IOException e) {
//          Log.i(TAG, "IO exception in writing audio to database " + e.toString());
//          Log.i(TAG, e.getStackTrace().toString());
//        } finally {
//          try {
//            if (os != null)
//              os.close();
//          } catch (IOException e) {
//
//          }
//          try {
//            if (is != null)
//              is.close();
//          } catch (IOException e) {
//
//          }
//
//        }

            // if we are in a completed state
            // then allow audio playback
          } else {
            MediaPlayer mp = new MediaPlayer();
              try {
          mp.setDataSource(path);
          mp.prepare();
                mp.start();
        } catch (Exception e) {
          return;
        }
        startRecButt.setText("Rerecord");
          }  
        }    
    }
    
    public void setAnswer(String answer) {
      this.answer = answer;
    }
    
    /**
     * Returns a string indicating whether a sound was recorded or not. If sound was 
     * recorded, getAnswer returns "sound."
     */
    public String getAnswer() {
//        if(!isViewActive())
//            return answer;
        return answer;
    }
    
    /**
     * Make question and answer=sound (if sound was recorded) into an XML string 
     * for storing or transmission.
     */
    public void buildXML(StringBuilder sb) {
      sb.append("<Element type=\"" + getType().name() + "\" id=\"" + id);
        sb.append("\" answer=\"" + getAnswer());
        sb.append("\" concept=\"" + getConcept());
        sb.append("\"/>\n");
    }
    
    private SoundElement(String id, String question, String answer, String concept, String figure, String audio) {
        super(id,question,answer, concept, figure, audio);
    }
    
    /**
     * Create a SoundElement from an XML procedure definition.
     */
    public static SoundElement fromXML(String id, String question, String answer, String concept, String figure, String audio, Node node) {
        return new SoundElement(id, question, answer, concept, figure, audio);
    }

}




Java Source Code List

.Moca.java
org.moca.Constants.java
org.moca.ImagePreviewDialog.java
org.moca.ScalingImageAdapter.java
org.moca.SelectableImageView.java
org.moca.activity.NotificationList.java
org.moca.activity.NotificationViewer.java
org.moca.activity.PatientInfoDialog.java
org.moca.activity.ProcedureRunner.java
org.moca.activity.ProceduresList.java
org.moca.activity.SavedProcedureList.java
org.moca.activity.Settings.java
org.moca.db.EncounterDAO.java
org.moca.db.EventDAO.java
org.moca.db.EventProvider.java
org.moca.db.Event.java
org.moca.db.ImageProvider.java
org.moca.db.MocaDB.java
org.moca.db.NotificationMessage.java
org.moca.db.NotificationProvider.java
org.moca.db.PatientInfo.java
org.moca.db.PatientProvider.java
org.moca.db.PatientValidator.java
org.moca.db.ProcedureDAO.java
org.moca.db.ProcedureProvider.java
org.moca.db.SavedProcedureProvider.java
org.moca.db.SoundProvider.java
org.moca.media.AudioPlayer.java
org.moca.net.MDSCode.java
org.moca.net.MDSInterface.java
org.moca.net.MDSNotification.java
org.moca.net.MDSResult.java
org.moca.net.SMSReceive.java
org.moca.procedure.BinaryUploadElement.java
org.moca.procedure.DateElement.java
org.moca.procedure.GpsElement.java
org.moca.procedure.MultiSelectElement.java
org.moca.procedure.PatientIdElement.java
org.moca.procedure.PictureElement.java
org.moca.procedure.ProcedureElement.java
org.moca.procedure.ProcedurePage.java
org.moca.procedure.ProcedureParseException.java
org.moca.procedure.Procedure.java
org.moca.procedure.RadioElement.java
org.moca.procedure.SelectElement.java
org.moca.procedure.SoundElement.java
org.moca.procedure.TextElement.java
org.moca.procedure.TextEntryElement.java
org.moca.procedure.ValidationError.java
org.moca.procedure.branching.Criteria.java
org.moca.procedure.branching.Criterion.java
org.moca.procedure.branching.LogicAnd.java
org.moca.procedure.branching.LogicBase.java
org.moca.procedure.branching.LogicNot.java
org.moca.procedure.branching.LogicOr.java
org.moca.service.BackgroundUploader.java
org.moca.service.QueueManager.java
org.moca.service.ServiceConnector.java
org.moca.service.ServiceListener.java
org.moca.task.CheckCredentialsTask.java
org.moca.task.ImageProcessingTaskRequest.java
org.moca.task.ImageProcessingTask.java
org.moca.task.MDSSyncTask.java
org.moca.task.PatientLookupListener.java
org.moca.task.PatientLookupTask.java
org.moca.task.ResetDatabaseTask.java
org.moca.task.ValidationListener.java
org.moca.util.MocaUtil.java
org.moca.util.UserDatabase.java