Android基础-VideoView (几行代码实现本地和网络视频播放)

Android基础-VideoView (几行代码实现本地和网络视频播放)_第1张图片

Android基础-VideoView (几行代码实现本地和网络视频播放)_第2张图片

下面看具体代码:

activity_paly.xml




    

    


PlayActivity



import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.VideoView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PlayActivity extends AppCompatActivity {

    @BindView(R.id.video_local)
    VideoView mVideoLocal;
    @BindView(R.id.video_net)
    VideoView mVideoNet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        ButterKnife.bind(this);
        initLocalVideo();
        initNetVideo();
    }

    //播放本地视频
    private void initLocalVideo() {
        //设置有进度条可以拖动快进
        MediaController localMediaController = new MediaController(this);
        mVideoLocal.setMediaController(localMediaController);
        String uri = ("android.resource://" + getPackageName() + "/" + R.raw.v1);
        mVideoLocal.setVideoURI(Uri.parse(uri));
        mVideoLocal.start();
    }

    //播放网络视频
    private void initNetVideo() {
        //设置有进度条可以拖动快进
        MediaController localMediaController = new MediaController(this);
        mVideoNet.setMediaController(localMediaController);
        String url = "https://flv2.bn.netease.com/videolib1/1811/26/OqJAZ893T/HD/OqJAZ893T-mobile.mp4";
        mVideoNet.setVideoPath(url);
        mVideoNet.start();
    }
}

本地视频无需权限,网络视频需增加网络权限即可:

  

你可能感兴趣的:(Android,VideoView,Android)