package com.example.recorde01;

import java.io.File;
import java.io.IOException;
import java.util.Random;


import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity
{
	
	Button recordingButton;
    Button stopButton;
    Button playButton;
    Button matchButton;

    MediaRecorder mediaRecorder;
    String voiceStoragePath;

    static final String AB = "abcdefghijklmnopqrstuvwxyz";
    static Random rnd = new Random();

    MediaPlayer mediaPlayer;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		hasSDCard();

        voiceStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        File audioVoice = new File(voiceStoragePath + File.separator + "voices");
        if(!audioVoice.exists())
        {
            audioVoice.mkdir();
        }
        
        voiceStoragePath = voiceStoragePath + File.separator + "voices/" + generateVoiceFilename(6) + ".3gpp";
        System.out.println("Audio path : " + voiceStoragePath);
        
        recordingButton = (Button)findViewById(R.id.record);
        stopButton = (Button)findViewById(R.id.stop);
        playButton = (Button)findViewById(R.id.play);

        stopButton.setEnabled(false);
        playButton.setEnabled(false);
        matchButton.setEnabled(false);

        initializeMediaRecord();

		        recordingButton.setOnClickListener(new View.OnClickListener() 
		        {
		            @Override
		            public void onClick(View v) 
		            {
		                if(mediaRecorder == null)
		                {
		                    initializeMediaRecord();
		                }
		                startAudioRecording();
		            }
		        });

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

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

    private String generateVoiceFilename( int len )
    {
        StringBuilder sb = new StringBuilder( len );
        for( int i = 0; i < len; i++ )
            sb.append( AB.charAt( rnd.nextInt(AB.length()) ) );
        return sb.toString();
    }

    private void startAudioRecording()
    {
        try 
        {
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        recordingButton.setEnabled(false);
        stopButton.setEnabled(true);
    }

    private void stopAudioRecording()
    {
        if(mediaRecorder != null)
        {
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
        }
        stopButton.setEnabled(false);
        playButton.setEnabled(true);
    }

    private void playLastStoredAudioMusic()
    {
        mediaPlayer = new MediaPlayer();
        try
        {
            mediaPlayer.setDataSource(voiceStoragePath);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();
        recordingButton.setEnabled(true);
        playButton.setEnabled(false);
    }

    private void stopAudioPlay()
    {
        if(mediaPlayer != null)
        {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    private void hasSDCard()
    {
        Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
        if(isSDPresent)     
        {
            System.out.println("There is SDCard");
        }
        else
        {
            System.out.println("There is no SDCard");
        }
    }

    private void mediaPlayerPlaying()
    {
        if(!mediaPlayer.isPlaying())
        {
            stopAudioPlay();
        }
    }

    private void initializeMediaRecord()
    {
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setOutputFile(voiceStoragePath);
    }

}
	
	


	
	

	

