试试这个代码,它对您有很大帮助,它可以理解.....
public class MainActivity extends Activity {
MediaRecorder mRecorder;
MediaPlayer mediaPlayer;
Button start, stop, play,stop_play;
String FileName="";
File outfile ;
public static final int request_code = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(Button)findViewById(R.id.btn_start_main);
stop=(Button)findViewById(R.id.btn_stop_main);
play=(Button)findViewById(R.id.btn_play_main);
stop_play=(Button)findViewById(R.id.btn_playstop_main);
if (checkPermissionFromDevice()){
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileName= Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+
UUID.randomUUID()+"AudioFile.3gp";
SetupMediaRecorder();
try {
mRecorder.prepare();
mRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
start.setEnabled(false);
stop.setEnabled(true);
play.setEnabled(false);
stop_play.setEnabled(false);
Toast.makeText(getApplicationContext(),"recording....",Toast.LENGTH_SHORT).show();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecorder.stop();
play.setEnabled(true);
start.setEnabled(true);
stop.setEnabled(false);
stop_play.setEnabled(false);
Toast.makeText(getApplicationContext(),"recording stoped....",Toast.LENGTH_SHORT).show();
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer=new MediaPlayer();
try {
mediaPlayer.setDataSource(FileName);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
start.setEnabled(false);
stop.setEnabled(false);
play.setEnabled(false);
stop_play.setEnabled(true);
Toast.makeText(getApplicationContext(),"playing..",Toast.LENGTH_SHORT).show();
}
});
stop_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.stop();
SetupMediaRecorder();
start.setEnabled(true);
stop_play.setEnabled(false);
play.setEnabled(false);
}
});
}
else {
requestPermissionFromDevice();
}
}
private void SetupMediaRecorder() {
mRecorder=new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(FileName);
}
private void requestPermissionFromDevice() {
ActivityCompat.requestPermissions(this,new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO},
request_code);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case request_code:
{
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ){
Toast.makeText(getApplicationContext(),"permission granted...",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(),"permission denied...",Toast.LENGTH_SHORT).show();
}
}
break;
}
}
private boolean checkPermissionFromDevice() {
int storage_permission= ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
int recorder_permssion=ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO);
return storage_permission == PackageManager.PERMISSION_GRANTED && recorder_permssion == PackageManager.PERMISSION_GRANTED;
}
}