此线程在15秒后自动停止媒体记录器。我想在15秒之前手动停止线程,当我点击停止按钮时该怎么做
recordstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
c=1;
recordstart.setVisibility(View.GONE);
recordstop.setVisibility(View.VISIBLE);
recordcomplete.setVisibility(View.GONE);
if(checkPermission()) {
AudioSavePathInDevice =
Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
CreateRandomAudioFileName(5) + "AudioRecording.3gp";
MediaRecorderReady();
try {
//mediaRecorder.setMaxDuration(15000);
mediaRecorder.prepare();
mediaRecorder.start();
if (view.getId() == R.id.record) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run()
{
mediaRecorder.stop();
recordstart.setVisibility(View.GONE);
recordstop.setVisibility(View.GONE);
recordcomplete.setVisibility(View.VISIBLE);
Toast.makeText(UploadActivity.this, "Recording Completed",
Toast.LENGTH_LONG).show();
}
});
}
}, 15000);
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(UploadActivity.this, "Recording started",
Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
});
recordstop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder.stop();
recordstart.setVisibility(View.GONE);
recordstop.setVisibility(View.GONE);
recordcomplete.setVisibility(View.VISIBLE);
Toast.makeText(UploadActivity.this, "Recording Completed",
Toast.LENGTH_LONG).show();
}
});
这是整个代码
package com.dell.mubox;
ImageView recordstart, play, upload, recordstop, recordcomplete;
String AudioSavePathInDevice = null;
MediaRecorder mediaRecorder ;
Random random ;
String RandomAudioFileName = "ABCDEFGHIJKLMNOP";
public static final int RequestPermissionCode = 1;
MediaPlayer mediaPlayer ;
int c=0;
Timer t;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
recordstart = (ImageView)findViewById(R.id.record);
recordstop = (ImageView)findViewById(R.id.recordstop);
play = (ImageView)findViewById(R.id.play);
upload = (ImageView)findViewById(R.id.upload);
recordcomplete = (ImageView)findViewById(R.id.recordcomplete);
random = new Random();
recordstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
c=1;
recordstart.setVisibility(View.GONE);
recordstop.setVisibility(View.VISIBLE);
recordcomplete.setVisibility(View.GONE);
if(checkPermission()) {
AudioSavePathInDevice =
Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
CreateRandomAudioFileName(5) + "AudioRecording.3gp";
MediaRecorderReady();
try {
//mediaRecorder.setMaxDuration(15000);
mediaRecorder.prepare();
mediaRecorder.start();
if (view.getId() == R.id.record) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run()
{
mediaRecorder.stop();
recordstart.setVisibility(View.GONE);
recordstop.setVisibility(View.GONE);
recordcomplete.setVisibility(View.VISIBLE);
Toast.makeText(UploadActivity.this, "Recording Completed",
Toast.LENGTH_LONG).show();
}
});
}
}, 15000);
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(UploadActivity.this, "Recording started",
Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
});
recordstop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder.stop();
recordstart.setVisibility(View.GONE);
recordstop.setVisibility(View.GONE);
recordcomplete.setVisibility(View.VISIBLE);
Toast.makeText(UploadActivity.this, "Recording Completed",
Toast.LENGTH_LONG).show();
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) throws IllegalArgumentException,
SecurityException, IllegalStateException {
if (c == 1) {
recordstart.setVisibility(View.VISIBLE);
recordstop.setVisibility(View.GONE);
recordcomplete.setVisibility(View.GONE);
mediaPlayer = new MediaPlayer();
try {
c = 0;
mediaPlayer.setDataSource(AudioSavePathInDevice);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.start();
} catch (Exception e) {
Toast.makeText(UploadActivity.this, "No Audio Found", Toast.LENGTH_LONG).show();
}
Toast.makeText(UploadActivity.this, "Recording Playing",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(UploadActivity.this, "Record Audio First", Toast.LENGTH_LONG).show();
}
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(UploadActivity.this, "Audio Uploaded Successfully", Toast.LENGTH_LONG).show();
}
});
/* buttonStopPlayingRecording.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonStop.setEnabled(false);
buttonStart.setEnabled(true);
buttonStopPlayingRecording.setEnabled(false);
buttonPlayLastRecordAudio.setEnabled(true);
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
MediaRecorderReady();
}
}
});*/
}
public void MediaRecorderReady(){
mediaRecorder=new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice);
}
public String CreateRandomAudioFileName(int string){
StringBuilder stringBuilder = new StringBuilder( string );
int i = 0 ;
while(i < string ) {
stringBuilder.append(RandomAudioFileName.
charAt(random.nextInt(RandomAudioFileName.length())));
i++ ;
}
return stringBuilder.toString();
}
private void requestPermission() {
ActivityCompat.requestPermissions(UploadActivity.this, new
String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO}, RequestPermissionCode);
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if (grantResults.length> 0) {
boolean StoragePermission = grantResults[0] ==
PackageManager.PERMISSION_GRANTED;
boolean RecordPermission = grantResults[1] ==
PackageManager.PERMISSION_GRANTED;
if (StoragePermission && RecordPermission) {
Toast.makeText(UploadActivity.this, "Permission Granted",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(UploadActivity.this,"Permission Denied",Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(),
WRITE_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(),
RECORD_AUDIO);
return result == PackageManager.PERMISSION_GRANTED &&
result1 == PackageManager.PERMISSION_GRANTED;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}