Sunday , November 28 2021

Video Call Android Tutorial – Implementing Video Call using Sinch – Android Coding by DMTechnolab

 

package techheromanish.example.com.videochatapp;

 

import com.sinch.android.rtc.AudioController;

import com.sinch.android.rtc.PushPair;

import com.sinch.android.rtc.calling.Call;

import com.sinch.android.rtc.calling.CallEndCause;

import com.sinch.android.rtc.calling.CallState;

import com.sinch.android.rtc.video.VideoCallListener;

import com.sinch.android.rtc.video.VideoController;

 

import android.media.AudioManager;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;

 

import java.util.List;

import java.util.Locale;

import java.util.Timer;

import java.util.TimerTask;

 

public class CallScreenActivity extends BaseActivity {

 

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

    static final String CALL_START_TIME = “callStartTime”;

    static final String ADDED_LISTENER = “addedListener”;

 

    private AudioPlayer mAudioPlayer;

    private Timer mTimer;

    private UpdateCallDurationTask mDurationTask;

 

    private String mCallId;

    private long mCallStart = 0;

    private boolean mAddedListener = false;

    private boolean mVideoViewsAdded = false;

 

    private TextView mCallDuration;

    private TextView mCallState;

    private TextView mCallerName;

 

    private class UpdateCallDurationTask extends TimerTask {

 

        @Override

        public void run() {

            CallScreenActivity.this.runOnUiThread(new Runnable() {

                @Override

                public void run() {

                    updateCallDuration();

                }

            });

        }

    }

 

    @Override

    protected void onSaveInstanceState(Bundle savedInstanceState) {

        savedInstanceState.putLong(CALL_START_TIME, mCallStart);

        savedInstanceState.putBoolean(ADDED_LISTENER, mAddedListener);

    }

 

    @Override

    protected void onRestoreInstanceState(Bundle savedInstanceState) {

        mCallStart = savedInstanceState.getLong(CALL_START_TIME);

        mAddedListener = savedInstanceState.getBoolean(ADDED_LISTENER);

    }

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.callscreen);

 

        mAudioPlayer = new AudioPlayer(this);

        mCallDuration = (TextView) findViewById(R.id.callDuration);

        mCallerName = (TextView) findViewById(R.id.remoteUser);

        mCallState = (TextView) findViewById(R.id.callState);

        Button endCallButton = (Button) findViewById(R.id.hangupButton);

 

        endCallButton.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                endCall();

            }

        });

 

        mCallId = getIntent().getStringExtra(SinchService.CALL_ID);

        if (savedInstanceState == null) {

            mCallStart = System.currentTimeMillis();

        }

    }

 

    @Override

    public void onServiceConnected() {

        Call call = getSinchServiceInterface().getCall(mCallId);

        if (call != null) {

            if (!mAddedListener) {

                call.addCallListener(new SinchCallListener());

                mAddedListener = true;

            }

        } else {

            Log.e(TAG, “Started with invalid callId, aborting.”);

            finish();

        }

 

        updateUI();

    }

 

    //method to update video feeds in the UI

    private void updateUI() {

        if (getSinchServiceInterface() == null) {

            return; // early

        }

 

        Call call = getSinchServiceInterface().getCall(mCallId);

        if (call != null) {

            mCallerName.setText(call.getRemoteUserId());

            mCallState.setText(call.getState().toString());

            if (call.getState() == CallState.ESTABLISHED) {

                //when the call is established, addVideoViews configures the video to  be shown

                addVideoViews();

            }

        }

    }

 

    //stop the timer when call is ended

    @Override

    public void onStop() {

        super.onStop();

        mDurationTask.cancel();

        mTimer.cancel();

        removeVideoViews();

    }

 

    //start the timer for the call duration here

    @Override

    public void onStart() {

        super.onStart();

        mTimer = new Timer();

        mDurationTask = new UpdateCallDurationTask();

        mTimer.schedule(mDurationTask, 0, 500);

        updateUI();

    }

 

    @Override

    public void onBackPressed() {

        // User should exit activity by ending call, not by going back.

    }

 

    //method to end the call

    private void endCall() {

        mAudioPlayer.stopProgressTone();

        Call call = getSinchServiceInterface().getCall(mCallId);

        if (call != null) {

            call.hangup();

        }

        finish();

    }

 

    private String formatTimespan(long timespan) {

        long totalSeconds = timespan / 1000;

        long minutes = totalSeconds / 60;

        long seconds = totalSeconds % 60;

        return String.format(Locale.US, “%02d:%02d”, minutes, seconds);

    }

 

    //method to update live duration of the call

    private void updateCallDuration() {

        if (mCallStart > 0) {

            mCallDuration.setText(formatTimespan(System.currentTimeMillis() mCallStart));

        }

    }

 

    //method which sets up the video feeds from the server to the UI of the activity

    private void addVideoViews() {

        if (mVideoViewsAdded || getSinchServiceInterface() == null) {

            return; //early

        }

 

        final VideoController vc = getSinchServiceInterface().getVideoController();

        if (vc != null) {

            RelativeLayout localView = (RelativeLayout) findViewById(R.id.localVideo);

            localView.addView(vc.getLocalView());

            

            localView.setOnClickListener(new OnClickListener() {

                @Override

                public void onClick(View v) {

                    //this toggles the front camera to rear camera and vice versa

                    vc.toggleCaptureDevicePosition();

                }

            });

 

            LinearLayout view = (LinearLayout) findViewById(R.id.remoteVideo);

            view.addView(vc.getRemoteView());

            mVideoViewsAdded = true;

        }

    }

 

    //removes video feeds from the app once the call is terminated

    private void removeVideoViews() {

        if (getSinchServiceInterface() == null) {

            return; // early

        }

 

        VideoController vc = getSinchServiceInterface().getVideoController();

        if (vc != null) {

            LinearLayout view = (LinearLayout) findViewById(R.id.remoteVideo);

            view.removeView(vc.getRemoteView());

 

            RelativeLayout localView = (RelativeLayout) findViewById(R.id.localVideo);

            localView.removeView(vc.getLocalView());

            mVideoViewsAdded = false;

        }

    }

 

    private class SinchCallListener implements VideoCallListener {

 

        @Override

        public void onCallEnded(Call call) {

            CallEndCause cause = call.getDetails().getEndCause();

            Log.d(TAG, “Call ended. Reason: “ + cause.toString());

            mAudioPlayer.stopProgressTone();

            setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);

            String endMsg = “Call ended: “ + call.getDetails().toString();

            Toast.makeText(CallScreenActivity.this, endMsg, Toast.LENGTH_LONG).show();

 

            endCall();

        }

 

        @Override

        public void onCallEstablished(Call call) {

            Log.d(TAG, “Call established”);

            mAudioPlayer.stopProgressTone();

            mCallState.setText(call.getState().toString());

            setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);

            AudioController audioController = getSinchServiceInterface().getAudioController();

            audioController.enableSpeaker();

            mCallStart = System.currentTimeMillis();

            Log.d(TAG, “Call offered video: “ + call.getDetails().isVideoOffered());

        }

 

        @Override

        public void onCallProgressing(Call call) {

            Log.d(TAG, “Call progressing”);

            mAudioPlayer.playProgressTone();

        }

 

        @Override

        public void onShouldSendPushNotification(Call call, List pushPairs) {

            // Send a push through your push provider here, e.g. GCM

        }

 

        @Override

        public void onVideoTrackAdded(Call call) {

            Log.d(TAG, “Video track added”);

            addVideoViews();

        }

    }

}

 


Source link

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

Your email address will not be published. Required fields are marked *