Android 如何将文本到语音输出存储为WAV文件?

Android 如何将文本到语音输出存储为WAV文件?,android,text-to-speech,android-mediaplayer,Android,Text To Speech,Android Mediaplayer,我使用以下代码将文本到语音输出存储为我的应用程序中的wav文件。我不知道哪里出错了,你能帮我检查一下并提出建议吗 public class MainActivity extends Activity { Button store, play; EditText input; String speakTextTxt; TextToSpeech mTts; HashMap<String, String> myHashRender = new HashMap<String, Str

我使用以下代码将
文本到语音
输出存储为我的应用程序中的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);
    }
  }

 }
公共类MainActivity扩展活动{
按钮店,玩;
编辑文本输入;
字符串speakTextTxt;
texttospeechmtts;
HashMap myHashRender=新建HashMap();
字符串文件;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
store=(按钮)findviewbyd(R.id.button1);
play=(按钮)findViewById(R.id.button2);
输入=(EditText)findViewById(R.id.editText1);
store.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
speakTextTxt=“你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界”;
HashMap myHashRender=新建HashMap();
myHashRender.put(TextToSpeech.Engine.KEY_参数_话语_ID,speakTextTxt);
字符串exStoragePath=Environment.getExternalStorageDirectory().getAbsolutePath();
File appTmpPath=新文件(exStoragePath+“/sounds/”;
appTmpPath.mkdirs();
字符串tempFilename=“hello.mp3”;
tempDestFile=appTmpPath.getAbsolutePath()+“/”+tempFilename;
新MySpeech(speakTextTxt);
}
});
}
类MySpeech实现OnInitListener
{
字符串tts;
公共MySpeech(字符串tts)
{
this.tts=tts;
mTts=新的TextToSpeech(MainActivity.this,this);
}
@凌驾
公共无效onInit(int状态)
{
Log.v(“Log”、“initi”);
int i=mTts.synthezetofile(speakTextTxt、myHashRender、tempDestFile);
if(i==TextToSpeech.SUCCESS)
{
Toast Toast=Toast.makeText(MainActivity.this,“Saved”+i,
烤面包片(长/短);
toast.show();
}
System.out.println(“结果:+i”);
}
}
}

您可以使用
synthesizeToFile()


现在,您在本节中参考了Ted Hopp的答案

重要的方法是。它会将音频写入指定设备上的文件。然后,您可以使用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);
然后写信给这个目录

编辑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);
speakTextTxt=“你好世界”;
HashMap myHashRender=新建HashMap();
myHashRender.put(TextToSpeech.Engine.KEY_参数_话语_ID,speakTextTxt);
字符串exStoragePath=Environment.getExternalStorageDirectory().getAbsolutePath();
Log.d(“MainActivity”、“exStoragePath:+exStoragePath”);
File appTmpPath=新文件(exStoragePath+“/sounds/”;
布尔值isDirectoryCreated=appTmpPath.mkdirs();
Log.d(“MainActivity”,“创建目录”+appTmpPath+”:“+isDirectoryCreated”);
字符串tempFilename=“tmpaudio.wav”;
tempDestFile=appTmpPath.getAbsolutePath()+File.separator+tempFilename;
Log.d(“MainActivity”、“tempDestFile:+tempDestFile”);
新MySpeech(speakTextTxt);
我已经在android emulator上对其进行了测试,它运行良好,但是您需要通过使用设备管理器、编辑eumlator指定模拟器SD卡的大小,并指定SD卡的大小:例如512 Mb。然后您将在路径中找到wav文件:
mnt/sdcard/sounds/tmpaudio.wav

要测试它,只需打开
DDMS透视图
文件浏览器
,然后将文件导出到您的电脑。

谢谢您的快速回复。我会检查并确认。我需要一些澄清。我可以在Emulator中运行我的应用程序来测试这一点吗?@ChandraSekhar File appTmpPath=new File(exStoragePath+“/sekhar/”)。。。tempDestFile=appTmpPath.getAbsolutePath()+“/”+tempFilename;您的文件名中不是有两个“//”吗?@sinisha,我错加了多次。现在我把它取下来试了试。我从子类中的onInIt方法中得到了toast。现在我如何检查它是否保存。我将我的设备连接到我的机器并检查了sekhar文件夹。但是没有wav文件。你能推荐我吗?我打印了“I”的值,它是0。你的
myspache
实例说了什么?@Houcine,它应该说我在EditText@ChandraSekhar:但我在上面的代码中看不到任何指示,当我尝试使用上述代码时,指示实例说出
EditText
@中的文本,我在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)您在第56行有一个
NullPointerException
,这一行的代码是什么?把
onClick()的完整代码放进去
method这实际上是我的代码问题。之后我解决了它,它说文件已保存。但我在该文件夹中看不到该文件。输入
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);
context.getDir("soundfiles", Context.MODE_WORLD_WRITEABLE);
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);