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

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

想测试apk请加群261074724 

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

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

一.拨打视频通话

 

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

package com.lilin.mylinephone;
 
    。。。

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:
            
            break;
        case R.id.id_video_speaker:
             
            break;
        case R.id.id_video_qiev: 
            switchCamera(instance);
            break;
        default:
            break;
        }
    }
      
}
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 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值