Beam Media

A common use case with Connect SDK will be to beam a simple media file (image, video, audio) to a TV. The following is a quick example of how you can beam an image onto a TV. This example is assuming that you have discovered & connected to a device.

Beam an image file

String mediaURL = "http://www.connectsdk.com/files/9613/9656/8539/test_image.jpg"; // credit: Blender Foundation/CC By 3.0
String iconURL = "http://www.connectsdk.com/files/2013/9656/8845/test_image_icon.jpg"; // credit: sintel-durian.deviantart.com
String title = "Sintel Character Design";
String description = "Blender Open Movie Project";
String mimeType = "image/jpeg";

List<ImageInfo> imageList = Arrays.asList(new ImageInfo(iconURL));
MediaInfo mediaInfo = new MediaInfo(mediaURL, mimeType, title, description, imageList);

// These variable should be class fields
// LaunchSession mLaunchSession;
// MediaControl mMediaControl;
// ConnectableDevice mDevice;

MediaPlayer.LaunchListener listener = new MediaPlayer.LaunchListener() {
    @Override
    public void onSuccess(MediaLaunchObject object) {
        // save these object references to control media playback
        mLaunchSession = object.launchSession;
        mMediaControl = object.mediaControl;

        // you will want to enable your media control UI elements here
    }

    @Override
    public void onError(ServiceCommandError error) {
        Log.d("App Tag", "Display photo failure: " + error);
    }
};

mDevice.getMediaPlayer().displayImage(mediaInfo, listener);

Beam an audio/video file

String mediaURL = "http://www.connectsdk.com/files/8913/9657/0225/test_video.mp4"; // credit: Blender Foundation/CC By 3.0
String title = "Sintel Trailer";
String description = "Blender Open Movie Project";
String mimeType = "video/mp4"; // audio/* for audio files

List<ImageInfo> imageList = Arrays.asList(new ImageInfo(iconURL));
MediaInfo mediaInfo = new MediaInfo(mediaURL, mimeType, title, description, imageList);

// These variables should be class fields
// LaunchSession mLaunchSession;
// MediaControl mMediaControl;
// ConnectableDevice mDevice;

MediaPlayer.LaunchListener listener = new MediaPlayer.LaunchListener() {
    @Override
    public void onSuccess(MediaLaunchObject object) {
        // save these object references to control media playback
        mLaunchSession = object.launchSession;
        mMediaControl = object.mediaControl;

        // you will want to enable your media control UI elements here
    }

    @Override
    public void onError(ServiceCommandError error) {
        Log.d("App Tag", "Play media failure: " + error);
    }
};

mDevice.getMediaPlayer().playMedia(mediaInfo, false, listener);

Control media playback

In the previous example, you will notice that the success block was called with a mediaControl object. In order to control the media in the current playback session, you will need to store a reference to this mediaControl object and call control methods on that object.

// pause media file
mMediaControl.pause(null);

// play media file
mMediaControl.play(null);

// seek to 10 seconds
mMediaControl.seek(10000L, null);

// close media file
mMediaControl.close(null);
// or
mDevice.getMediaPlayer().closeMedia(mLaunchSession, null);

Beam a playlist

// These variables should be class fields
// LaunchSession mLaunchSession;
// MediaControl mMediaControl;
// PlaylistControl mPlaylistControl;
// ConnectableDevice mDevice;

MediaInfo mediaInfo = new MediaInfo("your-playlist.m3u",
"application/x-mpegurl", "Playlist", "Playlist description");
mDevice.getMediaPlayer().playMedia(mediaInfo, false, new MediaPlayer.LaunchListener() {
    @Override
    public void onSuccess(MediaLaunchObject object) {
        // save these object references to control media playback
        mLaunchSession = object.launchSession;
        mMediaControl = object.mediaControl;
        // playlistControl can be null if it's not supported by a service
        mPlaylistControl = object.playlistControl;
        // you will want to enable your media control UI elements here
    }

    @Override
    public void onError(ServiceCommandError error) {
        Log.d("App Tag", "Play playlist failure: " + error);
    }
});

Control a playlist

// play previous track
mPlaylistControl.previous(null);
// play next track
mPlaylistControl.next(null);
// play a track specified by index (index starts from zero)
mPlaylistControl.jumpToTrack(0, null);