我正在开发webrtc android应用程序,我使用Relative布局将两个SurfaceViewRenderer元素放在其中,一个用于本地流视图,另一个用于远程流。
这里是我的布局xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="0dip"
android:layout_weight="0"
android:layout_width="match_parent"
android:id="@+id/video_layout"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
android:clickable="true"
android:focusable="false"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/glview_parent"
>
<org.webrtc.SurfaceViewRenderer
android:id="@+id/remoteview"
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_above="@+id/localview"
/>
<org.webrtc.SurfaceViewRenderer
android:id="@+id/localview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
<TextView
android:id="@+id/messageTXT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:paddingTop="50dp"
android:paddingRight="50dp"
android:padding="10dp"
android:textSize="20sp"
android:background="#66000000"
android:textColor="@color/list_background"
android:visibility="gone"
/>
<Chronometer
android:id="@+id/chronoLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:paddingTop="50dp"
android:paddingRight="50dp"
android:padding="10dp"
android:textSize="20sp"
android:background="#66000000"
android:textColor="@color/list_background"
android:visibility="gone"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/buttons_layout"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:padding="5dp"
android:background="#66000000"
>
<ImageButton
android:id="@+id/speaker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/speaker_on"
android:background="@android:color/transparent"
android:paddingRight="10dp"
/>
<ImageView
android:id="@+id/ok01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_ok"
android:background="@android:color/transparent"
android:visibility="gone"
android:paddingRight="10dp"
/>
<ImageView
android:id="@+id/no01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/ic_no"
android:visibility="gone"
android:paddingRight="10dp"
/>
<ImageButton
android:id="@+id/micro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/mico_on"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
在通话进度上我按照这样的方式初步化了本地和远程SurfaceViewRenderer。
public void initVideos() {
remotevideoview.setVisibility(View.GONE);
RelativeLayout.LayoutParams local,remote;
remote=new RelativeLayout.LayoutParams(0,0);
remotevideoview.setLayoutParams(remote);
local = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
localvideoView.setLayoutParams(local);
}
当建立呼叫并且onaddstream事件触发时,我更新这样的布局:
public void updateVideos(){
RelativeLayout.LayoutParams remote = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
int w=video_layout.getWidth();
int h=video_layout.getHeight();
RelativeLayout.LayoutParams local = new RelativeLayout.LayoutParams(w/3, h/3); // or wrap_content
local.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
local.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
local.setMargins(2, 2, 2, 2);
glview_parent.updateViewLayout(remotevideoview, remote);
glview_parent.updateViewLayout(localvideoView, local);
remotevideoview.setVisibility(View.VISIBLE);
localvideoView.bringToFront();
glview_parent.invalidate();
localvideoView.invalidate();
remotevideoview.invalidate();
}
直到现在一切都还好。但是当我点击后退并退出通话活动时。然后我想在用户进入通话活动时再次显示本地和远程视频。我调用updateVideos(),我渲染本地和远程流,但我看不到localvideoView。我只看到匹配所有父宽度和高度的remotevideoview。 localvideoview总是带回来。我尝试了所有可能的代码,但我看不到本地视图。我使用localvideoView.bringToFront();但没有成功。