Arslan Maqbool
Arslan Maqbool

Reputation: 529

Sinch Audio and Video Calling in Android

I am trying to use sinch audio and video calling in android but problem is that how to check that the incoming call is audio or video . I am using shared preference for that purpose.Video to video call works fine on both side and when incoming call is audio on other side video incoming call activity called instead of audio in other user phone. Any help would be Appreciated.

public class SinchService extends Service {

    //set the shared preferences
    private SharedPreferences mPickSharedPrefs;
    private SharedPreferences.Editor mPickSharedPrefsEditor;

    private static final String APP_KEY = "*******************";
    private static final String APP_SECRET = "****************";
    private static final String ENVIRONMENT = "sandbox.sinch.com";

    public static final String CALL_ID = "CALL_ID";

    static final String TAG = SinchService.class.getSimpleName();

    private SinchServiceInterface mSinchServiceInterface = new SinchServiceInterface();
    private SinchClient mSinchClient;
    private String mUserId;

    private StartFailedListener mListener;

    @Override
    public void onCreate() {


        super.onCreate();
        mPickSharedPrefs = getSharedPreferences("mPickSharedPrefs", Context.MODE_PRIVATE);
        mPickSharedPrefsEditor=mPickSharedPrefs.edit();


    }

    @Override
    public void onDestroy() {
        if (mSinchClient != null && mSinchClient.isStarted()) {
            mSinchClient.terminate();
        }
        super.onDestroy();
    }

    private void start(String userName) {
        if (mSinchClient == null) {
            mUserId = userName;
            mSinchClient = Sinch.getSinchClientBuilder().context(getApplicationContext()).userId(userName)
                    .applicationKey(APP_KEY)
                    .applicationSecret(APP_SECRET)
                    .environmentHost(ENVIRONMENT).build();

            mSinchClient.setSupportCalling(true);
            mSinchClient.startListeningOnActiveConnection();

            mSinchClient.addSinchClientListener(new MySinchClientListener());
            // Permission READ_PHONE_STATE is needed to respect native calls.
            mSinchClient.getCallClient().setRespectNativeCalls(false);
            mSinchClient.getCallClient().addCallClientListener(new SinchCallClientListener());
            mSinchClient.start();
        }
    }

    private void stop() {
        if (mSinchClient != null) {
            mSinchClient.terminate();
            mSinchClient = null;
        }
    }

    private boolean isStarted() {
        return (mSinchClient != null && mSinchClient.isStarted());
    }

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

    public class SinchServiceInterface extends Binder {

        public Call callUserVideo(String userId) {
          //  mPickSharedPrefsEditor=mPickSharedPrefs.edit();
            mPickSharedPrefsEditor.putString("call_status","1");
            mPickSharedPrefsEditor.commit();
            return mSinchClient.getCallClient().callUserVideo(userId);
        }

        public Call callPhoneNumber(String phoneNumber) {
            return mSinchClient.getCallClient().callPhoneNumber(phoneNumber);
        }

        public Call callUser(String userId) {
          //  mPickSharedPrefsEditor=mPickSharedPrefs.edit();
            mPickSharedPrefsEditor.putString("call_status","0");
            mPickSharedPrefsEditor.commit();
            return mSinchClient.getCallClient().callUser(userId);
        }

        public String getUserName() {
            return mUserId;
        }

        public boolean isStarted() {
            return SinchService.this.isStarted();
        }

        public void startClient(String userName) {
            start(userName);
        }

        public void stopClient() {
            stop();
        }

        public void setStartListener(StartFailedListener listener) {
            mListener = listener;
        }

        public Call getCall(String callId) {
            return mSinchClient.getCallClient().getCall(callId);
        }

        public VideoController getVideoController() {
            if (!isStarted()) {
                return null;
            }
            return mSinchClient.getVideoController();
        }

        public AudioController getAudioController() {
            if (!isStarted()) {
                return null;
            }
            return mSinchClient.getAudioController();
        }
    }

    public interface StartFailedListener {

