zoukankan      html  css  js  c++  java
  • 【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与设计模式之------中介者模式Mediator
    大熊君说说JS与设计模式之------命令模式Command
    读书笔记:《HTML5开发手册》--HTML5新的结构元素
    读书笔记:JavaScript DOM 编程艺术(第二版)
    记一次debug记录:Uncaught SyntaxError: Unexpected token ILLEGAL
    总结:Mac前端开发环境的搭建(配置)
    js学习笔记:操作iframe
    js学习笔记:webpack基础入门(一)
    js学习笔记:webpack基础入门(一)
    微信日志开发之人脸识别开发
  • 原文地址:https://www.cnblogs.com/CoderTian/p/6204303.html
Copyright © 2011-2022 走看看