本文整理汇总了Python中pychromecast.Chromecast方法的典型用法代码示例。如果您正苦于以下问题:Python pychromecast.Chromecast方法的具体用法?Python pychromecast.Chromecast怎么用?Python pychromecast.Chromecast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pychromecast
的用法示例。
在下文中一共展示了pychromecast.Chromecast方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def __init__(self, cast: pychromecast.Chromecast, app: App, prep: Optional[str] = None) -> None:
self._cast = cast
self.name = app.name
self.info_type = None
self.save_capability = None
self.playlist_capability = None
self._cast_listener = CastStatusListener(app.id, self._cast.app_id)
self._cast.register_status_listener(self._cast_listener)
try:
self._cast.register_handler(self._controller) # type: ignore
except AttributeError:
self._controller = self._cast.media_controller
if prep == "app":
self.prep_app()
elif prep == "control":
self.prep_control()
elif prep == "info":
self.prep_info()
示例2: kill
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def kill(self, idle_only=False, force=False):
"""
Kills current Chromecast session.
:param idle_only: If set, session is only killed if the active Chromecast app
is idle. Use to avoid killing an active streaming session
when catt fails with certain invalid actions (such as trying
to cast an empty playlist).
:type idle_only: bool
:param force: If set, a dummy chromecast app is launched before killing the session.
This is a workaround for some devices that do not respond to this
command under certain circumstances.
:type force: bool
"""
if idle_only and not self._is_idle:
return
# The Google cloud app which is launched by the workaround is functionally
# identical to the Default Media Receiver.
if force:
listener = CastStatusListener(CLOUD_APP_ID)
self._cast.register_status_listener(listener)
self._cast.start_app(CLOUD_APP_ID)
listener.app_ready.wait()
self._cast.quit_app()
示例3: disconnect
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def disconnect(self, chromecast=None, timeout=None, blocking=True):
"""
Disconnect a Chromecast and wait for it to terminate
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
will be used.
:type chromecast: str
:param timeout: Number of seconds to wait for disconnection (default: None: block until termination)
:type timeout: float
:param blocking: If set (default), then the code will wait until disconnection, otherwise it will return
immediately.
:type blocking: bool
"""
cast = self.get_chromecast(chromecast)
cast.disconnect(timeout=timeout, blocking=blocking)
示例4: set_volume
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def set_volume(self, volume, chromecast=None):
"""
Set the Chromecast volume
:param volume: Volume to be set, between 0 and 100
:type volume: float
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
will be used.
:type chromecast: str
"""
chromecast = chromecast or self.chromecast
cast = self.get_chromecast(chromecast)
cast.set_volume(volume/100)
cast.wait()
status = self.status(chromecast=chromecast)
status.output['volume'] = volume
return status
示例5: volup
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def volup(self, chromecast=None, step=10):
"""
Turn up the Chromecast volume by 10% or step.
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
will be used.
:type chromecast: str
:param step: Volume increment between 0 and 100 (default: 100%)
:type step: float
"""
chromecast = chromecast or self.chromecast
cast = self.get_chromecast(chromecast)
step /= 100
cast.volume_up(min(step, 1))
cast.wait()
return self.status(chromecast=chromecast)
示例6: voldown
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def voldown(self, chromecast=None, step=10):
"""
Turn down the Chromecast volume by 10% or step.
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
will be used.
:type chromecast: str
:param step: Volume decrement between 0 and 100 (default: 100%)
:type step: float
"""
chromecast = chromecast or self.chromecast
cast = self.get_chromecast(chromecast)
step /= 100
cast.volume_down(max(step, 0))
cast.wait()
return self.status(chromecast=chromecast)
示例7: mute
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def mute(self, chromecast=None):
"""
Toggle the mute status on the Chromecast
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
will be used.
:type chromecast: str
"""
chromecast = chromecast or self.chromecast
cast = self.get_chromecast(chromecast)
cast.set_volume_muted(not cast.status.volume_muted)
cast.wait()
return self.status(chromecast=chromecast)
# vim:sw=4:ts=4:et:
示例8: chromecast_play_video
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def chromecast_play_video(phrase):
# Chromecast declarations
# Do not rename/change "TV" its a variable
TV = pychromecast.Chromecast("192.168.1.13") #Change ip to match the ip-address of your Chromecast
mc = TV.media_controller
idx1=phrase.find(custom_action_keyword['Dict']['Play'])
idx2=phrase.find('on chromecast')
query=phrase[idx1:idx2]
query=query.replace(custom_action_keyword['Dict']['Play'],'',1)
query=query.replace('on chromecast','',1)
query=query.strip()
youtubelinks=youtube_search(query)
youtubeurl=youtubelinks[0]
streams=youtube_stream_link(youtubeurl)
videostream=streams[1]
TV.wait()
time.sleep(1)
mc.play_media(videostream,'video/mp4')
示例9: get_chromecasts
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def get_chromecasts() -> List[pychromecast.Chromecast]:
devices = pychromecast.get_chromecasts()
devices.sort(key=lambda cc: cc.name)
return devices
示例10: get_chromecast
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def get_chromecast(device_name: Optional[str]) -> Optional[pychromecast.Chromecast]:
devices = get_chromecasts()
if not devices:
return None
if device_name:
try:
return next(cc for cc in devices if cc.name == device_name)
except StopIteration:
return None
else:
return devices[0]
示例11: get_chromecast_with_ip
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def get_chromecast_with_ip(device_ip: str, port: int = DEFAULT_PORT) -> Optional[pychromecast.Chromecast]:
try:
# tries = 1 is necessary in order to stop pychromecast engaging
# in a retry behaviour when ip is correct, but port is wrong.
return pychromecast.Chromecast(device_ip, port=port, tries=1)
except pychromecast.error.ChromecastConnectionError:
return None
示例12: get_cast
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def get_cast(device: Optional[str] = None) -> Tuple[pychromecast.Chromecast, CCInfo]:
"""
Attempt to connect with requested device (or any device if none has been specified).
:param device: Can be an ip-address or a name.
:type device: str
:returns: Chromecast object for use in a CastController,
and CCInfo object for use in setup_cast and StreamInfo
:rtype: (pychromecast.Chromecast, CCInfo)
"""
cast = None
if device and is_ipaddress(device):
cast = get_chromecast_with_ip(device, DEFAULT_PORT)
if not cast:
msg = "No device found at {}".format(device)
raise CastError(msg)
cc_info = CCInfo(cast.host, cast.port, None, None, cast.cast_type)
else:
cache = Cache()
maybe_cc_info = cache.get_data(device)
if maybe_cc_info:
# Get the Chromecast from the CCInfo IP/port.
cast = get_chromecast_with_ip(maybe_cc_info.ip, maybe_cc_info.port)
cc_info = maybe_cc_info
if not cast:
cast = get_chromecast(device)
if not cast:
msg = 'Specified device "{}" not found'.format(device) if device else "No devices found"
raise CastError(msg)
cc_info = CCInfo(cast.host, cast.port, cast.device.manufacturer, cast.model_name, cast.cast_type)
cache.set_data(cast.name, cc_info)
cast.wait()
return (cast, cc_info)
示例13: wait_for
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def wait_for(self, states: list, invert: bool = False, fail: bool = False, timeout: Optional[int] = None) -> bool:
media_listener = MediaStatusListener(
self._cast.media_controller.status.player_state, states, invert=invert, fail=fail
)
self._cast.media_controller.register_status_listener(media_listener)
try:
return media_listener.wait_for_states(timeout=timeout)
except pychromecast.error.UnsupportedNamespace:
raise CastError("Chromecast app operation was interrupted")
示例14: __init__
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def __init__(self, chromecast=None, *args, **kwargs):
"""
:param chromecast: Default Chromecast to cast to if no name is specified
:type chromecast: str
"""
super().__init__(*args, **kwargs)
self._is_local = False
self.chromecast = chromecast
self.chromecasts = {}
self._media_listeners = {}
示例15: get_chromecast
# 需要导入模块: import pychromecast [as 别名]
# 或者: from pychromecast import Chromecast [as 别名]
def get_chromecast(self, chromecast=None, n_tries=2):
import pychromecast
if isinstance(chromecast, pychromecast.Chromecast):
return chromecast
if not chromecast:
if not self.chromecast:
raise RuntimeError('No Chromecast specified nor default Chromecast configured')
chromecast = self.chromecast
if chromecast not in self.chromecasts:
casts = {}
while n_tries > 0:
n_tries -= 1
casts.update({
cast.device.friendly_name: cast
for cast in pychromecast.get_chromecasts()
})
if chromecast in casts:
self.chromecasts.update(casts)
break
if chromecast not in self.chromecasts:
raise RuntimeError('Device {} not found'.format(chromecast))
cast = self.chromecasts[chromecast]
cast.wait()
return cast