        void onStartFailed(SinchError error);

        void onStarted();
    }

    private class MySinchClientListener implements SinchClientListener {

        @Override
        public void onClientFailed(SinchClient client, SinchError error) {
            if (mListener != null) {
                mListener.onStartFailed(error);
            }
            mSinchClient.terminate();
            mSinchClient = null;
        }

        @Override
        public void onClientStarted(SinchClient client) {
            Log.d(TAG, "SinchClient started");
            if (mListener != null) {
                mListener.onStarted();
            }
        }

        @Override
        public void onClientStopped(SinchClient client) {
            Log.d(TAG, "SinchClient stopped");
        }

        @Override
        public void onLogMessage(int level, String area, String message) {
            switch (level) {
                case Log.DEBUG:
                    Log.d(area, message);
                    break;
                case Log.ERROR:
                    Log.e(area, message);
                    break;
                case Log.INFO:
                    Log.i(area, message);
                    break;
                case Log.VERBOSE:
                    Log.v(area, message);
                    break;
                case Log.WARN:
                    Log.w(area, message);
                    break;
            }
        }

        @Override
        public void onRegistrationCredentialsRequired(SinchClient client,
                ClientRegistration clientRegistration) {
        }
    }

    private class SinchCallClientListener implements CallClientListener {

        @Override
        public void onIncomingCall(CallClient callClient, Call call) {
            Log.d(TAG, "Incoming call");
            Log.d(TAG,call.getRemoteUserId());

            mPickSharedPrefs=getSharedPreferences("mPickSharedPrefs", Context.MODE_PRIVATE);
            mPickSharedPrefsEditor=mPickSharedPrefs.edit();
            String Check_Call_Status;
            Check_Call_Status=mPickSharedPrefs.getString("call_status","");
            Log.d(TAG,Check_Call_Status);
            if(Check_Call_Status.equals("1")) {

                Log.d(TAG,"Video Call");
                Intent intent = new Intent(SinchService.this, IncomingCallScreenActivity.class);
                intent.putExtra(CALL_ID, call.getCallId());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                SinchService.this.startActivity(intent);
                mPickSharedPrefsEditor.clear();
            }
            else
                if(Check_Call_Status.equals("0"))
                {
                    Log.d(TAG,"Voice Call");

                    Intent intent = new Intent(SinchService.this, VoiceIncomingCallScreenActivity.class);
                    intent.putExtra(CALL_ID, call.getCallId());
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    SinchService.this.startActivity(intent);
                    mPickSharedPrefsEditor.clear();


                }
        }
    }

}

Upvotes: 1

Views: 2705

Answers (3)

Davinder Goel
Davinder Goel

Reputation: 853

you can check using

call.getDetails().isVideoOffered()

in onIncomingCall(){} function. if it returns true that mean it is a video call otherwise audio.

Upvotes: 0

Sanjay Sharma
Sanjay Sharma

Reputation: 86

I did audio and video calling simultaneously using this below code. In SinchService

private class SinchCallClientListener implements CallClientListener {

    @Override
    public void onIncomingCall(CallClient callClient, Call call) {

        if (call.getDetails().isVideoOffered()) {
            Log.d(TAG, "Incoming video call");
            Intent intent = new Intent(SinchService.this, VideoIncomingCallScreenActivity.class);
            intent.putExtra(CALL_ID, call.getCallId());
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            SinchService.this.startActivity(intent);
        } else {
            Log.d(TAG, "Incoming audio call");
            Intent intent = new Intent(SinchService.this, IncomingCallScreenActivity.class);
            intent.putExtra(CALL_ID, call.getCallId());
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            SinchService.this.startActivity(intent);
        }
    }
}

Upvotes: 2

cjensen
cjensen

Reputation: 2703

You cant do it like that, if its a video call you need to answer it video and if its a audio call you need to answer it like a Audio, the recipient cant decide. In the GA release we will have the possibility to not turn on the video but just audio when you are in a video call

Upvotes: 0

Related Questions