首页 > 系统 > Android > 正文

android视频播放器

2019-11-07 23:12:13
字体:
来源:转载
供稿:网友

这里介绍两种方式:

1.简单的视频播放器

2.自定义的视频播放器

1.简单的视频播放器(系统自带)直接上代码

public class MainActivity extends AppCompatActivity {    PRivate VideoView mVideoView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mVideoView = (VideoView) findViewById(R.id.videoView);        //寻找手机sd卡中的视频        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"文件名字";        //网络        http://.............................服务器播放地址        /**         * 本地播放         *         * */         mVideoView.setVideoPath("");        /**         * 网络播放         * */        mVideoView.setVideoURI(Uri.parse(""));        /**         * 使用MediaController控制视频播放         * */        MediaController mediaController = new MediaController(this);        /*        * 设置videoVIew和mediaController建立联系        * **/        mVideoView.setMediaController(mediaController);         /*        * 设置mediaController和videoVIew建立联系        * **/        mediaController.setMediaPlayer(mVideoView);    }/**

布局就是一个videoView

*/

二.自定义的播放器(代码没有加音量,亮度调节)

布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <RelativeLayout        android:id="@+id/videoViewLayout"        android:layout_width="match_parent"        android:layout_height="240dp">        <VideoView            android:layout_width="match_parent"            android:layout_height="240dp"            android:id="@+id/videoView"/>                <LinearLayout            android:layout_width="match_parent"            android:layout_height="50dp"            android:layout_alignParentBottom="true"            android:orientation="vertical">            <SeekBar                android:id="@+id/play_seek"                android:layout_width="match_parent"                android:layout_height="5dp"                android:layout_marginLeft="-20dp"                android:layout_marginRight="-20dp"                android:progress="20"                android:max="100"                android:indeterminate="false"                android:progressDrawable="@drawable/seekbar_style2"                android:thumb="@null"                />            <RelativeLayout                android:layout_width="match_parent"                android:layout_height="match_parent"                android:background="#1a1a1a"                android: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/press_img"                        android:layout_marginLeft="16dp"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:src="zhe li shi zhanting bofang buju"                        />                    <TextView                        android:id="@+id/time_current_tv"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text="00:00:00"                        android:textColor="#ffffff"                        android:textSize="14dp"                        android:layout_marginLeft="32dp"                        />                    <TextView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text="/"                        android:textColor="#4c4c4c"                        android:textSize="14dp"                        android:layout_marginLeft="5dp"                        />                     //视频总时间                    <TextView                        android:id="@+id/time_total_tv"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text="00:00:00"                        android:textColor="#4c4c4c"                        android:textSize="14dp"                        android:layout_marginLeft="5dp"                        />                </LinearLayout>                <LinearLayout                    android:gravity="center_vertical|right"                    android:layout_width="10dp"                    android:layout_height="match_parent"                    android:layout_alignParentRight="true"                    android:orientation="horizontal"                    android:layout_toRightOf="@+id/left_layout"                    >                    //控制音量                    <ImageView                        android:visibility="gone"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:src="tu pian"                        />                    <SeekBar                        android:id="@+id/volume_seek"                        android:visibility="gone"                        android:indeterminate="false"                        android:layout_width="100dp"                        android:layout_height="5dp"                        android:thumb="@null"                        android:progress="20"                        android:max="100"                        android:progressDrawable="@drawable/seeck_style"/>                    <view                        android:layout_width="1dp"                        android:layout_height="match_parent"                        android:layout_marginTop="5dp"                        android:layout_marginBottom="5dp"                        android:layout_marginLeft="32dp"                        android:background="#1e1e1e">                    </view>                    //qie huan quan ping                    <ImageView                        android:src="tu pian"                        android:id="@+id/screen_img"                        android:layout_marginLeft="16dp"                        android:layout_marginRight="16dp"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content" />                </LinearLayout>            </RelativeLayout>        </LinearLayout>            </RelativeLayout></RelativeLayout>

资源文件  (进度条的颜色)

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id ="@android:id/backgroud">        <shape>            <solid android:color="#707070"></solid>            <size android:height="5dp"></size>        </shape>    </item>    <item android:id="@android:id/progress">        <clip>            <shape>                <solid android:color="#b94310"></solid>                <size android:height="5dp"></size>            </shape>        </clip>    </item></layer-list>

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@android:id/backgroud">        <shape>            <solid android:color="#101010"></solid>        </shape>    </item>    <item android:id="@android:id/progress">        <clip>            <shape>                <solid android:color="#ffb97422"></solid>            </shape>        </clip>    </item></layer-list>

代码:

public class ZiDingYiViewActivity  extends Activity{    private VideoView mVideoView;    private LinearLayout controllerlayout;    private ImageView pause_image,srceen_img;    private TextView current_text_tv,time_total_tv;    private SeekBar play_seek,volume_seek;    public static  final int UPDATE_UI = 1;    public int width,height;    public RelativeLayout allLayout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.zidingyiview);        initUI();        setPlayerEvent();        //寻找手机sd卡中的视频        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"文件名字";        mVideoView.start();        UiHandler.sendEmptyMessage(UPDATE_UI);        /**         * 本地播放         *         * */        mVideoView.setVideoPath(path);    }    /**     * 初始化布局     * */    private void initUI(){        mVideoView = (VideoView) findViewById(R.id.videoView);        controllerlayout =(LinearLayout) findViewById(R.id.left_layout);        pause_image = (ImageView)findViewById(R.id.press_img);        srceen_img = (ImageView)findViewById(R.id.screen_img);        current_text_tv = (TextView)findViewById(R.id.time_current_tv);        time_total_tv = (TextView)findViewById(R.id.time_total_tv);        play_seek = (SeekBar)findViewById(R.id.play_seek);        volume_seek = (SeekBar)findViewById(R.id.volume_seek);        allLayout = (RelativeLayout)findViewById(R.id.videoViewLayout);        width = getResources().getDisplayMetrics().widthPixels;        height = getResources().getDisplayMetrics().heightPixels;    }    private void setmVideoViewScale(int width,int height){        ViewGroup.LayoutParams layoutParams = mVideoView.getLayoutParams();        layoutParams.width = width;        layoutParams.height = height;        allLayout.setLayoutParams(layoutParams);        ViewGroup.LayoutParams layoutParams1 = mVideoView.getLayoutParams();        layoutParams1.width = width;        layoutParams1.height = height;        allLayout.setLayoutParams(layoutParams1);    }    /*    * 时间的转换    * **/    private void updateTextViewWithTimeFormat(TextView textView ,int millisecond){        int second = millisecond/1000;        int hh = second/3600;        int mm = second%3600/60;        int ss = second%60;        String str = null;        if(hh!=0){            str = String.format("%02d:%02d:%02d",hh,mm,ss);        }        else        {            str = String.format("%02d:%02d",mm,ss);        }        textView.setText(str);    }    /*    * 更新ui    * **/    private Handler UiHandler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            if(msg.what==UPDATE_UI) {                //获取当前时间                int currentPosition = mVideoView.getCurrentPosition();                //获取总时间                int totaldruation = mVideoView.getDuration();                //格式化视频播放时间                updateTextViewWithTimeFormat(current_text_tv, currentPosition);                updateTextViewWithTimeFormat(time_total_tv, totaldruation);                play_seek.setMax(totaldruation);                play_seek.setProgress(currentPosition);                //自己调用自己                UiHandler.sendEmptyMessageDelayed(UPDATE_UI, 500);            }        }    };    /**     * 控制播放器的暂停和播放     * */    private void setPlayerEvent(){        pause_image.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //判断是否播放状态                if(mVideoView.isPlaying()){                    pause_image.setImageResource(R.drawable.seeck_style);                    //暂停播放                    mVideoView.pause();                    UiHandler.removeMessages(UPDATE_UI);                }                else {                    pause_image.setImageResource(R.drawable.seeck_style);                    //开始播放                    mVideoView.start();                    UiHandler.sendEmptyMessage(UPDATE_UI);                }            }        });        play_seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {            @Override            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {                    updateTextViewWithTimeFormat(current_text_tv,progress);            }            @Override            public void onStartTrackingTouch(SeekBar seekBar) {                    UiHandler.removeMessages(UPDATE_UI);            }            @Override            public void onStopTrackingTouch(SeekBar seekBar) {                    int progress = seekBar.getProgress();                //另视频的播放进度遵循seeckbar停止拖动的这一刻的进度                mVideoView.seekTo(progress);                UiHandler.sendEmptyMessage(UPDATE_UI);            }        });    }    @Override    protected void onPause() {        super.onPause();        UiHandler.removeMessages(UPDATE_UI);    }    /**     * 切换横屏让其全屏,监听屏幕方向改变     * */    @Override    public void onConfigurationChanged(Configuration newConfig) {        super.onConfigurationChanged(newConfig);        //当屏幕为横屏时        if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE){            setmVideoViewScale(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);        }        //当屏幕是竖屏时        else{            setmVideoViewScale(ViewGroup.LayoutParams.MATCH_PARENT,gong ju lei dp2px(height));        }    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表