android的扩展屏幕是通过Presentation 类实现的。

Presentation继承自  Dialog。

  • 主屏显示的代码
  • 主屏播放一个video3的视频 。video3在项目的 assets文件里 可以随便拷贝一个放进去。
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRouter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import com.xinyintai.guoruiyibushow.R;
import java.io.IOException;/*** 用于测试   android 异步播放。* <p>* 主界面 lvds 信号播放的界面。*/public class MainActivity extends Activity {private MediaRouter mMediaRouter;private DemoPresentation mPresentation;int flag = 2;//   1 绿色同步 2 蓝色异步private SurfaceView surfaceView;private MediaPlayer mediaPlayer1;ImageButton bt;String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 得到media router service.mMediaRouter = (MediaRouter) getSystemService(Context.MEDIA_ROUTER_SERVICE);setContentView(R.layout.activity_main);//监听media rotues的改变mMediaRouter.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, mMediaRouterCallback);initMainPlay();//主屏幕updatePresentation();//扩展屏幕}//主屏幕播放 video3视频private void initMainPlay() {mediaPlayer1 = new MediaPlayer();surfaceView = this.findViewById(R.id.surface1);bt = findViewById(R.id.iv_state);surfaceView.getHolder().setKeepScreenOn(true);surfaceView.getHolder().addCallback(new SurfaceViewLis());mediaPlayer1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {@Overridepublic void onCompletion(MediaPlayer mp) {play();}});bt.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (flag == 1) {//绿色异步try {mPresentation.show();//异步让这个dialog展示} catch (WindowManager.InvalidDisplayException ex) {// Log.w(TAG, "Couldn't show presentation!  Display was removed in "+ "the meantime.", ex);mPresentation = null;}bt.setBackground(getResources().getDrawable(R.drawable.circle_blue));flag = 2;} else if (flag == 2) {//蓝色同步mPresentation.dismiss();//同步让presentation消失bt.setBackground(getResources().getDrawable(R.drawable.circle_green));flag = 1;}}});}public void play() {mediaPlayer1.reset();mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);try {mediaPlayer1.setDataSource(this, Uri.parse("android.resource://" + "com.xinyintai.guoruiyibushow" + "/" + R.raw.video3));// 把视频输出到SurfaceView上mediaPlayer1.setDisplay(surfaceView.getHolder());mediaPlayer1.prepare();mediaPlayer1.start();} catch (IOException e) {e.printStackTrace();}}private class SurfaceViewLis implements SurfaceHolder.Callback {@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}@Overridepublic void surfaceCreated(SurfaceHolder holder) {try {play();mediaPlayer1.seekTo(0);} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {}}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();}private final MediaRouter.SimpleCallback mMediaRouterCallback =new MediaRouter.SimpleCallback() {@Overridepublic void onRouteSelected(MediaRouter router, int type, MediaRouter.RouteInfo info) {Log.e(TAG, "onRouteSelected: type=" + type + ", info=" + info);updatePresentation();}@Overridepublic void onRouteUnselected(MediaRouter router, int type, MediaRouter.RouteInfo info) {Log.e(TAG, "onRouteeUnselected: type=" + type + ", info=" + info);updatePresentation();}@Overridepublic void onRoutePresentationDisplayChanged(MediaRouter router, MediaRouter.RouteInfo info) {Log.e(TAG, "onRoutePresentationDisplayChanged: info=" + info);updatePresentation();}};//准备异步的界面private void updatePresentation() {// Get the current route and its presentation display.MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);Display presentationDisplay = route != null ? route.getPresentationDisplay() : null;// 如果屏幕显示改变了 让当前的扩展屏消失if (mPresentation != null && mPresentation.getDisplay() != presentationDisplay) {//Log.i(TAG, "Dismissing presentation because the current route no longer "+ "has a presentation display.");mPresentation.dismiss();mPresentation = null;}//创建一个扩展屏if (mPresentation == null && presentationDisplay != null) {Log.e(TAG, "Showing presentation on display: " + presentationDisplay);mPresentation = new DemoPresentation(this, presentationDisplay);mPresentation.setOnDismissListener(mOnDismissListener);mPresentation.show();}updateContents();}//更新主界面中的内容private void updateContents() {}/*** 监听 副屏界面的消失 更新主界面。*/private final DialogInterface.OnDismissListener mOnDismissListener =new DialogInterface.OnDismissListener() {@Overridepublic void onDismiss(DialogInterface dialog) {if (dialog == mPresentation) {//Log.i(TAG, "Presentation was dismissed.");mPresentation = null;updateContents();}}};}
  • 主界面的布局 一个surfaceview用来播放视频。imagebutton用来切换同步异步的按钮。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  tools:context=".demo.MainActivity" ><SurfaceView  android:id="@+id/surface1"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_centerInParent="true" /><!--切换同步异步的按钮的小按钮--><ImageButtonandroid:id="@+id/iv_state"android:visibility="visible"android:layout_alignParentLeft="true"android:layout_alignParentBottom="true"android:layout_marginLeft="5dp"android:layout_marginBottom="5dp"android:background="@drawable/circle_green"android:layout_width="50dp"android:layout_height="50dp" /></RelativeLayout> *imagebutton的按钮background的背景。改一个绿色和蓝色的颜色即可<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"android:useLevel="false"><size android:width="21dp"android:height="21dp" /><strokeandroid:width="0dp"android:color="@android:color/white" /><solidandroid:color="@color/colorPrimaryDark" />
</shape>
  • 副屏的展示界面
  • 副屏是用HDMI给的信号。
  • 副屏的播放一个video的视频 。video在项目的 assets文件里 可以随便拷贝一个放进去。
import android.app.Presentation;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.xinyintai.guoruiyibushow.R;
import java.io.IOException;
import static android.content.ContentValues.TAG;/*** 用于测试   android 异步播放。* <p>* 副屏界面   hdmi信号口*/public class DemoPresentation extends Presentation {private SurfaceView surfaceView;private MediaPlayer mediaPlayer1;boolean isLeave = true;public DemoPresentation(Context context, Display display) {super(context, display);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.presentation_content);// Set up the surface view for visual interest.mediaPlayer1 = new MediaPlayer();surfaceView = this.findViewById(R.id.surface2);surfaceView.getHolder().setKeepScreenOn(true);surfaceView.getHolder().addCallback(new SurfaceViewLis());mediaPlayer1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {@Overridepublic void onCompletion(MediaPlayer mp) {if (isLeave) {play("android.resource://" + "com.xinyintai.guoruiyibushow" + "/" + R.raw.video);}}});}public void pause() {mediaPlayer1.pause();}public void play(String path) {if (surfaceView != null) {mediaPlayer1.reset();mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);try {mediaPlayer1.setDataSource(getContext(), Uri.parse(path));// 把视频输出到SurfaceView上mediaPlayer1.setDisplay(surfaceView.getHolder());mediaPlayer1.prepare();mediaPlayer1.start();} catch (IOException e) {e.printStackTrace();}}}private class SurfaceViewLis implements SurfaceHolder.Callback {@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}@Overridepublic void surfaceCreated(SurfaceHolder holder) {Log.e(TAG, "surfaceCreated: surfaceivew产生了");try {play("android.resource://" + "com.xinyintai.guoruiyibushow" + "/" + R.raw.video);mediaPlayer1.seekTo(0);} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {}}@Overridepublic void onDetachedFromWindow() {super.onDetachedFromWindow();isLeave = false;Log.e(TAG, "onDetachedFromWindow: 副屏消失了");}@Overridepublic void onAttachedToWindow() {super.onAttachedToWindow();Log.e(TAG, "onAttachedToWindow: 副屏显示了");isLeave = true;}@Overrideprotected void onStop() {super.onStop();}
}
  • 副屏的布局只有一个surfaceview用来播放视频
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  tools:context=".demo.MainActivity" ><SurfaceView  android:id="@+id/surface2"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_centerInParent="true" />   </RelativeLayout> 

这样就实现了主屏和副屏同时播放视频。可以在主屏通过按钮。切换同步或者异步播放的功能。

Android 扩展屏幕 实现主屏副屏同步或者异步显示相关推荐

  1. 如何设置Windows扩展屏幕,扩展屏幕和主屏有何区别,如何优化使用扩展屏幕?

    支持电脑同时接多个显示器是Windows的一项基本功能,使用起来很简单,无须特别的外部硬件设备支持.只要将多个显示器(或设备,如UVA)连接到电脑的多个显卡输出上,重启电脑后,就可以在"显示 ...

  2. win7在扩展屏/副屏显示任务栏

    效果图 右边是主屏,左边是副屏 下载地址 Dual Monitor Taskbar-Dual Monitor Taskbar(双显示器任务栏工具)下载 v1.22绿色版--pc6下载站Dual Mon ...

  3. mac 投影android手机屏幕尺寸,mac电脑投屏到投影仪的方法(苹果投屏到电脑屏幕图文教程详解)...

    好消息是,据熟悉 macOS 10.15 开发的人士称,Apple 可能正在开发一款新的 Mac 和 iPad 的显示屏功能,提供原生的「互通」服务. 它能允许用户将任何应用程序的任何窗口发送到外部显 ...

  4. android studio 双屏,Android Presentation双屏异显,副屏的操作

    最近有一个双屏显示的需求,当时一脸蒙逼完全不知如何着手,不负众望找到解决办法,在Android4.2版本以后提供了Presentation类,可以轻松实现在两块屏幕上同时显示不同的内容.做一下笔记. ...

  5. 副屏幕全屏_电脑 双屏(双屏电脑主屏副屏设置)

    怎么把电脑一变二 双屏显示设置法 随着人们对现实要求越来越高,双屏显示成为用户越来越关注的对象,如抄股做T+0操作,既要关注大盘的走势又要关注股票的走势,就需要不断的切换,很麻烦:在看盘时看看网络电影 ...

  6. 副屏幕全屏_win7双屏电脑主屏副屏设置|Win7系统如何设置双屏显示?

    随着人们对现实要求越来越高,双屏显示越来越受人们的关注.在日常生活中,我们使用电脑一般只需要一个显示屏,不过有时候做一些工作需要用到多个显示屏.例如抄股做T+0操作,既要关注大盘的走势又要关注股票的走 ...

  7. 前端控制屏幕双屏显示 pos副屏 副屏方案

    之前在做到一个需求 电脑接了多个显示器  要在主屏上显示web界面   第二个屏幕上通过主屏来调起第二个界面 之前在网上看到的方案 :通过window.open(url, name, fulls)来打 ...

  8. android 大屏幕平台,三大平台大屏旗舰速度比拼!苹果完爆安卓/WP

    还记得上次外媒Techradar进行了安卓.iOS.WP三大平台旗舰手机的速度对比吗?结果,iPhone 6s完胜安卓台的LG G4.Galaxy S6.Xperia Z5三大旗舰,也将WP旗舰Lum ...

  9. java修改手机锁屏密码,Android 处理屏幕解锁和设置锁屏密码

    完成自定义service后,自定义service参考之前的博客. 在frameworks/base/core/java/android/app/customized/ICustomizedServic ...

最新文章

  1. Shutil.move PermissionError: [WinError 82] 无法创建目录或文件,以及PermissionError: [Errno 13] Permission denied
  2. c语言程序设计歌手大奖赛,C语言二维数组怎么做:设计青年歌手参加歌曲大奖赛计分系统: 共...,怎样用c语言程序设计? 青年歌手参加歌曲大奖赛,有10个评委...
  3. 参加web前端培训要学哪些知识
  4. Solr Schema.xml分析
  5. Visual C#使用DirectX实现视频播放
  6. 高端唯有定制,把 sublime 打造成专属的 IDE
  7. 奇异值(Singular value decomposition SVD)分解
  8. 我学的是设计模式的视频教程——命令模式vs策略模式,唠嗑
  9. 连接一切:自媒体的未来是什么??
  10. arthas 排查内存溢出_小学妹问我:如何利用可视化工具排查问题?
  11. 操作系统:第二章 进程管理2 - 处理机调度
  12. armqt字体ttf_QT字体的设置
  13. 美丽联合与腾讯云签署战略协议 全面借助腾讯云打造智慧时尚电商平台
  14. Armchart Js版属性学习与总结
  15. Ubuntu 命令手册
  16. hexo搭建博客文章目录分析
  17. 转化类操作符:map、mapTo和pluck
  18. cajviewer打不开,卸载重装也于事无补。一分钟解决,亲测有效。
  19. java汉字转换为拼音
  20. 我的开源项目,趣享GIF源代码已正式公开

热门文章

  1. JVM之类加载阶段详解
  2. 单元测试、集成测试、确认测试、系统测试、验收测试
  3. java和JSP和JavaScript区别
  4. 第十四周java实验作业
  5. 笔记整理:数据处理方式Data Processing
  6. NOJ [1120] Reimu\'s Teleport
  7. PC微信机器人接口api之找微信个人数据基址
  8. 多媒体库SDL以及实时音视频库WebRTC中的多线程问题实战详解
  9. excel离散度图表怎么算_一般人不知道的几个excel制图技巧
  10. 只要功夫深,铁棒磨成针