我正在尝试将语音保存到wav输出文件。什么都不管用。它返回-1。我试图检查存储是否可由写入
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
它返回True。
我试着用MODE_WORLD_WRITEABLE预处理创建目录并连接到它。-不起作用
我试图在清单中给出可写的前提条件-不起作用
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
请帮帮我我该怎么办?非常感谢
p.s播放方法工作
TextToSpeech.synthesizeToFile
返回的int是什么?
你确定Environment.getExternalStorageDirectory
给出的路径吗?您可以尝试通过类似/storage/sdcard0/myTts.wav
的方法手动设置路径,并使用文件管理器应用程序搜索该文件。我真的很难在手机上找到我的合成文件,因为android将文件保存到外部sd卡的错误行为。
我在使用Tts时遇到的另一个问题是对最多4000个字符的字符串的限制。但只要你的.speak
方法有效,这就不应该是问题。
1)首先创建您的路径
boolean file = new File("give your path here").mkdirs();
2) 您的文件名
String destination_file_name = {"gave your path"}+"Your_file_name";
3) 让字符串Converting_Ting作为转换的字符串
String Converting_Sting;
4) 让我们获得您的文本到语音引擎的最大容量
TextToSpeech tts;
int max_capacity_tts=tts.getMaxSpeechInputLength();
5) 如果你的转换字符串长度大于最大容量,你必须减少的长度
if(Converting_Sting.length()>max_capacity_tts())
{
Converting_Sting.substring(0,max_capacity_tts);
}
4) 然后你可以把缩减的字符串放在tts引擎中
HashMap<String, String> params = new HashMap();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, parsedText);
tts.synthesizeToFile(Converting_Sting,params,destinationFileName+".wav");
不要忘记在清单上给予写入权限
好运
(-1)当您使用targetsdkversion29的synthesizeToFile方法时出错,但当targetsdkvversion小于或等于28时,效果良好,请检查最新更新的
private void makeRingtoneFilename(String texttoconvertIntoAudiFile) {
File saveFilePath = null;
try {
String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/media/audio/ringtones/";
File root = new File(rootPath);
if (!root.exists()) {
root.mkdirs();
}
saveFilePath = new File(rootPath + "niceSong.m4a");
if (saveFilePath.exists()) {
saveFilePath.delete();
}
saveFilePath.createNewFile();
FileOutputStream out = new FileOutputStream(saveFilePath);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
int i1;
if (Build.VERSION.SDK_INT >= 21) {
//texttoconvertIntoAudiFile is the text you want to convrt into mp3
i1 = textToSpeech.synthesizeToFile( texttoconvertIntoAudiFile, null,saveFilePath, null );
} else {
i1 = textToSpeech.synthesizeToFile( texttoconvertIntoAudiFile, null, String.valueOf( saveFilePath.getAbsoluteFile() ) );
}
Log.e( "i1", i1 + "" );
String outPath=saveFilePath.getAbsolutePath();
String mimeType;
if (outPath.endsWith(".m4a")) {
mimeType = "audio/mp4a-latm";
} else if (outPath.endsWith(".wav")) {
mimeType = "audio/wav";
} else {
// This should never happen.
mimeType = "audio/mpeg";
}
String artist = "hum h yum";
ContentValues values = new ContentValues();
values.put( MediaStore.MediaColumns.DATA, outPath);
values.put( MediaStore.MediaColumns.TITLE, "niceSong.m4a");
values.put( MediaStore.MediaColumns.MIME_TYPE, mimeType);
values.put( MediaStore.Audio.Media.ARTIST, artist);
values.put( MediaStore.Audio.Media.IS_RINGTONE,FILE_KIND_RINGTONE);
// Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath);
final Uri newUri = getContentResolver().insert(uri, values);
setResult(RESULT_OK, new Intent().setData(newUri));
}