Android Media Player with SurfaceView Example

In this android tip, we are going to learn how to create video streaming using android Media Player and SurfaceView.

MediaPlayer class can be used to control playback of audio/video files and streams. Android Media Player is not the only way to stream and display video in android. You can as well use the android VideoView.

I wrote a tutorial on android VideoView with MediaController that afford the VideoView the PlayBack features in android.

Before we dive into more details, it is important for us to understand what we are planning to achieve. Below is the screen-shot of the application we will be creating.

android videoview

Lets start to soil our hands in code. Start up your IDE. For this tutorial, I am using the following tools and environment, feel free to use what works for you.

Windows 10

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 23

To create a new android application project, follow the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidMediaPlayer

Package: com.inducesmile.androidmediaplayer

Select Blank Activity

Name your activity : MainActivity

Keep other default selections

Continue to click on next button until Finish button is active, then click on Finish Button.



We are going to update our project strings.xml file located in the values folder inside the res folder. Open the file and add the code below to it.

<string name="app_name">Android MediaPlayer</string>
    <string name="app_name">Android MediaPlayer</string>



Open the colors.xml file in the same location as the strings.xml file and add the code below to the file.

<?xml version="1.0" encoding="utf-8"?>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="colorBlack">#000000</color>
    <color name="colorDivider">#B6B6B6</color>



Since our video file path point to remote source, we are going to add user permission for INTERNET.

Open the Manifest.xml file located in Manifest folder and add the code below to the file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    <uses-permission android:name="android.permission.INTERNET"/>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />



We are going to add a SurfaceView control in the main layout file. Set the layout width and height to match_parent. Go to the layout folder inside the res folder, open the activity_main.xml and add the code below to the file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:layout_height="match_parent" />


In MainActivity class, we are going to create a SurfaceView and MediaPlayer objects. The path to the video is provided in string format.

In Oncreate() method of the Activity class, we will use the getHolder() method with the SurfaceView object to obtain an instance of the SurfaceHolder. Thereafter, the addCallback() method is attached to it.

The MainActivity class will implement the SurfaceHolder.Callback and MediaPlayer.OnPreparedListener. This will add four new ovveride methods to the class.

In the surfaceCreated(SurfaceHolder holder) method, we will prepare the MediaPlayer instance by setting the display to the SurfaceHolder object. The path to the remote video source is as well passed as a parameter in its setDataSource() method.

Open the file and add the code below to the file.

import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
    private SurfaceView mSurfaceView;
    private MediaPlayer mMediaPlayer;
    private SurfaceHolder mSurfaceHolder;
    private static final String VIDEO_PATH = "";
    protected void onCreate(Bundle savedInstanceState) {
        mSurfaceView = (SurfaceView)findViewById(;
        mSurfaceHolder = mSurfaceView.getHolder();
    public void surfaceCreated(SurfaceHolder holder) {
        mMediaPlayer = new MediaPlayer();
        try {
        } catch (IOException e) {
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    public void surfaceDestroyed(SurfaceHolder holder) {
    public void onPrepared(MediaPlayer mp) {
    protected void onPause() {
    protected void onDestroy() {
    private void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer = null;

This is to give you an idea but there are lots of things to do if you want improve this code.

This brings us to the end of this tutorial. I hope that you have learn something. Run your app and see for yourself.

You can download the code for this tutorial below. If you are having hard time downloading the tutorial, kindly contact me.

Remember to subscribe with your email address so that you will be among the first to receive my new android blog post once it is published.

Please if you love this tutorial, kindly download my android app – Complete Mathematics – in Google Play Store and let me know what you think about it.

One Response

  1. Avatar

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.