文字转语音:语音在发布时自动发布

时间:2017-10-15 10:33:09

标签: java android

我为学校项目创建了一个应用程序。我使用EditText,Buttons和TextView以及texttospeech创建了一个应用程序。但是当我第一次启动应用程序时,它会自动发布演讲并说“你好”#34;但是我没有按下输入并说出#34; Hello"(在btnEnter()中),而且我的If语句也没有用(在btnEnter()和speakOut()中)。我想要做的就是演讲不会在发布时自动发布,直到我按回车键并说出#34; Hello + Name",当我按下回车但是我没有输入我的名字时,它会说"您必须输入您的姓名"。

这是我的Java代码:

package com.ramirez_finalprogram;

import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v4.view.GestureDetectorCompat;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnInitListener {

    private GestureDetectorCompat gestureObject;
    private TextToSpeech tts;
    MediaPlayer mp1, mp2, mp3, mp4, mp5;
    EditText et_name;
    Button btnEnter;
    TextView tvResult;
    Button btnA;
    Button btnB;
    Button btnC;
    Button btnD;
    Button btnE;
    Button btnExit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tts = new TextToSpeech(this, this);
        final RelativeLayout rl = (RelativeLayout)findViewById(R.id.container);
        gestureObject = new GestureDetectorCompat(this, new LearnGesture());
        mp1 = MediaPlayer.create(getApplicationContext(), R.raw.audi);
        mp2 = MediaPlayer.create(getApplicationContext(), R.raw.ford);
        mp3 = MediaPlayer.create(getApplicationContext(), R.raw.toyota);
        mp4 = MediaPlayer.create(getApplicationContext(), R.raw.bmw);
        mp5 = MediaPlayer.create(getApplicationContext(), R.raw.chev);
        et_name = (EditText)findViewById(R.id.editText_name);
        btnEnter = (Button)findViewById(R.id.button_enter);
        tvResult = (TextView)findViewById(R.id.textView3);
        btnA = (Button)findViewById(R.id.button_a);
        btnB = (Button)findViewById(R.id.button_b);
        btnC = (Button)findViewById(R.id.button_c);
        btnD = (Button)findViewById(R.id.button_d);
        btnE = (Button)findViewById(R.id.button_e);
        btnExit = (Button)findViewById(R.id.button_exit);

        btnEnter.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                rl.setBackgroundResource(R.drawable.grass);
                String name = et_name.getText().toString();

                if(name == ""){
                    speakOut();
                    Toast.makeText(MainActivity.this, "Must Enter your Name First", Toast.LENGTH_LONG).show();
                }else{
                    tvResult.setText("Hello, "+name);
                    speakOut();
                    et_name.setText("");
                    Toast.makeText(MainActivity.this, "Name Submitted", Toast.LENGTH_LONG).show();
                }
            }
        });
        btnA.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                rl.setBackgroundResource(R.drawable.audi);
                mp1.start();
            }
        });
        btnB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                rl.setBackgroundResource(R.drawable.ford);
                mp2.start();
            }
        });
        btnC.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                rl.setBackgroundResource(R.drawable.toyota);
                mp3.start();
            }
        });
        btnD.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                rl.setBackgroundResource(R.drawable.bmw);
                mp4.start();
            }
        });
        btnE.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                rl.setBackgroundResource(R.drawable.chev);
                mp5.start();
            }
        });
        btnExit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setCancelable(true);
                builder.setTitle("Confirm Exit");
                builder.setMessage("Do you really want to Exit?");

                builder.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                System.exit(0);
                                Toast.makeText(getApplicationContext(), 
                                        "Closing", Toast.LENGTH_LONG).show();
                            }
                        });
                builder.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // User pressed Cancel button. Write Logic Here
                                Toast.makeText(getApplicationContext(),
                                        "You clicked on Cancel",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                builder.show();
            }
        });
    }
    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub
        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);
            // tts.setPitch(5); // set pitch level
            tts.setSpeechRate(1); // set speech speed rate
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "Language is not supported");
            } else {
                btnEnter.setEnabled(true);
                speakOut();
            }
        } else {
            Log.e("TTS", "Initilization Failed");
        }
    }
    private void speakOut() {

        String text = et_name.getText().toString();
        if(text != ""){
            tts.speak("Hello"+text, TextToSpeech.QUEUE_FLUSH, null);
        }else if(text == ""){
            tts.speak("You must Enter your name first", TextToSpeech.QUEUE_FLUSH, null);
        }
    }

    public void showMessage(String title, String message){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event){
        this.gestureObject.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
    class LearnGesture extends GestureDetector.SimpleOnGestureListener {

        public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY){
            //if(event2.getX() > event1.getX()){

            //}
            //else
            //if(event2.getX() < event1.getX()){

            //}
            if(event2.getY() > event1.getY()){
                getActionBar().hide();
                Toast.makeText(MainActivity.this, "ActionBar Hide", Toast.LENGTH_SHORT).show();
            }else
            if(event2.getY() < event1.getY()){
                getActionBar().show();
                Toast.makeText(MainActivity.this, "ActionBar Show", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    }
}

0 个答案:

没有答案