How can i save Text to Speech file as .wav/.mp3 fo

2019-04-27 00:33发布

问题:

I want to save audio file in Arabic language. for that I am using the code as below.
i am trying it, but I am not able to save in Arabic language. its saves only in English language. please help me, Thanks

package com.t;

import java.io.File;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle; 
import android.os.Environment;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class TextToSpeechNewActivity 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.main);

    store = (Button) findViewById(R.id.button1);
    play = (Button) findViewById(R.id.button2);
    input = (EditText) findViewById(R.id.editText1);
    store.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            speakTextTxt = input.getText().toString();
            Log.v("log", ""+input.getText().toString());

            myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,speakTextTxt);

            String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
            File appTmpPath = new File(exStoragePath + "/pradip");
            appTmpPath.mkdirs();
            String tempFilename = input.getText().toString()+".wav";
            tempDestFile = appTmpPath.getAbsolutePath() + "/"+ tempFilename;
            new MySpeech(speakTextTxt);

        }
    });
}

class MySpeech implements OnInitListener{

            String tts;

    public MySpeech(String tts)
    {
        this.tts = tts;
        mTts = new TextToSpeech(TextToSpeechNewActivity.this, this);
    }

    @Override
    public void onInit(int status) 
    {
        Log.v("log", "initi");
        mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile);
    }
  }
 }

回答1:

From earlier comments:

This may sound a little obvious, but have you tried explicitly setting the language to an Arabic Locale using setLanguage(...)? You can potentially also first check if the Locale is available by checking the return value of isLanguageAvailable(...)

And:

Get them from Wikipedia: ISO 3166-1 and ISO 639-1. Do note that the Locale docs say: "The language codes are two-letter lowercase ISO language codes (such as "en") as defined by ISO 639-1. The country codes are two-letter uppercase ISO country codes (such as "US") as defined by ISO 3166-1."