• andriod VideoView


    package com.example.yanlei.myyk;
    
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.MediaController;
    import android.widget.VideoView;
    
    import java.io.File;
    
    
    public class MainActivity extends AppCompatActivity {
    
        private VideoView videoView;
        MediaController mediaco;
        private Uri mUri;
    
        public static final String TAG = "VideoPlayer";
    
        private int mPositionWhenPaused = -1;
    
        private MediaController mMediaController;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            videoView = (VideoView) findViewById(R.id.videoview);
            //文件路径
    
            mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/videoviewdemo.mp4");
    
            //Create media controller
            mediaco = new MediaController(this);
            //设置MediaController
            videoView.setMediaController(mediaco);
        }
    
        //监听MediaPlayer上报的错误信息
    
    
        public boolean onError(MediaPlayer mp, int what, int extra) {
            // TODO Auto-generated method stub
            return false;
        }
    
    
    //Video播完的时候得到通知
    
    
        public void onCompletion(MediaPlayer mp) {
            this.finish();
        }
    
    
        //开始
        public void onStart() {
            // Play Video
            videoView.setVideoURI(mUri);
            videoView.start();
    
            super.onStart();
        }
    
    
        //暂停
    
        public void onPause() {
            // Stop video when the activity is pause.
            mPositionWhenPaused = videoView.getCurrentPosition();
            videoView.stopPlayback();
            Log.d(TAG, "OnStop: mPositionWhenPaused = " + mPositionWhenPaused);
            Log.d(TAG, "OnStop: getDuration  = " + videoView.getDuration());
    
            super.onPause();
        }
    
    
        public void onResume() {
            // Resume video player
            if (mPositionWhenPaused >= 0) {
                videoView.seekTo(mPositionWhenPaused);
                mPositionWhenPaused = -1;
            }
    
            super.onResume();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }

    权限

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    或者
    package com.example.yanlei.myyk;
    
    import android.os.Bundle;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.MediaController;
    import android.widget.VideoView;
    
    import java.io.File;
    
    
    public class MainActivity extends AppCompatActivity {
    
        private VideoView video1;
        MediaController  mediaco;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            video1=(VideoView)findViewById(R.id.videoview);
            mediaco=new MediaController(this);
            File file=new File(Environment.getExternalStorageDirectory() + "/videoviewdemo.mp4");
            if(file.exists()){
                //VideoView与MediaController进行关联
                video1.setVideoPath(file.getAbsolutePath());
                video1.setMediaController(mediaco);
                mediaco.setMediaPlayer(video1);
                //让VideiView获取焦点
                video1.requestFocus();
            }
    
        }
    
    
    
    }
  • 相关阅读:
    [GoogleInterview]连续子序列问题
    [Codeforces #615 div3]1294E Obtain a Permutation
    重识线段树——Let's start with the start.
    [易语言][ExDui][Tutorial]1.NameSelector
    [LuoguP1025][数据加强]数的划分
    [易语言][ExDui][Tutorial]0.Hello,world!
    Hello,cnblog!
    棋牌覆盖问题(分治)
    UVA 11732题解
    LA 3942Remember the Word
  • 原文地址:https://www.cnblogs.com/gisoracle/p/5252627.html
Copyright © 2020-2023  润新知