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
    评论
如果您想要实现自定义视频播放进度条和暂停/播放按钮,可以按照以下步骤进行: 1. 在布局文件添加 VideoView、SeekBar 和暂停/播放按钮: ``` <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/play_pause_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pause" /> ``` 2. 在 Activity 获取 VideoView、SeekBar 和暂停/播放按钮的引用: ``` VideoView videoView = findViewById(R.id.video_view); SeekBar seekBar = findViewById(R.id.seek_bar); Button playPauseButton = findViewById(R.id.play_pause_button); ``` 3. 设置 VideoView 的路径并开始播放: ``` videoView.setVideoPath("path/to/video.mp4"); videoView.start(); ``` 4. 为 VideoView 添加一个 OnPreparedListener,当视频准备好时,设置 SeekBar 的最大值和添加一个定时器来更新 SeekBar 的进度: ``` videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { seekBar.setMax(videoView.getDuration()); final Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { seekBar.setProgress(videoView.getCurrentPosition()); handler.postDelayed(this, 1000); } }; handler.postDelayed(runnable, 1000); } }); ``` 5. 为 SeekBar 添加一个 OnSeekBarChangeListener,当拖动 SeekBar 时,改变视频播放的位置: ``` seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { videoView.seekTo(progress); } } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); ``` 6. 为暂停/播放按钮添加一个 OnClickListener,当点击按钮时,暂停或继续播放视频: ``` playPauseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (videoView.isPlaying()) { videoView.pause(); playPauseButton.setText("Play"); } else { videoView.start(); playPauseButton.setText("Pause"); } } }); ``` 7. 为 VideoView 添加一个 OnCompletionListener,当视频播放完成时,将暂停/播放按钮的文本设置为“Play”: ``` videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { playPauseButton.setText("Play"); } }); ``` 以上就是实现自定义视频播放进度条和暂停/播放按钮的方法。您可以根据您的需求自定义 SeekBar 的样式和暂停/播放按钮的图标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值