【问题标题】:How to store text to speech output as WAV file?如何将文本到语音输出存储为 WAV 文件?
【发布时间】:2013-01-18 23:09:19
【问题描述】:

我正在使用以下代码将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);
    }
  }

 }

【问题讨论】:

  • @ChandraSekhar File appTmpPath = new File(exStoragePath + "/sekhar/"); ... tempDestFile = appTmpPath.getAbsolutePath() + "/"+ tempFilename;你的文件名里不是有两个'//'吗?
  • @sinisha,我错误地添加了多次。现在我删除并尝试了。我从子类中的 onInIt 方法得到了祝酒词。现在我如何检查它是否已保存。我将我的设备连接到我的机器并检查了 sekhar 文件夹。但是没有wav文件。你能建议我吗?我打印了“i”的值为 0。
  • MySpeachdid 实例说出了什么文本??
  • @Houcine,它应该说出我在 EditText 中输入的内容
  • @ChandraSekhar :但我在上面的代码中看不到任何指令告诉您的实例说出来自EditText的文本

标签: android text-to-speech android-mediaplayer


【解决方案1】:

参考这个post :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 模拟器上对其进行了测试,它工作正常,但是您需要使用设备管理器指定模拟器的 sdCard 的大小,编辑 eumlator,并指定您的 sdcard 的大小:例如 512 Mb。然后你会在路径中找到 wav 文件:mnt/sdcard/sounds/tmpaudio.wav 要测试它,只需打开DDMS perspectiveFile Explorer,然后将文件导出到您的PC。

【讨论】:

  • @当我尝试使用上面的代码时,我在logcat中得到了以下错误,
  • 这是错误:02-04 15:42:17.211:E/AndroidRuntime(5735):未捕获的处理程序:线程主因未捕获的异常而退出 02-04 15:42:17.351:E/ AndroidRuntime(5735): java.lang.NullPointerException 02-04 15:42:17.351: E/AndroidRuntime(5735): at com.example.testmedai1.MainActivity$1.onClick(MainActivity.java:56) 02-04 15:42 :17.351: E/AndroidRuntime(5735): 在 android.view.View.performClick(View.java:2418)
  • 你有一个NullPointerException 在这一行:56,这一行的代码是什么?放上onClick()方法的完整代码
  • 这实际上是我的代码问题。之后我解决了它,它说文件已保存。但我在那个文件夹中看不到文件。
  • 输入sdcard/,您会在名称下找到它:test.wav
【解决方案2】:

你可以使用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();
    }
});

现在你已经完成了

【讨论】:

  • 感谢您的快速回复。我会检查并确认。
  • 我需要澄清一下。我可以通过在模拟器中运行我的应用来测试它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2019-12-21
  • 2012-04-20
  • 1970-01-01
相关资源
最近更新 更多