home-assistant-libs / pychromecast

Library for Python 3 to communicate with the Google Chromecast.
MIT License
2.53k stars 377 forks source link

Playing audio files from dropbox or google drive #186

Open simonwalkersamuel opened 7 years ago

simonwalkersamuel commented 7 years ago

Hi,

This might be a naive question, but is it possible to play an audio file from dropbox or google drive? I've amended the example code from the readme to try and play an MP3 file, but I just hear the default 'chromecast sound' played and nothing else. I've also tried an MP4, alongside changing the MIME definition to 'audio/mp3', 'audio/mp4' and 'audio/mpeg', none of which seem to help...

Is dropbox simply not supported? I tried with google drive, but that didn;t work either. Perhaps I'm misunderstanding what can be done with the chromecast protocol?

from __future__ import print_function
import time
import pychromecast

chromecasts = pychromecast.get_chromecasts()
name = 'Kitchen Speakers'
kh = [cc for cc in chromecasts if cc.device.friendly_name==name][0]

# Wait for cast device to be ready
kh.wait()

mc = kh.media_controller
dblink = r'http://www.dropbox.com/s/ggibxt035xvd7a4/demo.mp3?dl=0'
mc.play_media(dblink, 'audio/mpeg')
# mc.play_media(dblink, 'audio/mp4') # This doesn't seem to work either...
# mc.play_media(dblink, 'audio/mpeg') # Or this
strunker commented 7 years ago

Not entirely sure, have never tried drop box.

but if it was going to work you would need a direct link to the audio. you fed it a link to the drop box page which is not going work.

http://dl.dropboxusercontent.com/content_link/Kp97Rbx4tCxzQ49CixYysgA4NkXUwIzCeMOqr0FNDeJHW4hC7x50GuWzPa4rZ1N3/file?dl=0&duc_id=dropbox_duc_id://dl.dropboxusercontent.com/content_link/Kp97Rbx4tCxzQ49CixYysgA4NkXUwIzCeMOqr0FNDeJHW4hC7x50GuWzPa4rZ1N3/file?dl=0&duc_id=dropbox_duc_id

tjoen commented 6 years ago

just use mc.play_media(dblink, 'audio/mp3') and use the real location of that file, not the url you provided.

mc = cast.media_controller mc.play_media('https://dl.dropboxusercontent.com/content_link/WWK9zaBuvyVW2nASiUNSbOnUBPC9M44VHDq3B3JkXERFNvtERAWdlTHr3MB63s8e/file?dl=0&duc_id=dropbox_duc_id', 'audio/mp3') print(mc.status)