电话通话中的文字转语音

时间:2017-04-02 16:56:51

标签: android

我正在努力创建一个能够在我忙碌时接听电话的应用,为此,我想使用Android Text-To-Speech,我就达到了这个代码:

package com.winsoft.phone;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.ActivityCompat;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends Activity {

    private Button mCallbutton;
    private EditText mNumberEditText;

    private TextToSpeech ttobj;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ttobj = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
            }
        });

        ttobj.setLanguage(Locale.getDefault());

        mNumberEditText = (EditText) findViewById(R.id.number_edit_text);
        mCallbutton = (Button) findViewById(R.id.call_button);

        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);

        // add button listener
        mCallbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + mNumberEditText.getText().toString()));
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(callIntent);

            }

        });

    }

    private class PhoneCallListener extends PhoneStateListener {

        private boolean isPhoneCalling = false;
        private String TAG = PhoneCallListener.class.getSimpleName();

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (TelephonyManager.CALL_STATE_RINGING == state) {
                // phone ringing
                Log.d(TAG, "RINGING STATE");
            }

            if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                // active
                Log.d(TAG, "OFFHOOK STATE");
                isPhoneCalling = true;

                Log.d(TAG, "Speaking Hola");
                ttobj.speak("Hola", TextToSpeech.QUEUE_FLUSH, null);
            }

            if (TelephonyManager.CALL_STATE_IDLE == state) {
                Log.d(TAG, "IDLE");
            }
        }
    }
}

但问题是,当我收到电话时,tts服务无法正常工作:

Log.d(TAG, "Speaking Hola");
ttobj.speak("Hola", TextToSpeech.QUEUE_FLUSH, null);

有什么建议吗?

修改 我想通过电话联系,如果它不够清楚

0 个答案:

没有答案