如何在我的Android应用程序中添加音频消息?

时间:2012-05-13 17:48:32

标签: android audio android-activity

我想在我的Android应用程序的开头给出音频消息,就像“欢迎我们的系统”,但我不能。

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

            String text="Welcome to our location based system";
            if (text!=null && text.length()>0)              
            {
                Toast.makeText(SimpleAudioTestActivity.this, "Saying: " + text, Toast.LENGTH_LONG).show();
                tts.speak(text, TextToSpeech.QUEUE_ADD, null);
            }                       

    Intent checkintent=new Intent();
    checkintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkintent, MY_DATA_CHECK_CODE);
}

单击按钮后执行此操作时可以:

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

        but1=(Button) findViewById(R.id.button1);
        but1.setonclickListener(new View.onclickListener() 
        {

            @Override
            public void onclick(View arg0) 
            {
                String text="Welcome to our location based system";
                if (text!=null && text.length()>0)              
                {
                    Toast.makeText(SimpleAudioTestActivity.this, "Saying: " + text, Toast.LENGTH_LONG).show();
                    tts.speak(text, TextToSpeech.QUEUE_ADD, null);
                }                       
            }
        });           

        Intent checkintent=new Intent();
        checkintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkintent, MY_DATA_CHECK_CODE);
    }

我该怎么做?有任何建议请。

2 个答案:

答案 0 :(得分:0)

您可以将音频文件(例如mp3文件)放入res/raw文件夹,然后创建MediaPlayer并播放该文件

答案 1 :(得分:0)

这是你要找的吗?此代码将使用texttospeech说出字符串'text'中的任何内容。把它放在你的onCreate上。

TextToSpeech speak;


speak = new TextToSpeech(TextTalk.this,
            new TextToSpeech.OnInitListener() {

                @Override
                public void onInit(int status) {
                    if (status != TextToSpeech.ERROR) {
                        speak.setLanguage(Locale.US);
                    }
                }
            });

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