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

所属分类: 软件编程 / Android 阅读数: 30
收藏 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异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多