android媒体在二级设备上呈现和播放

2024-04-23 01:08

本文主要是介绍android媒体在二级设备上呈现和播放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

public class PresentationWithMediaRouterActivity extends Activity {private final String TAG = "PresentationWithMediaRouterActivity";// MediaRouter用于和MediaRouterService交互一起管理多媒体的播放行为,并维护当前已经配对上的remote// display设备,包括Wifi diplay、蓝牙A2DP设备、chromecast设备。private MediaRouter mMediaRouter;// MediaRouter提供了快速获得系统中用于演示(presentations)默认显示设备的方法。private DemoPresentation mPresentation;private GLSurfaceView mSurfaceView;private TextView mInfoTextView;private boolean mPaused;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/** 获取到媒体路由,当媒体路由被选择或取消选择或者路由首选的presentation显示屏幕发生变化时,* 它都会发送通知消息。一个应用程序可以非常简单通过地观察这些通知消息来自动地在首选的presentation* 显示屏幕上显示或隐藏一个presentation。*/mMediaRouter = (MediaRouter) getSystemService(Context.MEDIA_ROUTER_SERVICE);setContentView(R.layout.presentation_with_media_router_activity);mSurfaceView = (GLSurfaceView) findViewById(R.id.surface_view);// 设置我们要渲染的图形为CubeRenderermSurfaceView.setRenderer(new CubeRenderer(false));mInfoTextView = (TextView) findViewById(R.id.info);}@Overrideprotected void onResume() {super.onResume();/*** 注册一个具体的MediaRouter.Callback回调对象,并用来启动与特定MediaRouteSelector相匹配的媒体路由的发现* , 以及监听发现的媒体路由的相关事件,如用户已选择连接到某个媒体路由设备、某个媒体路由设备的特性发生改变或者断开某个媒体路由等事件。* 应用为了使用相关的媒体路由,必须调用该函数来启动媒体路由的发现,并通过登记的回调函数接收相关的事件。*/// 设置对媒体路由变化的监听mMediaRouter.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO,mMediaRouterCallback);mPaused = false;updatePresentation();}@Overrideprotected void onPause() {super.onPause();// 移除回调监听mMediaRouter.removeCallback(mMediaRouterCallback);mPaused = true;updateContents();}@Overrideprotected void onStop() {super.onStop();// 当Activity不可见时,清除Presentationif (mPresentation != null) {Log.i(TAG,"Dismissing presentation because the activity is no longer visible.");mPresentation.dismiss();mPresentation = null;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {super.onCreateOptionsMenu(menu);// 加载菜单getMenuInflater().inflate(R.menu.presentation_with_media_router_menu,menu);MenuItem mediaRouteMenuItem = menu.findItem(R.id.menu_media_route);MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) mediaRouteMenuItem.getActionProvider();mediaRouteActionProvider.setRouteTypes(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);// 显示菜需要返回truereturn true;}/*** 代码示例展示了Presentation实现对象的作为一个单独方法的控制层.当一个显示器处理不可选状态或者失去联系时,该方法负责清除无效的展示对象,* 而在一个显示设备连接时负责创建一个展示对象。* * @description:* @author ldm* @date 2016-6-4 上午9:34:05*/private void updatePresentation() {// 获取当前路由MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);// 判断route信息是否为空,如果不为空则返回被选择演示(presentation)设备。该方法只对// route信息类型为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.i(TAG, "Showing presentation on display: "+ presentationDisplay);mPresentation = new DemoPresentation(this, presentationDisplay);mPresentation.setOnDismissListener(mOnDismissListener);try {mPresentation.show();} catch (WindowManager.InvalidDisplayException ex) {Log.w(TAG,"Couldn't show presentation!  Display was removed in "+ "the meantime.", ex);mPresentation = null;}}// 更新Activity中内容updateContents();}/*** 更新内容* * @description:* @author ldm* @date 2016-6-4 上午9:27:40*/private void updateContents() {if (mPresentation != null) {mInfoTextView.setText(getResources().getString(R.string.presentation_with_media_router_now_playing_remotely,mPresentation.getDisplay().getName()));mSurfaceView.setVisibility(View.INVISIBLE);mSurfaceView.onPause();if (mPaused) {mPresentation.getSurfaceView().onPause();} else {mPresentation.getSurfaceView().onResume();}} else {mInfoTextView.setText(getResources().getString(R.string.presentation_with_media_router_now_playing_locally,getWindowManager().getDefaultDisplay().getName()));mSurfaceView.setVisibility(View.VISIBLE);if (mPaused) {mSurfaceView.onPause();} else {mSurfaceView.onResume();}}}private final MediaRouter.SimpleCallback mMediaRouterCallback = new MediaRouter.SimpleCallback() {// 当用户连接到一个媒体路由输出设备上时调用。@Overridepublic void onRouteSelected(MediaRouter router, int type, RouteInfo info) {Log.d(TAG, "onRouteSelected: type=" + type + ", info=" + info);updatePresentation();}// 当用户断开一个媒体路由输出设备时调用。@Overridepublic void onRouteUnselected(MediaRouter router, int type,RouteInfo info) {Log.d(TAG, "onRouteUnselected: type=" + type + ", info=" + info);updatePresentation();}// 当展示的显示器改变现实像素,如从720p变到1080p分辨率。@Overridepublic void onRoutePresentationDisplayChanged(MediaRouter router,RouteInfo info) {Log.d(TAG, "onRoutePresentationDisplayChanged: info=" + info);updatePresentation();}};/*** Listens for when presentations are dismissed.*/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();}}};/*** 要为辅助显示屏创建独特的内容,您需要扩展Presentation类,并实现onCreate()回调方法。在onCreate()中,* 调用setContentView()来指定您要在辅助显示屏上显示的UI。* 作为Dialog类的扩展,Presentation类提供了一个区域,在其中, 您的应用可以在辅助显示屏上显示不同的UI。* * @description:* @author ldm* @date 2016-6-4 上午9:08:45*/private final static class DemoPresentation extends Presentation {/*** GLSurfaceView是一个视图,继承至SurfaceView,它内嵌的surface专门负责OpenGL渲染。* GLSurfaceView提供了下列特性: 1>* 管理一个surface,这个surface就是一块特殊的内存,能直接排版到android的视图view上。 2> 管理一个EGL* display,它能让opengl把内容渲染到上述的surface上。 3> 用户自定义渲染器(render)。 4>* 让渲染器在独立的线程里运作,和UI线程分离。 5> 支持按需渲染(on-demand)和连续渲染(continuous)。* 6>一些可选工具,如调试。*/private GLSurfaceView mSurfaceView;public DemoPresentation(Context context, Display display) {super(context, display);}@Overrideprotected void onCreate(Bundle savedInstanceState) {// 必须要写下面这句话,调用父类的onCreate();super.onCreate(savedInstanceState);// 设置布局setContentView(R.layout.presentation_with_media_router_content);// mSurfaceView对象获取:建立有趣的视觉mSurfaceView = (GLSurfaceView) findViewById(R.id.surface_view);mSurfaceView.setRenderer(new CubeRenderer(false));}public GLSurfaceView getSurfaceView() {return mSurfaceView;}}
}

相关代码请见:https://github.com/ldm520/ANDROID_API_DEMOS
参考博客:
http://blog.csdn.net/lilian0118/article/details/22940943
http://blog.csdn.net/michaelcao1980/article/details/9293441

这篇关于android媒体在二级设备上呈现和播放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/927372

相关文章

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

录音功能在哪里? 电脑手机等设备打开录音功能的技巧

《录音功能在哪里?电脑手机等设备打开录音功能的技巧》很多时候我们需要使用录音功能,电脑和手机这些常用设备怎么使用录音功能呢?下面我们就来看看详细的教程... 我们在会议讨论、采访记录、课堂学习、灵感创作、法律取证、重要对话时,都可能有录音需求,便于留存关键信息。下面分享一下如何在电脑端和手机端上找到录音功能

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Linux之platform平台设备驱动详解

《Linux之platform平台设备驱动详解》Linux设备驱动模型中,Platform总线作为虚拟总线统一管理无物理总线依赖的嵌入式设备,通过platform_driver和platform_de... 目录platform驱动注册platform设备注册设备树Platform驱动和设备的关系总结在 l

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class