android 屏幕投射_将自定义内容从Android应用投射到电视(2020年方法)

android 屏幕投射

If you’re anything like me, who has never created a screen-cast app before and want to explore the options to do it, let’s dive in together.

如果您和我一样,以前从未创建过截屏应用程序,并且想探索实现此目的的选项,那么让我们一起来研究一下。

Things you should know already- Creating Android apps (used Kotlin for this post)

您应该已经知道的事情-创建Android应用程序(本文使用过Kotlin)

屏幕镜像或屏幕投射 (Screen mirroring or screen casting)

For most users, screen mirroring and screen casting might just mean the same thing: presenting the content from phone or any other small device to TV. It’s crucial to know the difference though, if you’re integrating one of these into your own apps.

对于大多数用户而言,屏幕镜像和屏幕投射可能只是同一件事:将内容从电话或任何其他小型设备呈现到电视。 但是,如果您要将其中之一集成到自己的应用程序中,则要知道它们之间的区别至关重要。

Screen Mirroring refers to showing your phone’s display on the TV as is. That means any clutter on the phone, navigation icons, and every other clickable item appears on the TV as well, even though on most TVs you can’t interact with those UI elements.

屏幕镜像是指按原样在电视上显示手机的显示屏。 这意味着电话上的任何杂物,导航图标以及所有其他可单击的项目也会出现在电视上,即使在大多数电视上,您也无法与这些UI元素进行交互。

Screen Casting means showing only the meaningful content on the TV. It’s used when you want to play a full-screen video on TV while your phone is showing controls, or maybe show graphs and charts on TV while your phone displays data in tabular format.

屏幕投射意味着仅在电视上显示有意义的内容。 当您要在手机显示控件时在电视上播放全屏视频,或者当手机以表格格式显示数据时在电视上显示图形和图表时,可以使用此功能。

规约 (Protocols)

The top most common protocols used for casting are:1. ChromeCast (Google Cast)2. MiraCast3. AirPlay4. Amazon Fling5. DLNA6. DIAL7. Samsung SmartTV

用于转换的最常见的协议是:1。 ChromeCast(Google Cast)2。 MiraCast3。 AirPlay4。 亚马逊物流5。 DLNA6。 拨号7。 三星SmartTV

We’ll focus on the first two of the list. I might soon add AirPlay and Amazon Fling also (although AirPlay works on Apple devices only).

我们将重点关注列表的前两个。 我可能很快还会添加AirPlay和Amazon Fling(尽管AirPlay仅适用于Apple设备)。

MiraCast (MiraCast)

MiraCast is essentially a screen-mirroring protocol. The typical use case is to mirror entire screen of your small device (phone or tablet), on a bigger device (TV). It is possible to override system behaviour and display our own (custom) views using MiraCast as well, which from a developer’s perspective will be a second screen being displayed on top of the mirrored screen.

MiraCast本质上是一个屏幕镜像协议。 典型的用例是在较大的设备(TV)上镜像小型设备(电话或平板电脑)的整个屏幕。 也可以使用MiraCast覆盖系统行为并显示我们自己的(自定义)视图,从开发人员的角度来看,这将是第二个屏幕显示在镜像屏幕的顶部。

On Android, MiraCast devices aren’t visible to app unless that MiraCast device is already selected to cast from the system settings (at least from Android 8.0).

在Android上,除非已选择从系统设置(至少从Android 8.0开始)投射MiraCast设备,否则应用程序将看不到MiraCast设备。

Once a MiraCast device is selected from settings, the app can then use MediaRouter API or DisplayManager API to get hold of a Display object, which can then be provided to Presentation object.

从设置中选择MiraCast设备后,应用程序便可以使用MediaRouter API或DisplayManager API来获取Display对象,然后可以将其提供给Presentation对象。

You can look for the selected MiraCast device by checking for selected route in the onResume method of the Activity or a Fragment.

您可以通过在“活动”或“片段”的onResume方法中检查选定的路线来查找选定的MiraCast设备。

override fun onResume() {
       
findSelectedDevice()
}private fun findSelectedDevice() { val route = mediaRouter.selectedRoute if (route.playbackType != PLAYBACK_TYPE_LOCAL && route.presentationDisplay != null) {
selectedDisplay = route.presentationDisplay
showPresentationDialog()
}

A Presentation object is just a common Android dialog, when you call its show() method, instead of being presented to screen, this dialog opens in fullscreen mode on the TV. Now you have any layout in that presentation dialog.

Presentation对象只是一个常见的Android对话框,当您调用它的show()方法时,该对话框以全屏模式在电视上打开,而不是显示在屏幕上。 现在,您可以在该演示文稿对话框中进行任何布局。

private fun showPresentationDialog() {
       
if (selectedDisplay == null)
return
if
(presentationDialogFragment != null && presentationDialogFragment!!.presentationDisplay != selectedDisplay) {
dismissPresentationDialog()
}
if (presentationDialogFragment == null) {
presentationDialogFra
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值