我使用以下代码将文本到语音输出存储为我的应用程序中的 wav 文件。错误可能出在哪里?
public class MainActivity extends Activity {
Button store, play;
EditText input;
String speakTextTxt;
TextToSpeech mTts;
HashMap<String, String> myHashRender = new HashMap<String, String>();
String tempDestFile ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
store = (Button) findViewById(R.id.button1);
play = (Button) findViewById(R.id.button2);
input = (EditText) findViewById(R.id.editText1);
store.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
speakTextTxt = "Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world";
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, speakTextTxt);
String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File appTmpPath = new File(exStoragePath + "/sounds/");
appTmpPath.mkdirs();
String tempFilename = "hello.mp3";
tempDestFile = appTmpPath.getAbsolutePath() + "/" + tempFilename;
new MySpeech(speakTextTxt);
}
});
}
class MySpeech implements OnInitListener
{
String tts;
public MySpeech(String tts)
{
this.tts = tts;
mTts = new TextToSpeech(MainActivity.this, this);
}
@Override
public void onInit(int status)
{
Log.v("log", "initi");
int i = mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile);
if(i == TextToSpeech.SUCCESS)
{
Toast toast = Toast.makeText(MainActivity.this, "Saved "+i,
Toast.LENGTH_SHORT);
toast.show();
}
System.out.println("Result : " + i);
}
}
}
参考Ted Hopp在这篇帖子中的回答:
重要的方法是synthesizeToFile。它将把音频写入您指定的设备上的文件中。然后,您可以使用 MediaPlayer 播放该文件,也可以使用 adb 命令行工具使用以下命令将其从设备拉到您的开发系统上
编辑:
尝试此代码,然后检查路径
sdcard/
是否包含该文件:test.wav
HashMap<String, String> myHashRender = new HashMap();
String textToConvert = "this is a demo for saving a WAV file";
String destinationFileName = "/sdcard/test.wav";
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, textToConvert);
mTts.synthesizeToFile(textToConvert, myHashRender, destinationFileName);
编辑2:
如果您尝试将波形文件保存到内部存储器(而不是
/sdcard/
文件夹),那么实现此目的的唯一方法是创建一个世界可写
内存中的目录如下:
context.getDir("soundfiles", Context.MODE_WORLD_WRITEABLE);
然后写入该目录。
编辑3:
查看代码后,您在创建目录和文件时遇到了一些问题:代码应该是这样的:
speakTextTxt = "Hello world";
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, speakTextTxt);
String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
Log.d("MainActivity", "exStoragePath : "+exStoragePath);
File appTmpPath = new File(exStoragePath + "/sounds/");
boolean isDirectoryCreated = appTmpPath.mkdirs();
Log.d("MainActivity", "directory "+appTmpPath+" is created : "+isDirectoryCreated);
String tempFilename = "tmpaudio.wav";
tempDestFile = appTmpPath.getAbsolutePath() + File.separator + tempFilename;
Log.d("MainActivity", "tempDestFile : "+tempDestFile);
new MySpeech(speakTextTxt);
我已经在android模拟器上测试了它,它工作正常,但是您需要使用设备管理器指定模拟器的sd卡大小,编辑eumlator,并指定sd卡的大小:例如512 Mb。然后你会在路径中找到wav文件:
mnt/sdcard/sounds/tmpaudio.wav
要测试它,只需打开 DDMS perspective
、File Explorer
,然后将文件导出到您的电脑。
您可以使用
synthesizeToFile()
来自 Android
HashMap<String, String> myHashRender = new HashMap();
String wakeUpText = "Are you up yet?";
String destFileName = "/sdcard/myAppCache/wakeUp.wav";
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, wakeUpText);
mTts.synthesizeToFile(wakuUpText, myHashRender, destFileName);
一旦收到合成完成的通知,您就可以像播放任何其他音频资源一样播放输出文件
android.media.MediaPlayer.
为此你可以使用它
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this,R.raw.button);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mMediaPlayer.stop();
}
});
现在你完成了