gpt4 book ai didi

java - 文本转语音保存到音频文件,静音(长时间停顿)

转载 作者:太空宇宙 更新时间:2023-11-04 09:39:27 25 4
gpt4 key购买 nike

我想将文本语音保存为音频文件,暂停 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);

最佳答案

   // 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);

}

关于java - 文本转语音保存到音频文件,静音(长时间停顿),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56145953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com