安卓使用VideoView播放raw目录的视频和sd卡的视频


效果

安卓使用VideoView播放raw目录的视频和sd卡的视频_第1张图片

代码

package com.javen.devicemange.CrazyOne.media;

import android.Manifest;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

import com.javen.devicemange.R;

import java.io.File;

/**
 * Created by Administrator on 2017/2/27 0027.
 * 使用VideoView播放raw目录的视频和sd卡的视频
 */

public class VideoViewTest extends AppCompatActivity implements View.OnClickListener {
    private VideoView video;
    private Button play;
    private MediaController mediaController;
    private Button raw;
    private Button assets;
    private AssetManager assetManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videoviewtest);
        initView();
        requestPermission();

    }

    private void requestPermission() {
        String[] needPermission = {
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
        };
        ActivityCompat.requestPermissions(this, needPermission, 1);
    }

    private void initView() {
        video = (VideoView) findViewById(R.id.video);
        play = (Button) findViewById(R.id.play);
        play.setOnClickListener(this);
        raw = (Button) findViewById(R.id.raw);
        raw.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play:
                playVideoMp4();
                break;
            case R.id.raw:
                playVideoRaw();
                break;
        }
    }

    /**
     * 播放raw的小视频
     */
    private void playVideoRaw() {
        mediaController = new MediaController(this);
        //获取raw.mp4uri地址
        String uri = "android.resource://" + getPackageName() + "/" + R.raw.raw;
        video.setVideoURI(Uri.parse(uri));
        //videomediaController建立关联
        video.setMediaController(mediaController);
        mediaController.setMediaPlayer(video);
        //video获取焦点
        video.requestFocus();
        //监听播放完成,
        video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //重新开始播放
                video.start();
            }
        });
    }

    /**
     * 播放sd卡的小视频
     */
    private void playVideoMp4() {
        mediaController = new MediaController(this);
        //获取sd卡根目录下的test.mp4文件
        File file = new File(Environment.getExternalStorageDirectory().toString() + "/test.mp4");
        if (file.exists()) {
            video.setVideoPath(file.getAbsolutePath());
            //videomediaController建立关联
            video.setMediaController(mediaController);
            mediaController.setMediaPlayer(video);
            //video获取焦点
            video.requestFocus();
        }
    }
}

raw目录

安卓使用VideoView播放raw目录的视频和sd卡的视频_第2张图片

sd卡目录

安卓使用VideoView播放raw目录的视频和sd卡的视频_第3张图片

布局xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    

AndroidManifest.xml

android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:name="android.permission.READ_EXTERNAL_STORAGE"/>

。。。

你可能感兴趣的:(安卓Media)