Giter Club home page Giter Club logo

chromecast-java-api-v2's Introduction

ChromeCast Java API v2

At the moment I have started implementing this library, there was a java implementation of V1 Google ChromeCast protocol, which seems to be deprecated and does not work for newly created applications. The new V2 protocol is implemented by tools that come with Cast SDK, which is available for Android, iOS and Chrome Extension as javascript. Also there is a third party implementation of V2 in Node.js. This project is a third party implementation of Google ChromeCast V2 protocol in java.

Install

Library is available in maven central. Put lines below into you project's pom.xml file:

<dependencies>
...
  <dependency>
    <groupId>su.litvak.chromecast</groupId>
    <artifactId>api-v2</artifactId>
    <version>0.9.2</version>
  </dependency>
...
</dependencies>

Or to build.gradle (mavenCentral() repository should be included in appropriate block):

dependencies {
// ...
    runtime 'su.litvak.chromecast:api-v2:0.9.2'
// ...
}

Build

To build library from sources:

  1. Clone github repo

    $ git clone https://github.com/vitalidze/chromecast-java-api-v2.git

  2. Change to the cloned repo folder and run mvn install

    $ cd chromecast-java-api-v2 $ mvn install

  3. Then it could be included into project's pom.xml from local repository:

<dependencies>
...
  <dependency>
    <groupId>su.litvak.chromecast</groupId>
    <artifactId>api-v2</artifactId>
    <version>0.9.3-SNAPSHOT</version>
  </dependency>
...
</dependencies>

Usage

This is still a work in progress. The API is not stable, the quality is pretty low and there are a lot of bugs.

To use the library, you first need to discover what Chromecast devices are available on the network.

ChromeCasts.startDiscovery();

Then wait until some device discovered and it will be available in list. Then device should be connected. After that one can invoke several available operations, like check device status, application availability and launch application:

ChromeCast chromecast = ChromeCasts.get().get(0);
// Connect
chromecast.connect();
// Get device status
Status status = chromecast.getStatus();
// Run application if it's not already running
if (chromecast.isAppAvailable("APP_ID") && !status.isAppRunning("APP_ID")) {
  Application app = chromecast.launchApp("APP_ID");
}

To start playing media in currently running media receiver:

// play media URL directly
chromecast.load("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");
// play media URL with additional parameters, such as media title and thumbnail image
chromecast.load("Big Buck Bunny",           // Media title
                "images/BigBuckBunny.jpg",  // URL to thumbnail based on media URL
                "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", // media URL
                "video/mp4" // media content type
                );

Then playback may be controlled with following methods:

// pause playback
chromecast.pause();
// continue playback
chromecast.play();
// rewind (move to specified position (in seconds)
chromecast.seek(120);
// update volume
chromecast.setVolume(0.5f);
// mute
chromecast.setMuted(true);
// unmute (will set up volume to value before muting)
chromecast.setMuted(false);

Also there are utility methods to get current chromecast status (running app, etc.) and currently played media status:

Status status = chromecast.getStatus();
MediaStatus mediaStatus = chromecast.getMediaStatus();

Current running application may be stopped by calling stopApp() method without arguments:

// Stop currently running application
chromecast.stopApp();

Don't forget to close connection to ChromeCast device by calling disconnect():

// Disconnect from device
chromecast.disconnect();

Finally, stop device discovery:

ChromeCasts.stopDiscovery();

Alternatively, ChromeCast device object may be created without discovery if address of chromecast device is known:

ChromeCast chromecast = new ChromeCast("192.168.10.36");

Since v.0.9.0 there is a possibility to send custom requests using send() methods. It is required to implement at least Request interface for an objects being sent to the running application. If some response is expected then Response interface must be implemented. For example to send request to the DashCast application:

Request interface implementation

public class DashCastRequest implements Request {
    @JsonProperty
    final String url;
    @JsonProperty
    final boolean force;
    @JsonProperty
    final boolean reload;
    @JsonProperty("reload_time")
    final int reloadTime;

    private Long requestId;

    public DashCastRequest(String url,
                           boolean force,
                           boolean reload,
                           int reloadTime) {
        this.url = url;
        this.force = force;
        this.reload = reload;
        this.reloadTime = reloadTime;
    }

    @Override
    public Long getRequestId() {
        return requestId;
    }

    @Override
    public void setRequestId(Long requestId) {
        this.requestId = requestId;
    }
}

Sending request

chromecast.send("urn:x-cast:es.offd.dashcast", new DashCastRequest("http://yandex.ru", true, false, 0));

This is it for now. It covers all my needs, but if someone is interested in more methods, I am open to make improvements.

Useful links

Projects using library

License

(Apache v2.0 license)

Copyright (c) 2014 Vitaly Litvak [email protected]

chromecast-java-api-v2's People

Contributors

dereulenspiegel avatar haku avatar jruesga avatar maggu2810 avatar mrstevenfeldman avatar vitalidze avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.