Created
July 18, 2016 09:49
-
-
Save chittaranjan-khuntia/2df7567e05d9dcb78b7bc25515853a16 to your computer and use it in GitHub Desktop.
How to capture audio in android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity { | |
Button play,stop,record; | |
private MediaRecorder audioRecorder; | |
private String outputFile = null; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
play=(Button)findViewById(R.id.button3); | |
stop=(Button)findViewById(R.id.button2); | |
record=(Button)findViewById(R.id.button); | |
stop.setEnabled(false); | |
play.setEnabled(false); | |
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/audiosample.3gp";; | |
audioRecorder=new MediaRecorder(); | |
audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); | |
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); | |
audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); | |
audioRecorder.setOutputFile(outputFile); | |
record.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
try { | |
audioRecorder.prepare(); | |
audioRecorder.start(); | |
} | |
catch (IllegalStateException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
record.setEnabled(false); | |
stop.setEnabled(true); | |
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
stop.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
audioRecorder.stop(); | |
audioRecorder.release(); | |
audioRecorder = null; | |
stop.setEnabled(false); | |
play.setEnabled(true); | |
Toast.makeText(getApplicationContext(), "Successfully audio recorded",Toast.LENTH_LONG).show(); | |
} | |
}); | |
play.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException { | |
MediaPlayer m = new MediaPlayer(); | |
try { | |
m.setDataSource(outputFile); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
try { | |
m.prepare(); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
m.start(); | |
Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment