• 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;
        }
    
        
    
    }
  • 相关阅读:
    CI框架源码学习笔记1——index.php
    angular.js的post数据方式
    腾讯2018年9月秋招前端笔试题--编程题
    有赞2018年9月秋招一面
    2018阿里秋招笔试编程题
    css3 flex布局
    tomcat启动后,无法访问,报404
    2018网易前端实习面试总结
    2018网易前端实习笔试编程题总结
    set用法
  • 原文地址:https://www.cnblogs.com/slowalker/p/3407488.html
Copyright © 2020-2023  润新知