Home > Chromecast, PHP, Video > Chromecast: Playing Local Media

Chromecast: Playing Local Media

February 6th, 2014

The Chromecast SDK just became publicly available yesterday and I wasted no time figuring out how to cast my local media (finally).

This is a hack at the moment while I work on writing a Chrome extension to act as the server (or someone else finishes it first). It works well enough for me for the time being. If you’re going to set this up, you’ll need a web server with PHP running on the computer that holds your media. Again, this is just a hack.

After cursing JavaScript again since my last adventure with Google Maps, I finally got the most basic code I could come up with to cast video. The HTML sucks, the JavaScript sucks, and the error handling is non-existent. But it works (barely). There is no seeking. The only thing it can do is play and pause. With a Chrome extension, you can write a very basic server in JavaScript to serve the movie file which removes the needs for a web server on your host.

Until then, I set up Apache with mod_php, set my DocumentRoot to my movie directory, stuck jquery.js in there and created a PHP script that does the HTML/Chromecast stuff.

First, stick jquery in your movie directory:

curl http://code.jquery.com/jquery-1.11.0.min.js > ~/Movies/jquery.js

And then create index.php with this code:

<html><head><title>cast</title>
<script type="text/javascript"
 src="http://localhost/movies/jquery.js"></script>
<script type="text/javascript"
 src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"></script>
</head><body>

<script type="text/javascript">
$(function(){

session = null;
currentMedia = null;
if (!chrome.cast || !chrome.cast.isAvailable) {
  setTimeout(initializeCastApi, 1000);
  }

function initializeCastApi() {
var applicationID = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID;
var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
    sessionListener,
    receiverListener);
chrome.cast.initialize(apiConfig, onInitSuccess, onError);
chrome.cast.requestSession(onRequestSessionSuccess, onLaunchError);
};

function onMediaDiscovered(how, media) {
       currentMedia = media;
}

function receiverListener(e) {
if( e === 'available' ) {
}
}

function onInitSuccess() {
      console.log("init success");
};

function onError() {
      console.log("error");
};


function onRequestSessionSuccess(e) {
    console.log('got it');
          session = e;
           }

function sessionListener(e) {
      session = e;
}

function onMediaError(e) {
    console.log('media error ' + e.code);
}

function onLaunchError() {
    console.log('launch error');
}


})

function setmedia(e) {
var mediaInfo = new chrome.cast.media.MediaInfo(
  'http://10.0.0.4/movies/' + e, 'video/mp4');
var request = new chrome.cast.media.LoadRequest(mediaInfo);
console.log(request);
  request.autoplay = true;
window.session.loadMedia(request,
       onMediaDiscovered.bind(this, 'loadMedia'),
          onError);
}

function onMediaDiscovered(how, media) {
       window.currentMedia = media;
       console.log('got currentmedia');
}
function mediaCommandSuccessCallback(e) {
    console.log('command success');
}
function onError() {
    console.log('error');
}


function play() {
    window.currentMedia.play(null,
            mediaCommandSuccessCallback,
            onError);
}
function pause() {
    window.currentMedia.pause(null,
            mediaCommandSuccessCallback,
            onError);
}
</script>
<font face=verdana>
<?php
echo "<a href=\"#\" onclick=\"javascript:play()\">Play</a>&nbsp;&nbsp;|";
echo "&nbsp;&nbsp;<a href=\"#\" onclick=\"javascript:pause()\">Pause</a><br><hr>\n";
foreach (scandir('.') as $f) {
    if ($f[0] == '.' || $f == '..') {
        continue;
    }
    if (! preg_match('/(.*)\.(mp4|m4v|avi)$/', $f, $regs)) {
        continue;
    }
    $name = $regs[1];
    echo "<a href=\"#\" onclick=\"javascript:window.setmedia('$f');\">$name</a><br>\n";
}

?>
</font>
</body></html>

Chromecast, PHP, Video

  1. No comments yet.
  1. No trackbacks yet.