Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

android - How to store text to speech output as WAV file?

I am using the following code to store Text to Speech output as wav file in my application. I am not sure where is the error, can you please take a look into that and suggest me?

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);
    }
  }

 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

referring to the answer of Ted Hopp in this post :

The important method is synthesizeToFile. It will write the audio to a file on the device that you specify. You can then play that file with a MediaPlayer or you can pull it off the device onto your development system with the adb command-line tool using the command

EDIT :

try this code , and then check the path sdcard/ if it contains the file : 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);

EDIT 2 :

and if you are trying to save the wave file to the internal memory ( not the /sdcard/folder) , then the only way to achieve this is to create a world writable directory in the internal memory like this:

context.getDir("soundfiles", Context.MODE_WORLD_WRITEABLE);

And then write to this dir.

EDIT 3 :

after taking a look into your code , you've just some problems with creating directories and files : the code should be like this :

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);

i've tested it on android emulator , and it works fine , but you need to specify the size of the sdCard of your emulator bye using device manager, edit eumlator, and specify the size of your sdcard : example 512 Mb. and then you will find the wav file in the path : mnt/sdcard/sounds/tmpaudio.wav to test it , just open the DDMS perspective, File Explorer, and then export the file to your PC .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...