3

I am creating an app which tells the user about the person who is calling.

So as a part of testing, I created an abstract class whih extends broadcast receiver in order to handle the calls.

I've also created a class which extends the abstract class and from here I started a service.

In the service, I created a TextToSpeech object and started speak().

This doesn't seem to work.I hear nothing except the ringtone. Whenever I get a call, I hear the ringtone but not the voice.

AIM : I want the voice to be heard the ring tone starts.

Please help me !

Thanks.

My broadcast receiver is as follows:

public abstract class PhoneCallReceiverbase extends BroadcastReceiver {


public static int lastState= TelephonyManager.CALL_STATE_IDLE;
public  static Date callstartTime;
public static boolean isIncoming;
public static String savedNumber;

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")){
        savedNumber=intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
    }
    else
    {
        String stateStr=intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
        String number=intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        int state=0;
        if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE))
            state=TelephonyManager.CALL_STATE_IDLE;
        else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
            state=TelephonyManager.CALL_STATE_OFFHOOK;
        else  if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING))
            state=TelephonyManager.CALL_STATE_RINGING;

        onCallStateChaned(context,state,number);



    }
}

private void onCallStateChaned(Context context, int state, String number) {
    if(lastState==state){
        return;
    }
    switch (state){
        case TelephonyManager.CALL_STATE_RINGING:
            isIncoming=true;
            callstartTime=new Date();
            savedNumber=number;
            onIncomingCallReceived(context,number,callstartTime);
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //offhook are pickup calls....nothing is done on them
            if(lastState!=TelephonyManager.CALL_STATE_RINGING)
            {
                isIncoming=false;
                callstartTime=new Date();
                onOutGoingCallStarted(context,number,callstartTime);

            }
            else
            {
                isIncoming=true;
                callstartTime=new Date();
                onIncomingCallAnswered(context,number,callstartTime);

            }
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            if(lastState==TelephonyManager.CALL_STATE_RINGING){
                onMissedCall(context,number,callstartTime);
            }
            else if(isIncoming)
                onIncomingCallEnded(context,number,callstartTime,new Date());
            else
                onOutGoingCallEnded(context,number,callstartTime,new Date());
            break;
    }
    lastState=state;

}

protected abstract void onIncomingCallReceived(Context context,String number,Date start);
protected abstract void onIncomingCallAnswered(Context context,String number,Date start);
protected abstract void onIncomingCallEnded(Context context,String number,Date start,Date end);
protected abstract void onOutGoingCallStarted(Context context,String number,Date start);
protected abstract void onOutGoingCallEnded(Context context,String number,Date start,Date end);
protected abstract void onMissedCall(Context context,String number,Date start);

}

The class which extends the PhoneCallReceiver class :

public class CalReceiver extends PhoneCallReceiverbase {

TextToSpeech toSpeech;


@Override
protected void onIncomingCallReceived(Context context, String number, Date start) {
    Toast.makeText(context,"CALL RECIEVED",Toast.LENGTH_LONG).show();
    Intent intent=new Intent(context,CallService.class);
    intent.putExtra("call","CALL RECEIVED");
    context.startService(intent);
}

@Override
protected void onIncomingCallAnswered(Context context, String number, Date start) {

}

@Override
protected void onIncomingCallEnded(Context context, String number, Date start, Date end) {
    Toast.makeText(context,"CALL ENDED",Toast.LENGTH_LONG).show();
}

@Override
protected void onOutGoingCallStarted(Context context, String number, Date start) {
    Toast.makeText(context,"CALL STARTED",Toast.LENGTH_LONG).show();
}

@Override
protected void onOutGoingCallEnded(Context context, String number, Date start, Date end) {
    Toast.makeText(context,"CALL ENDED",Toast.LENGTH_LONG).show();
}

@Override
protected void onMissedCall(Context context, String number, Date start) {
    Toast.makeText(context,"MISSED CALL",Toast.LENGTH_LONG).show();
}
}

My service is as follows :

public class CallService extends Service implements TextToSpeech.OnInitListener,TextToSpeech.OnUtteranceCompletedListener {

private TextToSpeech tts;
private String spoken;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    tts=new TextToSpeech(this,this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    spoken=(String)intent.getExtras().get("call");
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS)
{
    tts.setLanguage(Locale.ENGLISH);
    //   toSpeech.speak("CALL IS COMING",TextToSpeech.QUEUE_FLUSH,null);
    if (Build.VERSION.RELEASE.startsWith("5")) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tts.speak(spoken, TextToSpeech.QUEUE_FLUSH, null, null);
        }
    }
    else {
        tts.speak(spoken, TextToSpeech.QUEUE_FLUSH, null);
    }
}
}

@Override
public void onUtteranceCompleted(String utteranceId) {
stopSelf();
}

@Override
public void onDestroy() {

    if(tts!=null)
    {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}
}
sai raman
  • 45
  • 4
  • Are you 'ducking' the ringtone when the TTS starts? Does your TTS code work correctly when you test it without an incoming call? – brandall Jun 12 '16 at 17:06
  • You may suppose to use `AudioFocus` for making tts sound during a call. I'll try to make a sample based on your code. – Kae10 Jun 14 '16 at 13:25
  • For example, the ring tone during phone call will be sounded through `STREAM_RINGTONE` audio stream. So, if you want to make tts sound, you get `AudioFocus` about `STREAM_RINGTONE`. – Kae10 Jun 14 '16 at 13:29

0 Answers0