Android:VideoView

VideoView Overview

  A VideoView is mainly used to display a video file. it can load images from various sources (such as resources or centent providers), takes care of computing its measurement

from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

  The following is a little Demo to show:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >
    
        <VideoView 
            android:id="@+id/video_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="100dip"
            />
</LinearLayout>

MainActivity.java

package com.slowalker.videoviewdemo;

import java.io.File;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.VideoView;

public class MainActivity extends Activity implements MediaPlayer.OnErrorListener,MediaPlayer.OnCompletionListener {
    
    private static final String TAG = MainActivity.class.getSimpleName();
    private static final boolean DEBUG = true;
    
    private VideoView mVideoView;
    private Uri mUri;
    private int mPausePostion = -1;
    private MediaController mMediaController;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DEBUG) {
            Log.d(TAG, "onCreate");
        }
        
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        mVideoView = (VideoView) findViewById(R.id.video_view);
        mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/不能说的秘密.mp4");
        String path = Environment.getExternalStorageDirectory() + "/不能说的秘密.mp4";
        if (DEBUG && new File(path).exists() == false) {
            Toast.makeText(this, "File Not Exists!", Toast.LENGTH_SHORT).show();
        }
        mMediaController = new MediaController(this);
        mVideoView.setMediaController(mMediaController);
    }

    
    
    
    @Override
    protected void onStart() {
        super.onStart();
        if (DEBUG) {
            Log.d(TAG, "onStart");
        }
        mVideoView.setVideoURI(mUri);
        if (DEBUG) {
            Log.d(TAG, "start to play video.");
        }
        mVideoView.start();
    }




    @Override
    protected void onResume() {
        super.onResume();
        if (DEBUG) {
            Log.d(TAG, "onResume");
        }
        if (mPausePostion > 0) {
            mVideoView.seekTo(mPausePostion);
            mPausePostion = -1;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (DEBUG) {
            Log.d(TAG, "onPause");
        }
        mPausePostion = mVideoView.getCurrentPosition();
        if (DEBUG) {
            Log.d(TAG, "video play stopped.");
        }
        mVideoView.stopPlayback();
    }




    @Override
    public void onCompletion(MediaPlayer mp) {
        if (DEBUG) {
            Log.d(TAG, "finish playing.");
        }
        this.finish();
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        if (DEBUG) {
            Log.d(TAG, "error occurs when playing.");
        }
        return false;
    }

    

}

 

posted @ 2013-11-04 21:42  slowalker  阅读(273)  评论(0编辑  收藏  举报