python模块与google chromecast对话。

Cantiz-PyChromecast的Python项目详细描述


用于python 3.4+与google chromecast通信的库。它 当前支持:

  • 在网络上自动发现连接的Chromecast
  • 启动默认媒体接收器并播放任何联机媒体
  • 控制当前播放媒体的播放
  • 实现Google Chromecast API v2
  • 通过频道与应用程序通信
  • 易于扩展以添加对不受支持的命名空间的支持
  • 带音频播放设备的多房间设置

签出 用pychromecast控制和自动化的现成溶液 你的chromecast或cast设备,比如google home。

依赖关系

pychromecast依赖于python包请求、protobuf和 零配置。确保使用 pip install -r requirements.txt

如何使用

>>importtime>>importpychromecast>>chromecasts=pychromecast.get_chromecasts()>>[cc.device.friendly_nameforccinchromecasts]['Dev','Living Room','Den','Bedroom']>>cast=next(ccforccinchromecastsifcc.device.friendly_name=="Living Room")>># Start worker thread and wait for cast device to be ready>>cast.wait()>>print(cast.device)DeviceStatus(friendly_name='Living Room',model_name='Chromecast',manufacturer='Google Inc.',uuid=UUID('df6944da-f016-4cb8-97d0-3da2ccaa380b'),cast_type='cast')>>print(cast.status)CastStatus(is_active_input=True,is_stand_by=False,volume_level=1.0,volume_muted=False,app_id='CC1AD845',display_name='Default Media Receiver',namespaces=['urn:x-cast:com.google.cast.player.message','urn:x-cast:com.google.cast.media'],session_id='CCA39713-9A4F-34A6-A8BF-5D97BE7ECA5C',transport_id='web-9',status_text='')>>mc=cast.media_controller>>mc.play_media('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4','video/mp4')>>mc.block_until_active()>>print(mc.status)MediaStatus(current_time=42.458322,content_id='http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',content_type='video/mp4',duration=596.474195,stream_type='BUFFERED',idle_reason=None,media_session_id=1,playback_rate=1,player_state='PLAYING',supported_media_commands=15,volume_level=1,volume_muted=False)>>mc.pause()>>time.sleep(5)>>mc.play()

添加对额外名称空间的支持

在chromecast上运行的每个应用程序都支持名称空间。他们指定 基于json的mini协议。它用于 chromecast和你的手机/浏览器,现在是python。

通过使用控制器添加对额外名称空间的支持。要将自己的名称空间添加到当前chromecast实例,首先必须定义控制器。最小控制器示例:

frompychromecast.controllersimportBaseControllerclassMyController(BaseController):def__init__(self):super(MyController,self).__init__("urn:x-cast:my.super.awesome.namespace")defreceive_message(self,message,data):print("Wow, I received this message: {}".format(data))returnTrue# indicate you handled this messagedefrequest_beer(self):self.send_message({'request':'beer'})

定义控制器后,必须将实例添加到chromecast对象:cast.register\u handler(mycontroller())。当您的命名空间接收到消息时,它将被路由到您的控制器。

有关更多选项,请参见BaseController。有关完全实现的控制器的示例,请参见MediaController

探索现有的命名空间< /H2>

所以你已经运行了pychromecast,决定是时候为你最喜欢的应用添加支持了。不用担心,下面的说明将帮助您探索各种可能性。

以下说明要求使用Google Chrome browserGoogle Cast plugin

  • In Chrome, go to chrome://net-internals/#capture
  • Enable the checkbox ‘Include the actual bytes sent/received.’
  • Open a new tab, browse to your favorite application on the web that has Chromecast support and start casting.
  • Go back to the tab that is capturing events and click on stop.
  • From the dropdown click on events. This will show you a table with events that happened while you were recording.
  • In the filter box enter the text Tr@n$p0rt. This should give one SOCKET connection as result: the connection with your Chromecast.
  • Go through the results and collect the JSON that is exchanged.
  • Now write a controller that is able to mimic this behavior :-)

忽略CEC数据

chromecast通常报告它是否是设备上的活动输入 与之相连。此值存储在 跟踪属性。

cast.status.is_active_input

一些Chromecast用户报告说,CEC与他们的媒体不兼容 中心设备。这些不兼容有时可能导致此活动输入 不正确报告的值。

此活动输入值通常用于确定 是空闲的。当 在 Chromecast返回错误值。忽略这个cec检测 pychromecast中的数据,追加一个Linux style wildcard格式的字符串 到pychromecast中的ignore_cec列表,如下例所示。

pychromecast.IGNORE_CEC.append('*')# Ignore CEC on all devicespychromecast.IGNORE_CEC.append('Living Room')# Ignore CEC on Chromecasts named Living Room

谢谢

我要感谢Fred Clift为socket客户机接地 工作。没有他是不可能的!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Gson类中的java验证错误   If和elseif之间的java差异?   字典Java8地图。输入比较器   java连接到远程计算机以获得WMI支持   java如何使用改型处理JSON对象中的空值   在Java中使用Comparator时不可编译的源代码   java将Jar添加到JSP的运行时路径   带有随机对象的while循环的java大O时间复杂性   java可以在不考虑参数的情况下模拟方法吗?   java我有一个简单的代码,它不工作。无法修复错误“println”   向eclipse添加Xively java库   java是否可以启用本机代码的缓存?   全局变量如何在Java中的所有类之间共享要使用的语言环境?   Java内存游戏如何翻转单个卡?