Android 8.0 中如何实现视频通话的画中画模式的示例

所属分类: 软件编程 / Android 阅读数: 49
收藏 0 赞 0 分享

Android 8.0 当中允许 Activiy 以画中画模式展现。这是一种多窗口模式的改进加强,在视频类应用中用处非常大,有了这种模式,就可以在视频通话或者观看直播的过程当中打开另外的应用而不用退出当前视频。更详细的就不再累述了,大家去阅读官方文档 就行

这里以 Agora SDK 为例来给大家展示下该特性,实际上不用 Agora SDK 做任何修改。

准备环境

  1. Android 8.0 或以上版本手机
  2. Agora SDK 1.14.0 或以上 版本
  3. Android Studio 3.0 或以上版本(非必需)

如何实现画中画模式

默认应用是不支持画中画模式的,需要给视频所在的 Activity 做些配置,如下在 AndroidManifest.xml 加上属性 resizeableActivity/supportsPictureInPicture 并均设置为 true

android:resizeableActivity="true"
android:supportsPictureInPicture="true"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"

为了进入画中画模式,Activty 必需要用 enterPictureInPictureMode(PictureInPictureParams params) 方法,非常的简单,但是为了告诉系统进入画中画模式之后,Activity 界面在整个屏幕当中的布局,我们需要设置一些参数。我们这里简单设置下,具体在使用的时候需要根据屏幕的分辨率动态取设置,更多信息参考官方文档。

PictureInPictureParams params = new PictureInPictureParams.Builder()
   .setAspectRatio(new Rational(10, 16))
   .build();

当然需要在程序当中控制 Acticity 界面当中的内容,比如我们可以隐藏自己本地的预览画面,隐藏不需要的按钮信息等等,这个实现也非常简单。

@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
  super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
  FrameLayout container = findViewById(R.id.local_video_view_container);
  SurfaceView surfaceView = (SurfaceView) container.getChildAt(0);
  surfaceView.setZOrderMediaOverlay(!isInPictureInPictureMode);
  surfaceView.setVisibility(isInPictureInPictureMode ? View.GONE : View.VISIBLE);
  container.setVisibility(isInPictureInPictureMode ? View.GONE : View.VISIBLE);
}

另外值得一说的是,进入画中画模式,系统会触发生命周期的方法 onPause/onResume 方法,我们需要根据需要适当的做些操作,比如是画中画模式的话,就不做任何操作,音视频流继续,否则的话,就关闭视频流,反正在后台也看不见视频。

另外Android 8.0 画中画demo

记录一下简单的demo ,方便以后用到:

package com.example.myapplication;

import android.annotation.TargetApi;
import android.app.PictureInPictureParams;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Rational;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 画中画
 */

public class TestPIPActivity extends AppCompatActivity {
  private static final String TAG = "TestPIPActivity";
  private PictureInPictureParams.Builder mPictureInPictureParamsBuilder;

  @TargetApi(Build.VERSION_CODES.O)
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout content = new FrameLayout(this);
    setContentView(content,new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    if(Build.VERSION.SDK_INT == Build.VERSION_CODES.O){
      mPictureInPictureParamsBuilder = new PictureInPictureParams.Builder();

      final TextView textView = new TextView(this);
      textView.setText("test PIP");
      textView.setTextSize(20);
      FrameLayout.LayoutParams fl = new FrameLayout.LayoutParams(
          ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      fl.gravity = Gravity.CENTER ;
      textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {//主要操作
          Rational aspectRatio = new Rational(10,10);
          mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
          enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
        }
      });
      content.addView(textView,fl);

    }else{
      TextView descTv = new TextView(this);
      descTv.setText("当前版本不支持...");
      descTv.setTextSize(20);
      FrameLayout.LayoutParams Tvfl = new FrameLayout.LayoutParams(
          ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      Tvfl.gravity = Gravity.CENTER ;
      content.addView(descTv,Tvfl);
    }

  }



  @Override
  public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
    super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
    Log.d(TAG,String.valueOf(isInPictureInPictureMode));
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多