FileDocCategorySizeDatePackage
MediaPlayerDemo_Video.javaAPI DocAndroid 1.5 API5766Wed May 06 22:41:08 BST 2009com.example.android.apis.media

MediaPlayerDemo_Video

public class MediaPlayerDemo_Video extends android.app.Activity implements android.media.MediaPlayer.OnCompletionListener, SurfaceHolder.Callback, android.media.MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnPreparedListener

Fields Summary
private static final String
TAG
private int
mVideoWidth
private int
mVideoHeight
private android.media.MediaPlayer
mMediaPlayer
private android.view.SurfaceView
mPreview
private android.view.SurfaceHolder
holder
private String
path
private android.os.Bundle
extras
private static final String
MEDIA
private static final int
LOCAL_AUDIO
private static final int
STREAM_AUDIO
private static final int
RESOURCES_AUDIO
private static final int
LOCAL_VIDEO
private static final int
STREAM_VIDEO
Constructors Summary
Methods Summary
public voidonBufferingUpdate(android.media.MediaPlayer arg0, int percent)

        Log.d(TAG, "onBufferingUpdate percent:" + percent);

    
public voidonCompletion(android.media.MediaPlayer arg0)

        Log.d(TAG, "onCompletion called");
    
public voidonCreate(android.os.Bundle icicle)
Called when the activity is first created.


                 
        
        super.onCreate(icicle);
        setContentView(R.layout.mediaplayer_2);
        mPreview = (SurfaceView) findViewById(R.id.surface);
        holder = mPreview.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        extras = getIntent().getExtras();

    
protected voidonDestroy()

        super.onDestroy();
        // TODO Auto-generated method stub
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }

    
public voidonPrepared(android.media.MediaPlayer mediaplayer)

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

    
private voidplayVideo(java.lang.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.
                        Toast
                                .makeText(
                                        MediaPlayerDemo_Video.this,
                                        "Please edit MediaPlayerDemo_Video Activity, "
                                                + "and set the path variable to your media file path."
                                                + " Your media file must be stored on sdcard.",
                                        Toast.LENGTH_LONG).show();

                    }
                    break;
                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.
                        Toast
                                .makeText(
                                        MediaPlayerDemo_Video.this,
                                        "Please edit MediaPlayerDemo_Video Activity,"
                                                + " and set the path variable to your media file URL.",
                                        Toast.LENGTH_LONG).show();

                    }

                    break;


            }

            // Create a new media player and set the listeners
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepare();
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }
    
public voidsurfaceChanged(android.view.SurfaceHolder surfaceholder, int i, int j, int k)

        Log.d(TAG, "surfaceChanged called");

    
public voidsurfaceCreated(android.view.SurfaceHolder holder)

        // TODO Auto-generated method stub
        Log.d(TAG, "surfaceCreated called");
        playVideo(extras.getInt(MEDIA));


    
public voidsurfaceDestroyed(android.view.SurfaceHolder surfaceholder)

        Log.d(TAG, "surfaceDestroyed called");