国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 8.0 中如何實現視頻通話的畫中畫模式的示例

2019-10-22 18:23:11
字體:
來源:轉載
供稿:網友

android/222732.html">android/213420.html">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 界面當中的內容,比如我們可以隱藏自己本地的預覽畫面,隱藏不需要的按鈕信息等等,這個實現也非常簡單。

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。

 

注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 惠水县| 右玉县| 定安县| 湄潭县| 雷山县| 巴青县| 富顺县| 鞍山市| 元谋县| 卢湾区| 清流县| 武陟县| 临潭县| 会泽县| 银川市| 威信县| 建始县| 锦州市| 灵山县| 慈利县| 五大连池市| 江川县| 融水| 武鸣县| 上饶县| 陵水| 辛集市| 大悟县| 拜城县| 禄劝| 广西| 时尚| 横山县| 琼结县| 始兴县| 道真| 阜平县| 建平县| 新郑市| 舞钢市| 黎川县|