#Android使用VideoView播放本地和网络视频

1.播放本地视频

XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    tools:context=".MainActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:paddingTop="10dp">

        <Button
            android:id="@+id/start_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始"
            />
        <Button
            android:id="@+id/pause_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暂停"
            />
        <Button
            android:id="@+id/stop_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止"
            />
    </LinearLayout>

</LinearLayout>
Java代码
public class MainActivity extends AppCompatActivity {

    private VideoView videoView;
    private MediaController mediaController;
    private Button start_bt,pauze_bt,stop_bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        setListener();
    }

    /**
     * 该函数用于初始化组件
     */
    void initUI(){
        videoView = (VideoView)findViewById(R.id.videoView);
        start_bt = (Button) findViewById(R.id.start_bt);
        pauze_bt = (Button) findViewById(R.id.pause_bt);
        stop_bt = (Button) findViewById(R.id.stop_bt);
    }

    /**
     * 该函数用于设置监听
     */
    void setListener(){
        start_bt.setOnClickListener(listener);
        pauze_bt.setOnClickListener(listener);
        stop_bt.setOnClickListener(listener);
    }

    /**
     * 该函数用于设置视频播放开始
     */
    void startPlay(){
        try {
            mediaController = new MediaController(MainActivity.this);
            String uri ="android.resource://" + getPackageName() + "/" + R.raw.one;
            videoView.setVideoURI(Uri.parse(uri));
            videoView.setMediaController(mediaController);
            mediaController.setMediaPlayer(videoView);
            videoView.requestFocus();
            videoView.start();
        }catch (Exception e){
            System.out.println("播放失败");
            Toast.makeText(MainActivity.this,"播放失败!",Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 该函数用于暂停视频播放
     */
    void pausePlay(){
        videoView.pause();
    }

    /**
     * 该函数用于设置视频播放停止
     */
    void stopPlay(){
        videoView.stopPlayback();
    }

    /**
     * 创建一个监听内部类
     */
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.start_bt:
                    startPlay();            //视频播放
                    break;
                case R.id.pause_bt:
                    pausePlay();            //视频暂停
                    break;
                case R.id.stop_bt:
                    stopPlay();             //视频停止
                    break;
            }
        }
    };
}

把one.mp4这个视频保存到res文件下的raw(若没有这个文件夹,则手动创建它)。如果视频文件是存储在sd下的,就需要以下权限

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



2.播放网络视频

XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    tools:context=".MainActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:paddingTop="10dp">

        <Button
            android:id="@+id/start_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始"
            />
        <Button
            android:id="@+id/pause_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暂停"
            />
        <Button
            android:id="@+id/stop_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止"
            />
    </LinearLayout>

</LinearLayout>
Java代码
public class MainActivity extends AppCompatActivity {

    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        startPlay();

    }

    /**
     * 初始化组件
     */
    void initUI(){
        videoView = (VideoView)findViewById(R.id.videoView);
    }


    void startPlay(){
        //设置视频控制器
        videoView.setMediaController(new MediaController(this));

        //播放完成回调
        videoView.setOnCompletionListener( new MyPlayerOnCompletionListener());

        //设置网络视频路径
      //String uri = "http://127.0.0.1:8080/test/video.mp4";
       String uristr = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
       Uri uri = Uri.parse(uristr);
        videoView.setVideoURI(uri);

        videoView.requestFocus();
        videoView.start();
    }

    /**
     * 播放回调类
     */
    class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener{
        @Override
        public void onCompletion(MediaPlayer mp) {
            Toast.makeText(MainActivity.this,"播放结束!",Toast.LENGTH_SHORT).show();
        }
    }

}

不要忘了注册网络权限

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

播放网络视频前会有一段黑屏时间,以后有时间再写一篇解决黑屏的方法。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值