FileDocCategorySizeDatePackage
MovieView.javaAPI DocAndroid 1.5 API9061Wed May 06 22:42:42 BST 2009com.android.camera

MovieView

public class MovieView extends android.app.Activity implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener

Fields Summary
private static final String
TAG
private static final String
SERVICECMD
private static final String
CMDNAME
private static final String
CMDPAUSE
private android.widget.VideoView
mVideoView
private android.view.View
mProgressView
private boolean
mFinishOnCompletion
private android.net.Uri
mUri
private int
mPositionWhenPaused
private boolean
mWasPlayingWhenPaused
android.os.Handler
mHandler
Runnable
mPlayingChecker
Constructors Summary
public MovieView()


     
    
    
Methods Summary
private java.lang.IntegergetBookmark()

        if (!uriSupportsBookmarks(mUri)) {
            return null;
        }
        
        String[] projection = new String[]{Video.VideoColumns.DURATION,
                Video.VideoColumns.BOOKMARK};
        try {
            Cursor cursor = getContentResolver().query(mUri, projection, null, null, null);
            if (cursor != null) {
                try {
                    if ( cursor.moveToFirst() ) {
                        int duration = getCursorInteger(cursor, 0);
                        int bookmark = getCursorInteger(cursor, 1);
                        final int ONE_MINUTE = 60 * 1000;
                        final int TWO_MINUTES = 2 * ONE_MINUTE;
                        final int FIVE_MINUTES = 5 * ONE_MINUTE;
                        if ((bookmark < TWO_MINUTES)
                                || (duration < FIVE_MINUTES)
                                || (bookmark > (duration - ONE_MINUTE))) {
                            return null;
                        }

                        return new Integer(bookmark);
                    }
                } finally {
                    cursor.close();
                }
            }
        } catch (SQLiteException e) {
            // ignore
        }

        return null;
    
private intgetCursorInteger(android.database.Cursor cursor, int index)

        try {
            return cursor.getInt(index);
        } catch (SQLiteException e) {
            return 0;
        } catch (NumberFormatException e) {
            return 0;
        }

    
public voidonCompletion(android.media.MediaPlayer mp)

        if (mFinishOnCompletion) {
            finish();
        }
    
public voidonCreate(android.os.Bundle icicle)

        super.onCreate(icicle);

        setContentView(R.layout.movie_view);

        mVideoView = (VideoView) findViewById(R.id.surface_view);
        mProgressView = findViewById(R.id.progress_indicator);
        Intent intent = getIntent();
        if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
            int orientation = intent.getIntExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
                    ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
            if (orientation != getRequestedOrientation()) {
                setRequestedOrientation(orientation);
            }
        }
        mFinishOnCompletion = intent.getBooleanExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
        mUri = intent.getData();

        // For streams that we expect to be slow to start up, show a
        // progress spinner until playback starts.
        String scheme = mUri.getScheme();
        if ("http".equalsIgnoreCase(scheme) ||
                "rtsp".equalsIgnoreCase(scheme)) {
            mHandler.postDelayed(mPlayingChecker, 250);
        } else {
            mProgressView.setVisibility(View.GONE);
        }

        mVideoView.setOnErrorListener(this);
        mVideoView.setOnCompletionListener(this);
        mVideoView.setVideoURI(mUri);
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.requestFocus(); // make the video view handle keys for seeking and pausing

        Intent i = new Intent(SERVICECMD);
        i.putExtra(CMDNAME, CMDPAUSE);
        sendBroadcast(i);
        {
            final Integer bookmark = getBookmark();
            if (bookmark != null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(R.string.resume_playing_title);
                builder.setMessage(String.format(
                        getString(R.string.resume_playing_message),
                        MenuHelper.formatDuration(this, bookmark)));
                builder.setOnCancelListener(new OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        finish();
                    }});
                builder.setPositiveButton(R.string.resume_playing_resume,
                        new OnClickListener(){
                    public void onClick(DialogInterface dialog, int which) {
                        mVideoView.seekTo(bookmark);
                        mVideoView.start();
                    }});
                builder.setNegativeButton(R.string.resume_playing_restart, new OnClickListener(){
                    public void onClick(DialogInterface dialog, int which) {
                        mVideoView.start();
                    }});
                builder.show();
            } else {
                mVideoView.start();
            }
        }
    
public booleanonError(android.media.MediaPlayer player, int arg1, int arg2)


            
        mHandler.removeCallbacksAndMessages(null);
        mProgressView.setVisibility(View.GONE);
        return false;
    
public voidonPause()

        mHandler.removeCallbacksAndMessages(null);
        setBookmark(mVideoView.getCurrentPosition());

        mPositionWhenPaused = mVideoView.getCurrentPosition();
        mWasPlayingWhenPaused = mVideoView.isPlaying();
        mVideoView.stopPlayback();

        super.onPause();
    
public voidonResume()

        if (mPositionWhenPaused >= 0) {
            mVideoView.setVideoURI(mUri);
            mVideoView.seekTo(mPositionWhenPaused);
            if (mWasPlayingWhenPaused) {
                mVideoView.start();
            }
        }

        super.onResume();
    
private voidsetBookmark(int bookmark)

        if (!uriSupportsBookmarks(mUri)) {
            return;
        }
        
        ContentValues values = new ContentValues();
        values.put(Video.VideoColumns.BOOKMARK, Integer.toString(bookmark));
        try {
            getContentResolver().update(mUri, values, null, null);
        } catch (SecurityException ex) {
            // Ignore, can happen if we try to set the bookmark on a read-only resource
            // such as a video attached to GMail.
        } catch (SQLiteException e) {
            // ignore. can happen if the content doesn't support a bookmark column.
        } catch (UnsupportedOperationException e) {
            // ignore. can happen if the external volume is already detached.
        }
    
private static booleanuriSupportsBookmarks(android.net.Uri uri)

        String scheme = uri.getScheme();
        String authority = uri.getAuthority();
        return ("content".equalsIgnoreCase(scheme)
                && MediaStore.AUTHORITY.equalsIgnoreCase(authority));