在android中调用时静音铃声音量

时间:2016-06-14 03:09:10

标签: android text-to-speech android-audiomanager android-broadcastreceiver google-text-to-speech

我的目标是让用户知道正在呼叫的人。我在服务中使用TextToSpeech来完成此任务。

所以我决定在通话期间静音铃声音量:

  mAudioManager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
            volume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
            } else {
                mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
            }

我知道这是要添加的代码但在哪里?是否在扩展广播接收器的类中?

我的问题是,无论何时发生呼叫,电话都会正常振铃。我想静音铃声并让我的TextToSpeech对象说话

我的清单文件如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sairaman63yahoo.callerannouncer">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver android:name=".CalReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
        </intent-filter>


    </receiver>
    <service android:name=".CallService" android:enabled="true"></service>

    <activity
        android:name=".DetectPhoneCallsActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

扩展广播接收器的My PhoneCallReceiver类是:

public abstract class PhoneCallReceiverbase extends BroadcastReceiver {

private int mRingVolume=0;
public static int lastState= TelephonyManager.CALL_STATE_IDLE;
public  static Date callstartTime;
public static boolean isIncoming;
public static String savedNumber;
private static  AudioManager mAudioManager;
private int volume;

@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;
    }

  //  mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);

    switch (state){
        case TelephonyManager.CALL_STATE_RINGING:
      //  AudioManager manager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
            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);
}

扩展PhoneCallReceiver类的我的CallReceiver类是:

public class CalReceiver extends PhoneCallReceiverbase {

TextToSpeech toSpeech;
private AudioManager mAudioManager;
private int volume;


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

    mAudioManager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    volume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
    } else {
        mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
    }
    Intent intent1 = new Intent(context.getApplicationContext(), CallService.class);
    intent1.putExtra("call", "CALL RECEIVED");
    context.startService(intent1);

   // Toast.makeText(context, "CALL RECIEVED", Toast.LENGTH_LONG).show();


    final ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    final List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

    for (ActivityManager.RunningServiceInfo runningServiceInfo : services) {
        if (runningServiceInfo.service.getClassName().equals(CallService.class)) {
            Log.v("SERVICE", "RUNNING");

        }
    }
}



@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();
}
}

最后,我的服务课说的是:

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

int volume=0;
private TextToSpeech tts;
private String spoken="call started sir!";
private AudioManager mAudioManager;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void 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)
{
    // mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
    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){
mAudioManager.setStreamMute(AudioManager.STREAM_RING, false);
mAudioManager.setStreamVolume(AudioManager.STREAM_RING,
volume, AudioManager.FLAG_ALLOW_RINGER_MODES);
    stopSelf();
}
}

总结查询:

目标:在通话过程中静音铃声音量并使TextToSpeech对象说话并在演讲结束后继续铃声音量

问题:1)在哪里添加使铃声音量静音的代码           2)我在onRecomingCallReceived()中添加了CallReceiver类中的代码,但它似乎不起作用。铃声不会停止

请帮忙!

我知道这是一个很长的问题,但我被困在这里。  谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,我找不到办法让我的申请做你想做的事。我只能使当前的铃声静音并且无法启动它,除了在TTS宣布呼叫并监视他们与待处理呼叫的进一步交互之后复制用户的铃声,这是复杂的。

要做到这一点:

public static void muteRinger(final Context ctx, final boolean mute) {

    final AudioManager am = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);

    switch (am.getRingerMode()) {

        case AudioManager.RINGER_MODE_NORMAL:
            Log.i("TAG", "getRingerMode: RINGER_MODE_NORMAL");
            break;
        case AudioManager.RINGER_MODE_SILENT:
            Log.i("TAG", "getRingerMode: RINGER_MODE_NORMAL");
            break;
        case AudioManager.RINGER_MODE_VIBRATE:
            Log.i("TAG", "getRingerMode: RINGER_MODE_NORMAL");
            break;
        default:
            Log.e("TAG", "getRingerMode: Default??");
            break;
    }


    if(mute){
        Log.i("TAG", "muting ringtone");

    // Save am.getRingerMode() to the shared preferences as the user's default            

        try {
            am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        } catch (Exception e) {
            e.printStackTrace();
        }

    } else {
        Log.i("TAG", "restoring ringtone");

        try {
            am.setRingerMode(// The default you previously saved);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

显然,如果你无法正确恢复用户的铃声设置,你会有一个恼火的用户,如果他们错过了一个重要的电话,你可能会遇到麻烦......