Android 6.0 (API level 23) changes

You can see the list of API differences for the release of Android 6.0 Marshmallow here. More importantly, we are going to focus on the new addition to the android.media package, MediaDataSource.

MediaPlayer

A simple implementation for the Android MediaPlayer could be set up in the following manner, to play a video:

As you can see, setDataSource()’s parameter typically is a String or FileDescriptor that describes the URI location of a video file. It allows for a number of protocols such as RTSP and HTTP. The media player abstracts the networking aspect of receiving the video, buffering, etc, but you can’t have video physically in code and set that as a resource. You need to write whatever you have in the heap to internal storage, and give that as a resource to the MediaPlayer.

MediaDataSource

Now with the release of API 23, Android allows you to create a class that derives from MediaDataSource, provide the actual byte array, the video/audio, and handle whatever networking you need to do on your own. Here is what the abstract class consists of:

public abstract int readAt(long position, byte[] buffer, int offset, int size) throws IOException; public abstract long getSize() throws IOException;

The reference for MediaDataSource provides more of a description of each method and what they need to consist of.

Lets Create a Custom MediaDataSource

This tutorial will show the basics of extending a MediaDataSource and a general outline of what the networking could look like for retrieving a media source ourselves. The code will be available here, if you’d like to clone the project and test it out for yourselves. Or you can follow the tutorial in your own project.

We are going to be using Android Studio, and create a new project called “MediaDataSourceExample.”

Make sure to have a device or emulator that is using Android 6 (API 23).

Make sure to make the project’s minimum SDK is for API 23.

We are going to be using a blank activity, and that should just give a basic template to work with.