• 播放视频


    VideoView播放视频常见方法:

    关键代码:

    public class MainActivity extends AppCompatActivity {
        private Button play,pause,stop;
        private VideoView videoView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            videoView = (VideoView)findViewById(R.id.videoview);
            initVideo();
            // 动态申请授权
            if(ContextCompat.checkSelfPermission(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE) !=
                    PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        1);
            }
        }
    
        protected void myClick(View v){
            switch (v.getId()){
                case R.id.play:
                    if( !videoView.isPlaying() ){
                        videoView.start();
                    }
                    break;
                case R.id.pause:
                    if( videoView.isPlaying() ){
                        videoView.pause();
                    }
                    break;
                case R.id.replay:
                    if( videoView.isPlaying() ){
                        videoView.resume();
                    }
                    break;
                default:
                    break;
            }
        }
    
        private void initVideo(){
            try{
                File file = new File(Environment.getExternalStorageDirectory()+"/Guo","a.mp4");
                videoView.setVideoPath(file.getAbsolutePath());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    所需权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  • 相关阅读:
    我心飞翔
    又见草堂
    有一种爱只能用心去珍藏《转贴》
    人事管理制度(爆笑)
    廊桥放歌
    记住: 惰性,职业生涯的无形杀手
    职场“三无人员”如何突破自己
    升职“流产”,都是谦虚惹的祸(转)
    李嘉诚让员工忠诚的简单办法
    尤未尽意
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6747271.html
Copyright © 2020-2023  润新知