Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.soquestion;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import android.support.v7.app.ActionBarActivity;
- import android.support.v7.app.ActionBar;
- import android.support.v4.app.Fragment;
- import android.media.AudioFormat;
- import android.media.AudioManager;
- import android.media.AudioRecord;
- import android.media.AudioTrack;
- import android.media.MediaRecorder;
- import android.media.MediaRecorder.AudioSource;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.os.Build;
- public class MainActivity extends ActionBarActivity {
- protected static final String TAG = "SO_QUESTION";
- private int BufferSize;
- byte[] buffer = new byte[BufferSize];
- private AudioRecord recorder = null;
- private AudioTrack track = null;
- private int sampleRate = 8000;
- private int channelConfig = AudioFormat.CHANNEL_IN_MONO;
- private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
- private boolean isRecording = true;
- private Thread recordingThread = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setContentView(R.layout.activity_main);
- setButtonHandlers();
- enableButton(R.id.btnStartRec,true);
- enableButton(R.id.btnStopRec,false);
- enableButton(R.id.btnStartPlay,false);
- enableButton(R.id.btnStopPlay,false);
- BufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
- /*if (savedInstanceState == null) {
- getSupportFragmentManager().beginTransaction()
- .add(R.id.container, new PlaceholderFragment()).commit();
- }*/
- }
- private void writeAudioDataToFile()
- {
- byte data[] = new byte[BufferSize];
- String filename = "/sdcard/audiofile.raw";
- FileOutputStream os = null;
- try {
- os = new FileOutputStream(filename);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- int read = 0;
- if(null != os){
- while(isRecording)
- {
- read = recorder.read(data, 0, BufferSize);
- if(AudioRecord.ERROR_INVALID_OPERATION != read){
- try {
- os.write(data);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- private void startRecording()
- {
- recorder = new AudioRecord(AudioSource.DEFAULT, 8000, channelConfig, audioFormat, BufferSize);
- if(1 == recorder.getState())
- recorder.startRecording();
- isRecording = true;
- recordingThread = new Thread(new Runnable() {
- @Override
- public void run() {
- writeAudioDataToFile();
- }
- },"AudioRecorder Thread");
- recordingThread.start();
- }
- private void stopRecording(){
- if(null != recorder){
- isRecording = false;
- int i = recorder.getState();
- if(i==1)
- recorder.stop();
- recorder.release();
- Log.d(TAG, "===== Recording Audio Completed ===== ");
- recorder = null;
- recordingThread = null;
- }
- }
- public void StartPlaying()
- {
- int minBufferSize = AudioTrack.getMinBufferSize(8000,
- AudioFormat.CHANNEL_OUT_MONO,
- AudioFormat.ENCODING_PCM_16BIT); /* Return 640 */
- int bufferSize = minBufferSize;
- AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO,
- AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
- int i = 0;
- byte[] temp = new byte[bufferSize];
- try {
- FileInputStream fin = new FileInputStream("/sdcard/audiofile.raw");
- Log.d(TAG, "===== opening file : /sdcard/audiofile.raw ===== ");
- DataInputStream dis = new DataInputStream(fin);
- at.play();
- while((i = dis.read(temp, 0, bufferSize)) > -1)
- {
- at.write(temp, 0, i);
- }
- Log.d(TAG, "===== Playing Audio Completed ===== ");
- at.stop();
- at.release();
- dis.close();
- fin.close();
- } catch (FileNotFoundException e) {
- // TODO
- e.printStackTrace();
- } catch (IOException e) {
- // TODO
- e.printStackTrace();
- }
- }
- private View.OnClickListener btnClick = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.btnStartRec:{
- Log.d(TAG, "Start Recording");
- enableButton(R.id.btnStartRec,false);
- enableButton(R.id.btnStopRec,true);
- startRecording();
- enableButton(R.id.btnStopPlay,false);
- break;
- }
- case R.id.btnStopRec:{
- Log.d(TAG, "Stop Recording");
- enableButton(R.id.btnStartRec,true);
- enableButton(R.id.btnStopRec,false);
- stopRecording();
- enableButton(R.id.btnStartPlay,true);
- break;
- }
- case R.id.btnStartPlay:{
- Log.d(TAG, "Play Recording");
- enableButton(R.id.btnStartRec,false);
- enableButton(R.id.btnStopRec,false);
- StartPlaying();
- enableButton(R.id.btnStartPlay,false);
- enableButton(R.id.btnStopPlay,true);
- break;
- }
- case R.id.btnStopPlay:{
- Log.d(TAG, "Stop Playing");
- //StopPlaying();
- enableButton(R.id.btnStartPlay,true);
- enableButton(R.id.btnStopPlay,false);
- enableButton(R.id.btnStartRec,true);
- break;
- }
- }
- }
- };
- /* Assign OnClickListener to Buttons */
- private void setButtonHandlers() {
- ((Button)findViewById(R.id.btnStartRec)).setOnClickListener(btnClick);
- ((Button)findViewById(R.id.btnStopRec)).setOnClickListener(btnClick);
- ((Button)findViewById(R.id.btnStartPlay)).setOnClickListener(btnClick);
- ((Button)findViewById(R.id.btnStopPlay)).setOnClickListener(btnClick);
- }
- /* Function to Enable/Disable Buttons */
- private void enableButton(int id,boolean isEnable){
- ((Button)findViewById(id)).setEnabled(isEnable);
- }
- /*private String getTempFilename(){
- String filepath = Environment.getExternalStorageDirectory().getPath();
- File file = new File(filepath,AUDIO_RECORDER_FOLDER);
- if(!file.exists()){
- file.mkdirs();
- }
- File tempFile = new File(filepath,AUDIO_RECORDER_TEMP_FILE);
- //if(tempFile.exists())
- // tempFile.delete();
- raw_filename = file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE;
- Log.d(TAG, "================= Temp File Name : "+file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE+"=================");
- return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
- }
- */
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- /**
- * A placeholder fragment containing a simple view.
- */
- public static class PlaceholderFragment extends Fragment {
- public PlaceholderFragment() {
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_main, container,
- false);
- return rootView;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement