VideoView 播放本地视频

  1. /**
  2.  * 会根据视频尺寸自动缩放
  3.  * 自己对VideoView设置的宽高基本不起任何作用
  4.  */
  5. public class VideoViewDemo extends Activity implements io.vov.vitamio.MediaPlayer.OnPreparedListener, OnClickListener {
  6.     private EditText et_path;
  7.     private Button bt_play;
  8.     private VideoView mVideoView;
  9.     @Override
  10.     public void onCreate(Bundle icicle) {
  11.         super.onCreate(icicle);
  12.         Vitamio.isInitialized(this);
  13.         setContentView(R.layout.videoview);
  14.         et_path = (EditText) findViewById(R.id.et_path);
  15.         bt_play = (Button) findViewById(R.id.bt_play);
  16.         mVideoView = (VideoView) findViewById(R.id.surface_view);
  17.         mVideoView.setMediaController(new MediaController(this));
  18.         mVideoView.setOnPreparedListener(this);
  19.         bt_play.setOnClickListener(this);
  20.     }
  21.     @Override
  22.     public void onPrepared(MediaPlayer mp) {
  23.         mp.setPlaybackSpeed(1.0f);
  24.     }
  25.     @Override
  26.     public void onClick(View v) {
  27.         String filepath = et_path.getText().toString().trim();
  28.         mVideoView.setVideoPath(Environment.getExternalStorageDirectory().getAbsolutePath() + filepath);
  29.         mVideoView.requestFocus();
  30.         mVideoView.start();
  31.     }
  32. }

  33. <?xml version="1.0" encoding="utf-8"?>
  34. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  35.     android:layout_width="match_parent"
  36.     android:layout_height="match_parent"
  37.     android:orientation="vertical" >
  38.     <io.vov.vitamio.widget.VideoView
  39.         android:id="@+id/surface_view"
  40.         android:layout_width="300dp"
  41.         android:layout_height="300dp"
  42.         android:layout_gravity="center_horizontal" />
  43.     <EditText
  44.         android:id="@+id/et_path"
  45.         android:layout_width="fill_parent"
  46.         android:layout_height="wrap_content"
  47.         android:text="/a.rmvb" />
  48.     <Button
  49.         android:id="@+id/bt_play"
  50.         android:layout_width="match_parent"
  51.         android:layout_height="wrap_content"
  52.         android:text="播放本地多媒体" />
  53. </LinearLayout>
  54.  

VideoView 播放网络视频、控制缩放类型

  1. /**
  2.  * 字幕,调整视频缩放类型
  3.  * @author 白乾涛
  4.  */
  5. public class VideoViewSubtitle extends Activity implements OnPreparedListener, OnClickListener, OnTimedTextListener {
  6.     //HTTP,无效
  7.     //path=  "http://www.modrails.com/videos/passenger_nginx.mov";
  8.     //path=  "http://wsmp32.bbc.co.uk/";
  9.     //RTSP,无效
  10.     //path= "http://m.livestream.com";
  11.     //path= "rtsp://xgrammyawardsx.is.livestream-api.com/livestreamiphone/grammyawards";
  12.     //MMS,无效
  13.     //path= "mms://112.230.192.196/zb12";
  14.     //path = "mms://ting.mop.com/mopradio";
  15.     //组播 ,无效
  16.     //path = "udp://236.1.0.1:2000";
  17.     private String path = "http://dlqncdn.miaopai.com/stream/MVaux41A4lkuWloBbGUGaQ__.mp4";
  18.     private String subtitle_path = "";
  19.     private VideoView mVideoView;
  20.     private TextView mSubtitleView;
  21.     private Button btn_changeLayout;
  22.     private Button bt_play1, bt_play2, bt_play3, bt_play4;
  23.     private long mPosition = 0;
  24.     private int mVideoLayout = 0;
  25.     @Override
  26.     public void onCreate(Bundle icicle) {
  27.         super.onCreate(icicle);
  28.         Vitamio.isInitialized(getApplicationContext());
  29.         setContentView(R.layout.subtitle2);
  30.         mVideoView = (VideoView) findViewById(R.id.surface_view);
  31.         btn_changeLayout = (Button) findViewById(R.id.btn_changeLayout);
  32.         bt_play1 = (Button) findViewById(R.id.bt_play1);
  33.         bt_play2 = (Button) findViewById(R.id.bt_play2);
  34.         bt_play3 = (Button) findViewById(R.id.bt_play3);
  35.         bt_play4 = (Button) findViewById(R.id.bt_play4);
  36.         btn_changeLayout.setOnClickListener(this);
  37.         bt_play1.setOnClickListener(this);
  38.         bt_play2.setOnClickListener(this);
  39.         bt_play3.setOnClickListener(this);
  40.         bt_play4.setOnClickListener(this);

  41.         mVideoView.setVideoPath(path);
  42.         mVideoView.setMediaController(new MediaController(this));
  43.         mVideoView.requestFocus();
  44.         mVideoView.setOnPreparedListener(this);
  45.         mVideoView.setOnTimedTextListener(this);
  46.     }
  47.     @Override
  48.     protected void onPause() {
  49.         mPosition = mVideoView.getCurrentPosition();
  50.         mVideoView.stopPlayback();
  51.         super.onPause();
  52.     }
  53.     @Override
  54.     protected void onResume() {
  55.         if (mPosition > 0) {
  56.             mVideoView.seekTo(mPosition);
  57.             mPosition = 0;
  58.         }
  59.         super.onResume();
  60.         mVideoView.start();
  61.     }
  62.     @Override
  63.     public void onPrepared(MediaPlayer mp) {
  64.         mp.setPlaybackSpeed(1.0f);
  65.         mVideoView.addTimedTextSource(subtitle_path);//不知道哪来的API
  66.         mVideoView.setTimedTextShown(true);//不知道哪来的API
  67.     }
  68.     @Override
  69.     public void onClick(View v) {
  70.         if (v.getId() == R.id.btn_changeLayout) {
  71.             changeLayout(v);
  72.         } else {
  73.             switch (v.getId()) {
  74.             case R.id.bt_play1:
  75.                 path = "http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com   /D046015255134077DDB3ACA0D7E68D45.flv";
  76.                 break;
  77.             case R.id.bt_play2://HLS m3u8
  78.                 path = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";
  79.                 break;
  80.             case R.id.bt_play3:
  81.                 path = "http://7xt0mj.com1.z0.glb.clouddn.com/xia.v.1280.720.f4v";
  82.                 break;
  83.             case R.id.bt_play4:
  84.                 path = "http://7xt0mj.com1.z0.glb.clouddn.com/lianaidaren.v.640.480.mp4";
  85.                 break;
  86.             }
  87.             mVideoView.setVideoPath(path);
  88.             mVideoView.requestFocus();
  89.             mVideoView.start();
  90.         }
  91.     }
  92.     @Override
  93.     public void onTimedText(String text) {
  94.         mSubtitleView.setText(text);
  95.     }
  96.     @Override
  97.     public void onTimedTextUpdate(byte[] pixels, int width, int height) {
  98.     }
  99.     public void changeLayout(View view) {
  100.         mVideoLayout++;
  101.         if (mVideoLayout == 4) mVideoLayout = 0;//循环
  102.         switch (mVideoLayout) {
  103.         case 0:
  104.             mVideoLayout = VideoView.VIDEO_LAYOUT_ORIGIN;//原始画面
  105.             view.setBackgroundResource(R.drawable.mediacontroller_sreen_size_100);
  106.             break;
  107.         case 1:
  108.             mVideoLayout = VideoView.VIDEO_LAYOUT_SCALE;//全屏
  109.             view.setBackgroundResource(R.drawable.mediacontroller_screen_fit);
  110.             break;
  111.         case 2:
  112.             mVideoLayout = VideoView.VIDEO_LAYOUT_STRETCH;//拉伸
  113.             view.setBackgroundResource(R.drawable.mediacontroller_screen_size);
  114.             break;
  115.         case 3:
  116.             mVideoLayout = VideoView.VIDEO_LAYOUT_ZOOM;//裁剪
  117.             view.setBackgroundResource(R.drawable.mediacontroller_sreen_size_crop);
  118.             break;
  119.         }
  120.         mVideoView.setVideoLayout(mVideoLayout, 0);//第二个参数为【宽高比】,为0将自动检测
  121.     }
  122. }

  123. <?xml version="1.0" encoding="utf-8"?>
  124. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  125.     android:layout_width="match_parent"
  126.     android:layout_height="match_parent"
  127.     android:orientation="vertical" >
  128.     <RelativeLayout
  129.         android:layout_width="match_parent"
  130.         android:layout_height="400dp"
  131.         android:layout_gravity="center_horizontal"
  132.         android:orientation="vertical" >
  133.         <io.vov.vitamio.widget.CenterLayout
  134.             android:id="@+id/dd"
  135.             android:layout_width="match_parent"
  136.             android:layout_height="match_parent"
  137.             android:orientation="vertical" >
  138.             <io.vov.vitamio.widget.VideoView
  139.                 android:id="@+id/surface_view"
  140.                 android:layout_width="match_parent"
  141.                 android:layout_height="match_parent"
  142.                 android:layout_centerHorizontal="true"
  143.                 android:layout_centerVertical="true" />
  144.         </io.vov.vitamio.widget.CenterLayout>
  145.         <TextView
  146.             android:layout_width="wrap_content"
  147.             android:layout_height="wrap_content"
  148.             android:layout_alignParentBottom="true"
  149.             android:layout_centerHorizontal="true"
  150.             android:text="@string/res_audio" />
  151.     </RelativeLayout>
  152.     <LinearLayout
  153.         android:layout_width="match_parent"
  154.         android:layout_height="wrap_content"
  155.         android:orientation="horizontal" >
  156.         <Button
  157.             android:id="@+id/bt_play1"
  158.             android:layout_width="0dp"
  159.             android:layout_height="wrap_content"
  160.             android:layout_weight="1"
  161.             android:text="flv" />
  162.         <Button
  163.             android:id="@+id/bt_play2"
  164.             android:layout_width="0dp"
  165.             android:layout_height="wrap_content"
  166.             android:layout_weight="1"
  167.             android:text="m3u8" />
  168.         <Button
  169.             android:id="@+id/bt_play3"
  170.             android:layout_width="0dp"
  171.             android:layout_height="wrap_content"
  172.             android:layout_weight="1"
  173.             android:text="f4v" />
  174.         <Button
  175.             android:id="@+id/bt_play4"
  176.             android:layout_width="0dp"
  177.             android:layout_height="wrap_content"
  178.             android:layout_weight="1"
  179.             android:text="mp4" />
  180.         <Button
  181.             android:id="@+id/btn_changeLayout"
  182.             android:layout_width="wrap_content"
  183.             android:layout_height="wrap_content"
  184.             android:background="@drawable/mediacontroller_sreen_size_100" />
  185.     </LinearLayout>
  186. </LinearLayout>
  187.  

VideoView 显示缓冲信息

  1. /**
  2.  * 显示缓冲相关信息
  3.  * @author 白乾涛
  4.  */
  5. public class VideoViewBuffer extends Activity implements OnInfoListener, OnBufferingUpdateListener, OnPreparedListener {
  6.     private String path = "http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com   /D046015255134077DDB3ACA0D7E68D45.flv";
  7.     
  8.     private Uri uri;
  9.     private VideoView mVideoView;
  10.     private ProgressBar pb;
  11.     private TextView downloadRateView, loadRateView;//网速和已经加载的百分比
  12.     @Override
  13.     public void onCreate(Bundle icicle) {
  14.         super.onCreate(icicle);
  15.         Vitamio.isInitialized(getApplicationContext());
  16.         setContentView(R.layout.videobuffer);
  17.         mVideoView = (VideoView) findViewById(R.id.buffer);
  18.         pb = (ProgressBar) findViewById(R.id.probar);
  19.         downloadRateView = (TextView) findViewById(R.id.download_rate);
  20.         loadRateView = (TextView) findViewById(R.id.load_rate);

  21.         uri = Uri.parse(path);
  22.         mVideoView.setVideoURI(uri);
  23.         mVideoView.setMediaController(new MediaController(this));
  24.         mVideoView.requestFocus();
  25.         mVideoView.setOnInfoListener(this);
  26.         mVideoView.setOnBufferingUpdateListener(this);
  27.         mVideoView.setOnPreparedListener(this);
  28.     }
  29.     //******************************************************************************************
  30.     @Override
  31.     public boolean onInfo(MediaPlayer mp, int what, int extra) {//在有警告或错误信息时调用。例如:开始缓冲、缓冲结束、下载速度变化
  32.         switch (what) {
  33.         case MediaPlayer.MEDIA_INFO_BUFFERING_START://开始缓存,暂停播放,显示正在加载
  34.             if (mVideoView.isPlaying()) {
  35.                 mVideoView.pause();
  36.                 pb.setVisibility(View.VISIBLE);
  37.                 downloadRateView.setText("");
  38.                 loadRateView.setText("");
  39.                 downloadRateView.setVisibility(View.VISIBLE);
  40.                 loadRateView.setVisibility(View.VISIBLE);
  41.             }
  42.             break;
  43.         case MediaPlayer.MEDIA_INFO_BUFFERING_END://缓存完成,继续播放
  44.             mVideoView.start();
  45.             pb.setVisibility(View.GONE);
  46.             downloadRateView.setVisibility(View.GONE);
  47.             loadRateView.setVisibility(View.GONE);
  48.             break;
  49.         case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED://显示 下载速度(KB/s)
  50.             downloadRateView.setText("" + extra + "kb/s" + "  ");
  51.             break;
  52.         }
  53.         return true;
  54.     }
  55.     @Override
  56.     public void onBufferingUpdate(MediaPlayer mp, int percent) {//在网络视频流缓冲变化时调用
  57.         loadRateView.setText(percent + "%");
  58.     }
  59.     @Override
  60.     public void onPrepared(MediaPlayer mp) {
  61.         mp.setPlaybackSpeed(1.0f);
  62.     }
  63. }

  64. <?xml version="1.0" encoding="utf-8"?>
  65. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  66.     android:layout_width="match_parent"
  67.     android:layout_height="match_parent"
  68.     android:orientation="vertical" >
  69.     <io.vov.vitamio.widget.CenterLayout
  70.         android:layout_width="match_parent"
  71.         android:layout_height="match_parent"
  72.         android:orientation="vertical" >
  73.         <io.vov.vitamio.widget.VideoView
  74.             android:id="@+id/buffer"
  75.             android:layout_width="match_parent"
  76.             android:layout_height="match_parent"
  77.             android:layout_centerHorizontal="true"
  78.             android:layout_centerVertical="true" />
  79.     </io.vov.vitamio.widget.CenterLayout>
  80.     <LinearLayout
  81.         android:layout_width="wrap_content"
  82.         android:layout_height="wrap_content"
  83.         android:layout_centerInParent="true"
  84.         android:orientation="horizontal" >
  85.         <ProgressBar
  86.             android:id="@+id/probar"
  87.             style="?android:attr/progressBarStyleLarge"
  88.             android:layout_width="wrap_content"
  89.             android:layout_height="wrap_content" />
  90.         <TextView
  91.             android:id="@+id/download_rate"
  92.             android:layout_width="wrap_content"
  93.             android:layout_height="wrap_content"
  94.             android:layout_gravity="center"
  95.             android:text="" />
  96.         <TextView
  97.             android:id="@+id/load_rate"
  98.             android:layout_width="wrap_content"
  99.             android:layout_height="wrap_content"
  100.             android:layout_gravity="center"
  101.             android:text="" />
  102.     </LinearLayout>
  103. </RelativeLayout>
  104.  

