溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android 8.0 中如何實現(xiàn)視頻通話的畫中畫模式的示例

發(fā)布時間:2020-10-04 14:13:06 來源:腳本之家 閱讀:235 作者:Agora 欄目:移動開發(fā)

Android 8.0 當(dāng)中允許 Activiy 以畫中畫模式展現(xiàn)。這是一種多窗口模式的改進加強,在視頻類應(yīng)用中用處非常大,有了這種模式,就可以在視頻通話或者觀看直播的過程當(dāng)中打開另外的應(yīng)用而不用退出當(dāng)前視頻。更詳細(xì)的就不再累述了,大家去閱讀官方文檔 就行

這里以 Agora SDK 為例來給大家展示下該特性,實際上不用 Agora SDK 做任何修改。

準(zhǔn)備環(huán)境

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

如何實現(xiàn)畫中畫模式

默認(rèn)應(yīng)用是不支持畫中畫模式的,需要給視頻所在的 Activity 做些配置,如下在 AndroidManifest.xml 加上屬性 resizeableActivity/supportsPictureInPicture 并均設(shè)置為 true

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

為了進入畫中畫模式,Activty 必需要用 enterPictureInPictureMode(PictureInPictureParams params) 方法,非常的簡單,但是為了告訴系統(tǒng)進入畫中畫模式之后,Activity 界面在整個屏幕當(dāng)中的布局,我們需要設(shè)置一些參數(shù)。我們這里簡單設(shè)置下,具體在使用的時候需要根據(jù)屏幕的分辨率動態(tài)取設(shè)置,更多信息參考官方文檔。

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

當(dāng)然需要在程序當(dāng)中控制 Acticity 界面當(dāng)中的內(nèi)容,比如我們可以隱藏自己本地的預(yù)覽畫面,隱藏不需要的按鈕信息等等,這個實現(xiàn)也非常簡單。

@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);
}

另外值得一說的是,進入畫中畫模式,系統(tǒng)會觸發(fā)生命周期的方法 onPause/onResume 方法,我們需要根據(jù)需要適當(dāng)?shù)淖鲂┎僮?,比如是畫中畫模式的話,就不做任何操作,音視頻流繼續(xù),否則的話,就關(guān)閉視頻流,反正在后臺也看不見視頻。

另外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("當(dāng)前版本不支持...");
      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));
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI