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

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

下面看具体代码:

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

    <VideoView
        android:id="@+id/video_local"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <VideoView
        android:id="@+id/video_net"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>
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();
    }
}

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

  <uses-permission android:name="android.permission.INTERNET" />