Vitamio VideoView 示例的更多相关文章

  1. vitamio videoView 用隐藏除videoview的控件,并旋转屏幕方向实现的全屏功能,出现的画面不能填充满videoview(画面不完整)

    使用vitamio 封装的播放器 当切换到全屏模式,有时候会出现播放的画面不是全屏的情况, 全屏时,画面只占左半部分并出现拉伸效果,还显示不全,等等其他情况 阅读分析源代码发现是getHolder() ...

  2. 玩转Android之在线视频播放控件Vitamio的使用

    其实Android中自带的MediaPlayer本身就能播放在线视频,MediaPlayer结合SurfaceView播放在线视频也是不错的选择(如果你没有性能或者用户体验上的要求),关于MediaP ...

  3. Android 多媒体视频播放一( 多媒体理解与经验分享)

    前言 说到android的多媒体,一把辛酸一把泪,当初听说会多媒体的比较牛掰,公司也有需求,于是乎我也积极的加入研究android多媒体的行列,记得以前刚接触的时候,最开始还是比较头大的,主要是但是很 ...

  4. 视频播放-VideoVIew,Vitamio

    播放视频文件其实并不比播放音频文件复杂,主要是使用 VideoView类来实现的.这个类将视频的显示和控制集于一身,使得我们仅仅借助它就可以完成一个简易的视频播放器.VideoView的用法和 Med ...

  5. Android视频媒体相关,VideoView和开源框架vitamio

    虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,但是Vitamio的确强大到没朋友! Vitamio 是一款 An ...

  6. VideoView 视频播放 示例

    介绍 实现的功能: 可播放本地视频或网络视频,可控制播放或暂停 最小化时保存播放位置及播放状态,resume时恢复所有状态: 横竖屏切换时保持切换前的位置及状态 在屏幕上竖直滑动可调节屏幕亮度和音量 ...

  7. Vitamio

    前言 虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,这里与大家一起利用免费的Vitamio来打造属于自己的And ...

  8. vitamio框架

    import io.vov.vitamio.LibsChecker; import io.vov.vitamio.MediaPlayer; import io.vov.vitamio.MediaPla ...

  9. Android视录视频示例

    这几天需要搞一个Android视频通话功能,从最简单的视频录制开始,网上例子大多不完整.下面的示例参考过别人的代码,还是拿出来给需要的朋友分享下. Activity类:VideoActivity pa ...

