android - 如何将文本转语音输出存储为 WAV 文件?

标签 android text-to-speech android-mediaplayer

我正在使用以下代码将 Text to Speech 输出存储为我的应用程序中的 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 的回答 post :

重要的方法是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:

并且如果您试图将 wave 文件保存到内部存储器(而不是 /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 模拟器上对其进行了测试,它工作正常,但是您需要使用设备管理器指定模拟器的 sdCard 的大小,编辑 eumlator,并指定 sdcard 的大小:例如 512 Mb。然后你会在路径中找到 wav 文件:mnt/sdcard/sounds/tmpaudio.wav 要对其进行测试,只需打开 DDMS 透视图文件资源管理器,然后将文件导出到您的 PC。

关于android - 如何将文本转语音输出存储为 WAV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14683559/

相关文章:

android - 使用 Amazon Cognito 访问 AWS Iot 时出现禁止异常

Android - 为 ListView 填充适配器的异步任务

android - 如何在服务调用中传递 bool 参数

Android:如何使用 KeyEvents 调用 MediaController 快进和快退

android - 如果我输入 "interrupt",为什么我的 MediaPlayer 会停止工作?

android - 在 Android 中创建 Parcelable 类时遇到问题

azure - 如何测量在 Azure 认知服务语音合成 (TTS) 中使用的字符?

Android TTS 不会说话

Android(RecognitionListener) 实时语音转文本预览

Android FFmpegMediaPlayer - MediaPlayer 错误(0, 0)