• 【Android 多媒体应用】使用 VideoView 播放视频


    1.MainActivity.java

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.MediaController;
    import android.widget.VideoView;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private VideoView videoView;
        private Button btn_start;
        private Button btn_pause;
        private Button btn_stop;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView() {
            videoView = (VideoView) findViewById(R.id.videoView);
            btn_start = (Button) findViewById(R.id.btn_start);
            btn_pause = (Button) findViewById(R.id.btn_pause);
            btn_stop = (Button) findViewById(R.id.btn_stop);
    
            btn_start.setOnClickListener(this);
            btn_pause.setOnClickListener(this);
            btn_stop.setOnClickListener(this);
    
            //根据文件路径播放
            videoView.setVideoPath("/sdcard/TopGirl.mp4");
            videoView.setMediaController(new MediaController(this));
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_start:
                    videoView.start();
                    break;
                case R.id.btn_pause:
                    videoView.pause();
                    break;
                case R.id.btn_stop:
                    videoView.stopPlayback();
                    break;
            }
        }
    }

    2.activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="5dp">
    
        <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">
    
            <Button
                android:id="@+id/btn_start"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="开始" />
    
            <Button
                android:id="@+id/btn_pause"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="暂停 " />
    
            <Button
                android:id="@+id/btn_stop"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="停止" />
        </LinearLayout>
    
    </LinearLayout>
  • 相关阅读:
    原生,js,自执行函数的三种写法,及特性
    js延迟加载的六种方式
    antd table 合并行的方法
    css 常用样式antd design 中table,表格省略号处理的问题
    axios 中文文档
    js ,几张遍历,循环的方法
    javascript怎么保留两位小数
    Category(十九)
    Protocol(协议)(二十)
    Extension延展(十八)
  • 原文地址:https://www.cnblogs.com/CoderTian/p/6204303.html
Copyright © 2020-2023  润新知