Code Monkey home page Code Monkey logo

casty's Introduction

Project no longer maintained

This project is no longer maintained. If you have any questions contact [email protected]

Casty

Casty is a small Android library that provides a simple media player for Chromecast. It's fully consistent with Google Cast v3.

Installation

Insert the following dependency to build.gradle file of your project:

dependencies {
    compile 'pl.droidsonroids:casty:1.0.8'
}

Usage

Casty requires Google Play Services and I assume that the target device has it installed (if not this example won't work). In a bigger project I suggest you check it using GoogleApiAvailability.

First, you need to initialize a Casty instance in every Activity you want to use it in:

public class MainActivity extends AppCompatActivity {
    private Casty casty;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        casty = Casty.create(this);
    }
}

If you want to add a Mini Controller widget, you can do it like this:

casty = Casty.create(this)
    .withMiniController();

Alternatively you can place it in your layout XML, just like in the official Google Cast example (remember to change fill_parent to match_parent ๐Ÿ˜†).

To support device discovery, add a menu item in overriden onCreateOptionsMenu method:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    casty.addMediaRouteMenuItem(menu);
    getMenuInflater().inflate(R.menu.your_menu, menu);
    return true;
}

Automatically, Introduction Overlay (with text "Use this button to connect with Chromecast") will be shown at the first device discovery. If you want to change it or add another language override string with id casty_introduction_text in a XML resource.

You can also add a discovery button anywhere else by placing it in your layout XML:

<android.support.v7.app.MediaRouteButton
    android:id="@+id/media_route_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

And then set in up in your Activity:

MediaRouteButton mediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
casty.setUpMediaRouteButton(mediaRouteButton);

You can add the above functionality (except MediaRouteButton) simply by extending CastyActivity. It will add a casty field and set up the rest:

public class MainActivity extends CastyActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (casty.isConnected()) {
            casty.getPlayer().loadMediaAndPlay(...)
        }
    }
}

All media player actions are included in a CastyPlayer object, which you can access by calling casty.getPlayer(). When you are connected to the device, you can play media the following way:

MediaData mediaData = new MediaData.Builder("http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4")
    .setStreamType(MediaData.STREAM_TYPE_BUFFERED) //required
    .setContentType("videos/mp4") //required
    .setMediaType(MediaData.MEDIA_TYPE_MOVIE)
    .setTitle("Sample title")
    .setSubtitle("Sample subtitle")
    .addPhotoUrl("https://peach.blender.org/wp-content/uploads/bbb-splash.png?x11217")
    .build();
casty.getPlayer().loadMediaAndPlay(mediaData);

Alternativly you can use loadMediaAndPlay(MediaInfo, autoPlay, position) similar to Google Cast example.

To react on Chromecast connect and disconnect events, you can simply register a listener:

casty.setOnConnectChangeListener(new Casty.OnConnectChangeListener() {
    @Override
    public void onConnected() {
        Log.d("Casty", "Connected with Chromecast");
    }
    
    @Override
    public void onDisconnected() {
        Log.d("Casty" "Disconnected from Chromecast");
    }
});

Custom usage

In case the library doesn't fit you, I left the possibility to change everything like in Google Cast v3. You can set receiver ID or even the whole CastOptions in your Application class:

Casty.configure(receiverId); //or
Casty.configure(customCastOptions);

Get CastContext by calling:

CastContext.getSharedInstance(context);

Get CastSession (so RemoteMediaClient) by register OnCastSessionUpdatedListener:

casty.setOnCastSessionUpdatedListener(new Casty.OnCastSessionUpdatedListener() {
    @Override
    public void onCastSessionUpdated(CastSession castSession) {
        if (castSession != null) {
            RemoteMediaClient remoteMediaClient = castSession.getRemoteMediaClient();
            //...
        }
    }
});

License

MIT

casty's People

Contributors

burnoo avatar koral-- 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.