将文本转换为语音,然后此语音制作移动铃声

时间:2015-03-19 17:51:36

标签: android

我想将语音存储到SD卡中。 我制作了一个应用程序,通过使用将文本转换为语音 TextToSpeech班级。当文本转换成语音时,我怎样才能将这个声音变成手机铃声? 请帮助伙计:)感谢提前:))

1 个答案:

答案 0 :(得分:1)

文本到语音转换的简单示例:

package com.androidhive.texttospeech;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidTextToSpeechActivity extends Activity implements
        TextToSpeech.OnInitListener {
    /** Called when the activity is first created. */

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tts = new TextToSpeech(this, this);

        btnSpeak = (Button) findViewById(R.id.btnSpeak);

        txtText = (EditText) findViewById(R.id.txtText);

        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    @Override
    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }

    private void speakOut() {

        String text = txtText.getText().toString();

        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}

您可以使用setLanguage()功能更改要发言的语言。支持许多语言,如加拿大,法国,中国,德国等,

使用tts.setPitch(0.6);

更改投球率

Full reference could be found here

文件渲染和播放 虽然speakOut()方法用于让Android立即说出文本,但有时您可能希望将合成结果记录在音频文件中。例如,如果您的申请经常发表文字,就会出现这种情况;您可以通过只渲染一次文件,然后在需要时播放该音频文件来避免合成CPU开销。就像speak()一样,您可以使用可选的话语标识符在合成完成时通知文件:

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);
tts.synthesizeToFile(wakeUpText, myHashRender, destFileName);

一旦您收到合成完成的通知,您就可以像使用android.media.MediaPlayer的任何其他音频资源一样播放输出文件。

Reference for storing the speech in an audio file is here.