随机推荐

  1. golang io需要牢记的几个点

    对于Reader比较麻烦需要记住以下: When Read encounters an error or end-of-file condition after successfully readin ...

  2. jQuery中的模拟操作

    jQuery中的模拟操作主要是通过trigger来触发,相当于页面加载完成后不需要用户点击按钮,就可以自动触发页面中的相关事件. trigger(type,[data])可以用来模拟触发自定义事件的触 ...

  3. Thinkphp 零散知识点(caa/js路径,引入第三方类,ajax返回,session/cookie)

    一.关于JS和CSS路径问题 1.找路径是从入口文件index.php来找的,而不是从文件本身所在位置来找, 因为我们访问时是访问的入口文件 2.在存放JS和CSS的时候可以放到public文件夹下 ...

  4. js 鼠标双击滚动单击停止

    <!DOCTYPE html> <html> <head> <title>双击滚动代码</title> <script src=&qu ...

  5. window.onload() 等待所有的数据加载都完成之后才会触发

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Javascript图片轮播

    原文链接:http://www.imooc.com/article/7393 编辑HTML代码: <div id="wrap"><!--图片展示区--> & ...

  7. Android 之 WebView

    1:在AndroidManifest.xml中添加允许android访问网络权限. <uses-permission android:name="android.permission. ...

  8. 将银行读卡设备读取到的身份证头像Bitmap属性转换成路径

    需求是这样的,在项目开发的时候要求读取身份证,读到身份证的所有信息(信息里面包括头像属性,类型是Bitmap的).然后服务器要求我传过去的头像信息是String类型的Uri路径. 这是读卡器读到的身份 ...

  9. (转载)PHP mb_substr函数在实际编码中的应用方法

    (转载)http://developer.51cto.com/art/200912/166080.htm 我们在使用PHP语言进行实际编码中时,通常会遇到许多错误的出现,比如在截取字符串时会出现乱码等 ...

  10. 【动态规划】POJ 1161 & ZOJ1463 & XMU 1033 Brackets sequence

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1033 http://poj.org/problem?id=1141 ZOJ目前挂了. ...