基于linphone android sdk 的voip语音、视频通话 教程三、视频通话

如果觉得下面的麻烦可以直接到https://item.taobao.com/item.htm?id=587133228080获取源码。源码功能性更好、更完善。

想测试apk请加群261074724 

最新的sdk4教程地址  https://blog.csdn.net/Java_lilin/article/details/84842314

前面两篇介绍了注册、拨打、接听等 参考地址https://me.csdn.net/java_lilin

一.拨打视频通话

1.添加activity_video.xml 布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <org.linphone.mediastream.video.display.GL2JNIView
        android:id="@+id/id_video_rendering"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <SurfaceView
        android:id="@+id/id_video_preview"
        android:layout_width="120dp"
        android:layout_height="200dp"/>
 
     <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_gravity="bottom|center"
        android:orientation="horizontal" > 
        
    <Button
        android:id="@+id/id_video_gua"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
         android:layout_weight="1"
        android:text="挂断"/>

    <Button
        android:id="@+id/id_video_mute"
        android:layout_width="wrap_content"
         android:layout_weight="1"
        android:layout_height="wrap_content" 
        android:text="切换静音"/>

    <Button
        android:id="@+id/id_video_speaker"
        android:layout_width="wrap_content"
         android:layout_weight="1"
        android:layout_height="wrap_content" 
        android:text="切换免提"/>
    
    <Button
        android:id="@+id/id_video_qiev"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="切换摄像"/>
</LinearLayout>

</FrameLayout>

2.创建VideoActivity.java 并且在AndroidManifest.xml配置

package com.lilin.mylinephone;
 
   
import org.linphone.core.LinphoneCore;
import org.linphone.mediastream.Log;
import org.linphone.mediastream.video.AndroidVideoWindowImpl;
import org.linphone.mediastream.video.capture.hwconf.AndroidCameraConfiguration;
 


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class VideoActivity extends Activity implements OnClickListener{
      
    private VideoActivityReceiver mReceiver;
    public static final String RECEIVE_VIDEO_ACTIVITY = "receive_video_activity";
    
    private SurfaceView mRenderingView,mPreviewView;  
    private AndroidVideoWindowImpl mAndroidVideoWindow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video); 
        init(); 
        
        //广播
        IntentFilter intentFilter = new IntentFilter(RECEIVE_VIDEO_ACTIVITY);
        mReceiver = new VideoActivityReceiver();
        registerReceiver(mReceiver, intentFilter);
        
        fixZOrder(mRenderingView, mPreviewView);
        
        mAndroidVideoWindow = new AndroidVideoWindowImpl(mRenderingView, mPreviewView, new AndroidVideoWindowImpl.VideoWindowListener() {
            public void onVideoRenderingSurfaceReady(AndroidVideoWindowImpl vw, SurfaceView surface) {
                mRenderingView = surface;
                LinphoneMiniManager.getLC().setVideoWindow(vw);
            }

            public void onVideoRenderingSurfaceDestroyed(AndroidVideoWindowImpl vw) {
                 LinphoneCore linphoneCore = LinphoneMiniManager.getLC();
                 if (linphoneCore != null) {
                     linphoneCore.setVideoWindow(null);
                 }
            }

            public void onVideoPreviewSurfaceReady(AndroidVideoWindowImpl vw, SurfaceView surface) {
                mPreviewView = surface;
                LinphoneMiniManager.getLC().setPreviewWindow(mPreviewView); 
            }

            public void onVideoPreviewSurfaceDestroyed(AndroidVideoWindowImpl vw) {
                LinphoneMiniManager.getLC().setPreviewWindow(null);
            }
        });
         
    }
    
    private void init() {
        ((Button) findViewById(R.id.id_video_gua)).setOnClickListener(this);
        ((Button) findViewById(R.id.id_video_mute)).setOnClickListener(this);
        ((Button) findViewById(R.id.id_video_speaker)).setOnClickListener(this);
        ((Button) findViewById(R.id.id_video_qiev)).setOnClickListener(this);
        mRenderingView = (SurfaceView) findViewById(R.id.id_video_rendering);
        mPreviewView = (SurfaceView) findViewById(R.id.id_video_preview); 
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        if (mRenderingView != null) {
            ((GLSurfaceView) mRenderingView).onResume();
        }

        if (mAndroidVideoWindow != null) {
            synchronized (mAndroidVideoWindow) {
                LinphoneMiniManager.getLC().setVideoWindow(mAndroidVideoWindow);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mAndroidVideoWindow != null) {
            synchronized (mAndroidVideoWindow) {
                 LinphoneMiniManager.getLC().setVideoWindow(null);
            }
        }

        if (mRenderingView != null) {
            ((GLSurfaceView) mRenderingView).onPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
        }
        mPreviewView = null;
        mRenderingView = null;

        if (mAndroidVideoWindow != null) {
            mAndroidVideoWindow.release();
            mAndroidVideoWindow = null;
        }
    }
    
    public class VideoActivityReceiver extends BroadcastReceiver { 
        @Override
        public void onReceive(Context context, Intent intent) {
             String action = intent.getStringExtra("action");
             switch (action) {
             case "end": 
                VideoActivity.this.finish();
                break;

            default:
                break;
            } 
        }
    }

    @Override
    public void onClick(View view) {
        LinphoneMiniManager instance = LinphoneMiniManager.getInstance();
        switch (view.getId()) { 
        case R.id.id_video_gua: 
            instance.hangUp();
            finish();
            break;
        case R.id.id_video_mute:
            //QuhwaLinphone.toggleMicro(!QuhwaLinphone.getLC().isMicMuted());
            break;
        case R.id.id_video_speaker:
            //QuhwaLinphone.toggleSpeaker(!QuhwaLinphone.getLC().isSpeakerEnabled());
            break;
        case R.id.id_video_qiev: 
            switchCamera(instance);
            break;
        default:
            break;
        }
    }
    
    public void switchCamera(LinphoneMiniManager l) {
        try { 
            int videoDeviceId = l.getLC().getVideoDevice();
            videoDeviceId = (videoDeviceId + 1) % AndroidCameraConfiguration.retrieveCameras().length; 
            Toast.makeText(getApplicationContext(), "默认Toast样"+mPreviewView+"式"+videoDeviceId,   Toast.LENGTH_SHORT).show();
            //前置摄像头-1,后置摄像头-0 
            l.getLC().setVideoDevice(videoDeviceId);  
            l.updateCall();
            // previous call will cause graph reconstruction -> regive preview
            // window 
            if (mPreviewView != null) {
                l.getLC().setPreviewWindow(mPreviewView);
            }
            
        } catch (ArithmeticException ae) { Log.e("Cannot swtich camera : no camera"); }
    }
    
    private   void fixZOrder(SurfaceView rendering, SurfaceView preview) {
        rendering.setZOrderOnTop(false);
        preview.setZOrderOnTop(true);
        preview.setZOrderMediaOverlay(true); // Needed to be able to display control layout over
    }
}
3. 在原来的MainActivity.java的xml添加一个按钮用于拨打

  <Button 
            android:layout_width="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_height="wrap_content"
            android:id="@+id/id_btn_vda"
            android:text="视频拨打"
        />

