我想将文本语音另存为音频文件,并保持5秒钟的静默。我能够将文本保存到mp3文件中。我尝试过,但是我什么都没得到。
将文本保存到mp3文件的代码。我需要添加静音5秒。
HashMap<String, String> myHashRender = new HashMap();
String text="Name ,How are you.";//After Name i need to stop 5 seconds silent
String destFileName = Environment.getExternalStorageDirectory()+"/myfolder/tts.mp3";
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);
tts.synthesizeToFile(text, myHashRender, destFileName);
答案 0 :(得分:0)
// TAKE PERMISSIONS RUNTIME TOO
private void requestPermission() {
ActivityCompat.requestPermissions(MainActivity.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(MainActivity.this, "Permission Granted",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.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;
}
//Prepare RECORDER
String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
private MediaRecorder myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
// START THE RECORDING
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
// START YOUR TEXT_TO_SPEECH HERE
} catch (IllegalStateException ise) {
// make something ...
} catch (IOException ioe) {
// make something
}
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
// NOW TO STOP OR TO SAVE USE BELOW CODE
// when TTS is done playing, the OnUtteranceCompletedListener() get called automatically, for this your activity needs to IMPLEMENTS OnUtteranceCompletedListener
// It's callback // After 5 sec of TTS's speaking, file have saved
public void onUtteranceCompleted(String utteranceId) {
Log.i(TAG, utteranceId); //utteranceId == "SOME MESSAGE"
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
Toast.makeText(getApplicationContext(), "Audio Recorder successfully", Toast.LENGTH_LONG).show();
}
}, 5000);
}