|
| 1 | +""" |
| 2 | +Example changing the playback rate. |
| 3 | +
|
| 4 | +""" |
| 5 | + |
| 6 | +# pylint: disable=invalid-name |
| 7 | + |
| 8 | +import argparse |
| 9 | +import sys |
| 10 | +import time |
| 11 | + |
| 12 | +import pychromecast |
| 13 | + |
| 14 | +from .common import add_log_arguments, configure_logging |
| 15 | + |
| 16 | +# Enable deprecation warnings etc. |
| 17 | +if not sys.warnoptions: |
| 18 | + import warnings |
| 19 | + |
| 20 | + warnings.simplefilter("default") |
| 21 | + |
| 22 | +# Change to the friendly name of your Chromecast |
| 23 | +CAST_NAME = "Living Room" |
| 24 | + |
| 25 | +# Change to an audio or video url |
| 26 | +MEDIA_URL = ( |
| 27 | + "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" |
| 28 | +) |
| 29 | + |
| 30 | +parser = argparse.ArgumentParser( |
| 31 | + description="Example on how to use the Media Controller." |
| 32 | +) |
| 33 | +parser.add_argument( |
| 34 | + "--cast", help='Name of cast device (default: "%(default)s")', default=CAST_NAME |
| 35 | +) |
| 36 | +parser.add_argument( |
| 37 | + "--known-host", |
| 38 | + help="Add known host (IP), can be used multiple times", |
| 39 | + action="append", |
| 40 | +) |
| 41 | +add_log_arguments(parser) |
| 42 | +parser.add_argument( |
| 43 | + "--url", help='Media url (default: "%(default)s")', default=MEDIA_URL |
| 44 | +) |
| 45 | +args = parser.parse_args() |
| 46 | + |
| 47 | +configure_logging(args) |
| 48 | + |
| 49 | +chromecasts, browser = pychromecast.get_listed_chromecasts( |
| 50 | + friendly_names=[args.cast], known_hosts=args.known_host |
| 51 | +) |
| 52 | +if not chromecasts: |
| 53 | + print(f'No chromecast with name "{args.cast}" discovered') |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | +cast = chromecasts[0] |
| 57 | + |
| 58 | +# Start socket client's worker thread and wait for initial status update |
| 59 | +cast.wait() |
| 60 | + |
| 61 | +print(f'Playing media "{args.url}"') |
| 62 | +cast.play_media(args.url, "video/mp4") |
| 63 | + |
| 64 | +print("Waiting for media session to be active") |
| 65 | +cast.media_controller.block_until_active() |
| 66 | + |
| 67 | +SPEEDS = [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2] |
| 68 | +for speed in SPEEDS: |
| 69 | + time.sleep(10) |
| 70 | + print(f"Setting playback rate to {speed}") |
| 71 | + cast.media_controller.set_playback_rate(speed) |
| 72 | + |
| 73 | +# Shut down discovery |
| 74 | +browser.stop_discovery() |
0 commit comments