Skip to content

Player issue while phone call is initiating and solution. #36

@nillbiplob

Description

@nillbiplob

It was a major issue and I fixed as follows

File: PlaybackService.java

TelephonyManager telManager;
boolean wasPlayingBeforePhoneCall=false;

public class LocalBinder extends Binder {
    public PlaybackService getService() {
        return PlaybackService.this;
    }
}

@Override
public void onCreate() {
    super.onCreate();
    mPlayer = com.aapbd.mediaplayeraudio.player.Player.getInstance();
    mPlayer.registerCallback(this);

    telManager = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);
    if (telManager != null) {
        telManager.listen(phoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);
    }
}


PhoneStateListener phoneListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            if(mPlayer==null)
                return;
            wasPlayingBeforePhoneCall = isPlaying();
            pause();
        } else if (state == TelephonyManager.CALL_STATE_IDLE) {
            if(mPlayer==null)
                return;

            if (wasPlayingBeforePhoneCall) {
               play();
            }
        } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            // A call is dialing,
            // active or on hold
            if(mPlayer==null)
                return;
            wasPlayingBeforePhoneCall = isPlaying();
            pause();
        }
        super.onCallStateChanged(state, incomingNumber);
    }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions