Casty


Source link: https://github.com/DroidsOnRoids/Casty

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.7' 
}

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 "You can 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

Resources

Gradle plugin that tells you what libs have new versions on Maven Central, so when you come back to a project, you know what you can update.

A helper class that extends SpannableStringBuilder and adds methods to easily mark the text with multiple spans.

Print it, cut it, give it to your designer!

Salut is a wrapper around the WiFi Direct service discovery API in Android. Before using Salut, you should at least skim over some of the documentation and recommended reading below.

NexusData is an object graph and persistence framework for Android. It allows for organizing and managing relational data and serializing it to SQLite or custom stores. The data can be accessed or modified using higher level objects representing entities and their relationships. NexusData manages all the objects in the persistence store, tracks changes, and maintains consistency in relationships.

NexusDialog is a library that allows you to dynamically generate forms in Android with little code. It's great for apps with many form-based UIs, since it reduces the boilerplate code to setup the view layout and tie things together in the Activity.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes