home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (MediaPlayerDemo_Video.java)

This example Android source code file (MediaPlayerDemo_Video.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

activity, android, audio, exception, local_video, media, mediaplayerdemo_video, onbufferingupdatelistener, oncompletionlistener, override, photo, please, sound, stream_video, string, surfaceview, tag, toast, ui, view

The MediaPlayerDemo_Video.java Android example source code

package com.example.android.apis.media;

import com.example.android.apis.R;
import com.example.android.apis.app.AlarmController;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MediaPlayerDemo_Video extends Activity implements
        OnBufferingUpdateListener, OnCompletionListener,
        MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {

    private static final String TAG = "MediaPlayerDemo";
    private int mVideoWidth;
    private int mVideoHeight;
    private MediaPlayer mMediaPlayer;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    private String path;
    private Bundle extras;
    private static final String MEDIA = "media";
    private static final int LOCAL_AUDIO = 1;
    private static final int STREAM_AUDIO = 2;
    private static final int RESOURCES_AUDIO = 3;
    private static final int LOCAL_VIDEO = 4;
    private static final int STREAM_VIDEO = 5;

     * Called when the activity is first created.
    public void onCreate(Bundle icicle) {
        mPreview = (SurfaceView) findViewById(R.id.surface);
        holder = mPreview.getHolder();
        extras = getIntent().getExtras();


    private void playVideo(Integer Media) {
        try {

            switch (Media) {
                case LOCAL_VIDEO:
                     * TODO: Set the path variable to a local media file path.
                    path = "";
                    if (path == "") {
                        // Tell the user to provide a media file URL.
                                        "Please edit MediaPlayerDemo_Video Activity, "
                                                + "and set the path variable to your media file path."
                                                + " Your media file must be stored on sdcard.",

                case STREAM_VIDEO:
                     * TODO: Set path variable to progressive streamable mp4 or
                     * 3gpp format URL. Http protocol should be used.
                     * Mediaplayer can only play "progressive streamable
                     * contents" which basically means: 1. the movie atom has to
                     * precede all the media data atoms. 2. The clip has to be
                     * reasonably interleaved.
                    path = "";
                    if (path == "") {
                        // Tell the user to provide a media file URL.
                                        "Please edit MediaPlayerDemo_Video Activity,"
                                                + " and set the path variable to your media file URL.",




            // Create a new media player and set the listeners
            mMediaPlayer = new MediaPlayer();

        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);

    public void onBufferingUpdate(MediaPlayer arg0, int percent) {
        Log.d(TAG, "onBufferingUpdate percent:" + percent);


    public void onCompletion(MediaPlayer arg0) {
        Log.d(TAG, "onCompletion called");

    public void onPrepared(MediaPlayer mediaplayer) {
        Log.d(TAG, "onPrepared called");
        mVideoWidth = mMediaPlayer.getVideoWidth();
        mVideoHeight = mMediaPlayer.getVideoHeight();
        if (mVideoWidth != 0 && mVideoHeight != 0) {
            holder.setFixedSize(mVideoWidth, mVideoHeight);


    public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
        Log.d(TAG, "surfaceChanged called");


    public void surfaceDestroyed(SurfaceHolder surfaceholder) {
        Log.d(TAG, "surfaceDestroyed called");

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Log.d(TAG, "surfaceCreated called");


    protected void onDestroy() {
        // TODO Auto-generated method stub
        if (mMediaPlayer != null) {
            mMediaPlayer = null;



Other Android examples (source code examples)

Here is a short list of links related to this Android MediaPlayerDemo_Video.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.