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

InfinityMenu is an Android Library implementing an accordion style menu. You can place any view with any size in the menu. To close the menu you can drag to close or tap outside the menu.

The easiest Kotlin Library for Android Runtime Permissions & Device Info.

Android MediaPlayer/VideoView API-compatible media player library with exact seek, playback speed adjustment and DASH support.

SwitchDateTime Picker is a library for select a Date object in dialog with a DatePicker (Calendar) and a TimePicker (Clock) in the same UI.

AndroidMaryTTS is an open source Android offline text to speech application, built on top of MaryTTS. Can use own HMM-based voice in any android application with using this lib.

The IpGetter is a plugin which will help us to get the Ip of the current machine to use on a build.gradle.

You may ask, why do I need that? The answer is simple, you need that to replace your API URL for your computer address during development stage, to point to a MockApi or a under development api. That's the main idea.

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