前面兩篇介紹了註冊、撥打、接聽等 參考地址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();
}
好了 所有的基本功能都已經實現了 其他的例如編碼等需參考源碼的寫法
對此感興趣的可以加羣261074724