Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
This crashes now, but in theory it should do basic record and playback.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpatrick committed Sep 1, 2011
1 parent 9c6115f commit d090046
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/org/miloss/TalkActivity.java
@@ -1,13 +1,27 @@
package org.miloss;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;

import android.app.Activity;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioRecord.OnRecordPositionUpdateListener;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

public class TalkActivity extends Activity {

Expand All @@ -17,6 +31,12 @@ public class TalkActivity extends Activity {
Button mTalkButton;
Button mGuysButton;

/**
* Media player and recorder
*/
AudioRecord mRecorder;
ByteBuffer mAudioBuffer;

public static final String TAG = "TalkActivity";

/**
Expand All @@ -29,9 +49,30 @@ public boolean onTouch(View yourButton, MotionEvent motion) {
switch (motion.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "Talk Down");
try {
startRecording();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "Talk Up");
try {
stopRecording();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}

Expand All @@ -48,6 +89,28 @@ public void onClick(View v) {
}
};

private void startRecording() throws IllegalStateException, IOException {
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
FileOutputStream file = new FileOutputStream("/sdcard/output.sound");
recorder.setOutputFile(file.getFD());
recorder.prepare();
recorder.start();
}

private void stopRecording() throws IllegalArgumentException, IllegalStateException, IOException {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("/sdcard/output.sound");
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
}

/** Get the refs to the ui elements on the page */
public void setupUIReferences() {
mTalkButton = (Button)this.findViewById(R.id.talkButton);
Expand All @@ -65,6 +128,7 @@ public void setupEventListeners()
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
setupUIReferences();
setupEventListeners();
Expand Down

0 comments on commit d090046

Please sign in to comment.