androidstudio自带tts没声音也不报错

发布时间:2019-08-01 09:21:11

下面我写的代码,既不报错也没有语音播报

public class Main2Activity extends AppCompatActivity implements AdapterView.OnItemClickListener,TextToSpeech.OnInitListener {

    //notifiction

    private String speechTxt = "1234"; // 需要朗读的内容

    private TextToSpeech textToSpeech; // TTS对象

    NotificationManager manager;

    Notification notification;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        try {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main2);


            manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            textToSpeech = new TextToSpeech(this, (OnInitListener) this); // 参数Context,TextToSpeech.OnInitListener

            //通知及语音播报功能

           // sendNotification(speechTxt);

        } catch (Exception e) {

            Looper.prepare();

            PublicTool.showMessageDialog(Main2Activity.this, e.getMessage());

            Looper.loop();

        }

    }

    @Override

    public void onInit(int status) {

        if (status == SUCCESS) {

            int result = textToSpeech.setLanguage(Locale.CHINA);

            if (result == LANG_MISSING_DATA

                    || result == LANG_NOT_SUPPORTED) {

                Toast.makeText(this, "数据丢失或不支持", Toast.LENGTH_SHORT).show();

            }

        }

    }

    @Override

    protected void onStop() {

        super.onStop();

        textToSpeech.stop(); // 不管是否正在朗读TTS都被打断

        textToSpeech.shutdown(); // 关闭,释放资源

    }

    @Override

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        rollNo.setText(listView.getItemAtPosition(position).toString());

        RollNo = null;

        if (textToSpeech != null && !textToSpeech.isSpeaking()) {

            textToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规

            //设定语速 ,默认1.0正常语速

            textToSpeech.setSpeechRate(1.2f);

            textToSpeech.speak(speechTxt,

                    TextToSpeech.QUEUE_FLUSH, null);

        }


    }

推荐回答

还没有选出推荐答案,请稍候访问或查看其他回答!
以上问题属网友观点,不代表本站立场,仅供参考!