android之播放视频(SDCard)

播放视频(SDCard)
VideoView+MediaController
在布局中只需要一个VideoView的控件即可。

Activity_mian.xml




    

AndroidManifest,xml




MainActivity.java
public class MainActivity extends AppCompatActivity {
    // permission
    private static final String[] PERMISSION_STORAGE = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };
    private static final int REQUEST_EXTEANAL_STORAGE = 9527;

    private VideoView videoView;
    private MediaController mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int permission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, PERMISSION_STORAGE, REQUEST_EXTEANAL_STORAGE);
        } else {

            sdCardReady();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_EXTEANAL_STORAGE) {
            sdCardReady();
        }
    }

    private void sdCardReady() {

        videoView = findViewById(R.id.videoView);
        mediaController = new MediaController(MainActivity.this);
        String strFilePath = SDCardUtils.getSDCardPublicDir(Environment.DIRECTORY_MOVIES) + File.separator + "ha.mp4";//
        videoView.setVideoPath(strFilePath);
        videoView.setMediaController(mediaController);
        mediaController.setMediaPlayer(videoView);
    }
}

你可能感兴趣的:(android之播放视频(SDCard))