zoukankan      html  css  js  c++  java
  • Android 视频播放器 VideoView 的使用,播放本地视频 和 网络 视频

    1、布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="app.com.myapplication.LocalVideoActivity">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp">
    
            <VideoView
                android:id="@+id/videoView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />
    
        </RelativeLayout>
    
    
    </RelativeLayout>

    2、代码部分

    package app.com.myapplication;
    
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.MediaController;
    import android.widget.Toast;
    import android.widget.VideoView;
    
    public class LocalVideoActivity extends AppCompatActivity {
    
        private VideoView videoView ;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_local_video);
    
            //本地的视频  需要在手机SD卡根目录添加一个 fl1234.mp4 视频
            String videoUrl1 = Environment.getExternalStorageDirectory().getPath()+"/fl1234.mp4" ;
    
            //网络视频
            String videoUrl2 = Utils.videoUrl ;
    
            Uri uri = Uri.parse( videoUrl2 );
    
            videoView = (VideoView)this.findViewById(R.id.videoView );
    
            //设置视频控制器
            videoView.setMediaController(new MediaController(this));
    
            //播放完成回调
            videoView.setOnCompletionListener( new MyPlayerOnCompletionListener());
    
            //设置视频路径
            videoView.setVideoURI(uri);
    
            //开始播放视频
            videoView.start();
        }
    
        class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {
    
            @Override
            public void onCompletion(MediaPlayer mp) {
                Toast.makeText( LocalVideoActivity.this, "播放完成了", Toast.LENGTH_SHORT).show();
            }
        }
    }
    package app.com.myapplication;
    
    /**
     * Created by ${zyj} on 2016/4/19.
     */
    public class Utils {
    
        public static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" ;
    
    }

    3、效果展示

         

           

      

  • 相关阅读:
    ftrace 使用方法
    在Ubuntu custom kernel上裝perf by compile
    [fw]How to use DISM to install a hotfix from within Windows
    Ubuntu下配置了ssh,但是连接很慢
    Fix invisible cursor issue in Ubuntu 13.10
    超強的Linux指令解釋網站《explainshell.com》,學Linux必備!
    python 读取 log日志的编码问题
    随机森林random forest及python实现
    评分卡系列(三):分类学习器的评估
    评分卡系列(二):特征工程
  • 原文地址:https://www.cnblogs.com/zhaoyanjun/p/5412984.html
Copyright © 2011-2022 走看看