Android中自定义VideoView视频播放器

首先我们最简单的实现VideoView视频播放功能

通过VideoView播放视频的步骤:

 1、在界面布局文件中定义VideoView组件,或在程序中创建VideoView组件

 2、调用VideoView的如下两个方法来加载指定的视频

         setVidePath(String path):加载path文件代表的视频

         setVideoURI(Uri uri):加载uri所对应的视频

首先在xml文件中声明一个videoView控件我就不粘贴出来了,java代码如下

  /**
         * 获取资源的绝对路径getAbsolutePath();
         */
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "视频路径";
        /**
         * 本地视频播放
         */
        mVideoView.setVideoPath(path);
      
        // 网络视频播放
        
        mVideoView.setVideoURI(Uri.parse(""));
       
         // 使用MediaController控制视频播放
        
        MediaController mediaController = new MediaController(this);
       
         //设置Videoview与MediaController建立关联
        
        mVideoView.setMediaController(mediaController);
        
         //设置MediaController与VideoView建立关联
         
        mediaController.setMediaPlayer(mVideoView);
    
    }

我们将自定义一个视屏播放器主要功能有,可以播放 暂停 前进 后退 加减音量 增加和降低视屏亮度  

效果如如下:


xml中的布局如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/videoLayout"
        android:layout_width="match_parent"
        android:layout_height="240dp">

        <com.imooc.zhangtao.viewplayertest.FullVideoView
            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="240dp" />

        <include layout="@layout/center_progress"/>

        <LinearLayout
            android:id="@+id/controllerBar"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <SeekBar
                android:id="@+id/pos_seekBar"
                android:layout_width="match_parent"
                android:layout_height="4dp"
                android:thumb="@null"
                android:progressDrawable="@drawable/seekbar_style2"
                android:layout_marginRight="-20dp"
                android:layout_marginLeft="-20dp"
                android:indeterminate="false"
                android:progress="40"
                android:max="100"/>
            
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#101010"
                android:layout_gravity="center_vertical">

                <LinearLayout
                    android:id="@+id/left_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:gravity="center_vertical">

                    <ImageView
                        android:id="@+id/pause_img"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/video_stop_style"
                        android:layout_marginLeft="16dp"/>

                    <TextView
                    
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值