MainActivity.java添加启动的

    case R.id.id_btn_vda: 
            try {
                instance.lilin_call(id_etext_dail.getText().toString(),host,true);
                startActivity(new Intent(MainActivity.this, VideoActivity.class));
            } catch (LinphoneCoreException e1) {Log.e("MainActivity", e1.getMessage());}  
            break;

好了  现在就可以拨打视频了 挂断的需要发送广播接受

在LinphoneMiniManager.java添加

    @Override
    public void displayStatus(LinphoneCore lc, String message) {
        Log.e("lilin  displayStatus: "+message ); 
        if(message.indexOf("Call terminated")!=-1){
            Intent intent = new Intent(VideoActivity.RECEIVE_VIDEO_ACTIVITY);
            intent.putExtra("action", "end"); 
            sendBroadcast( intent);  
        }
    }

好了  上面的就可以进行拨打视频通话了

二、接受视频通话

可以在接电话按钮中添加

case R.id.id_btn_jie:
            try { 
                instance.lilin_jie();
                if(instance.lilin_getVideoEnabled()) {//启动视频 
                    startActivity(new Intent(MainActivity.this, VideoActivity.class));
                }
            } catch (LinphoneCoreException e) {Log.e("MainActivity", e.getMessage()); } 

并在LinphoneMiniManager.java添加 

 public boolean lilin_getVideoEnabled() {
            LinphoneCallParams remoteParams = mLinphoneCore.getCurrentCall().getRemoteParams();
            return remoteParams != null && remoteParams.getVideoEnabled();
    }

public void hangUp() {
        LinphoneCall currentCall = mLinphoneCore.getCurrentCall();
        if (currentCall != null) {
            mLinphoneCore.terminateCall(currentCall);
        } else if (mLinphoneCore.isInConference()) {
            mLinphoneCore.terminateConference();
        } else {
            mLinphoneCore.terminateAllCalls();
        }
    } 

public void updateCall() { 
        LinphoneCall lCall = mLinphoneCore.getCurrentCall();
        if (lCall == null) {
            Log.e("Trying to updateCall while not in call: doing nothing");
            return;
        }  
        mLinphoneCore.updateCall(lCall,null);
    }

好了  所有的基本功能都已经实现了  其他的例如编码等需参考源码的写法

对此感兴趣的可以加群261074724 

猜你喜欢

转载自blog.csdn.net/Java_lilin/article/details/81871844