Skip to content

Media browser and playback

Yu-Hsuan Lin edited this page Sep 18, 2016 · 5 revisions

Below is an sample demonstrating some of the features of StraaS SDK media core. It will try to play the first content with rolling Interactive Media Ads (IMA) in your StraaS management system.

Be sure to fill up our SDK credential first.

private StraasMediaCore mMediaCore;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StraasPlayerView straasPlayerView = (StraasPlayerView) findViewById(R.id.straas);
    straasPlayerView.initialize(this);     
    mMediaCore = new StraasMediaCore(straasPlayerView, Identity.GUEST,
            new MediaBrowserCompat.ConnectionCallback() {
                @Override
                public void onConnected() {
                    getMediaBrowser().subscribe(getMediaBrowser().getRoot(),
                            new SubscriptionCallback() {
                                @Override
                                public void onChildrenLoaded(@NonNull String parentId,
                                                             List<MediaItem> children) {
                                    MediaItem item = children.get(0);
                                    if (item != null && item.isPlayable()) {
                                        getSupportMediaController().getTransportControls()
                                                .playFromMediaId(item.getMediaId(), null);
                                    }
                                }
                            });
                }
            })
            .setImaHelper(ImaHelper.newInstance());
    getMediaBrowser().connect();  
}

private MediaBrowserCompat getMediaBrowser() {
    return mMediaCore.getMediaBrowser();
}

@Override
protected void onStop() {
    super.onStop();
    if (getSupportMediaController() != null) {
        if (isFinishing()) {
            getSupportMediaController().unregisterCallback(mMediaControllerCallback);
            getSupportMediaController().getTransportControls().stop();
        } else {
            getSupportMediaController().getTransportControls().pause();
        }
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    getMediaBrowser().disconnect();
}