Firefly开源社区

打印 上一主题 下一主题

[基础,代码流]利用VideoView和SurfaceView进行视频播放

493

积分

6

威望

0

贡献

技术达人

Rank: 2

积分
493
QQ

[基础,代码流]利用VideoView和SurfaceView进行视频播放

发表于 2016-10-11 16:39:49      浏览:4985 | 回复:0        打印      只看该作者   [复制链接] 楼主
本帖最后由 jingjin221 于 2016-10-11 16:52 编辑

MainActivity.java
  1. /*
  2. /*        利用原生VIDEOVIEW和SURFACEVIEW进行本地文件播放
  3. /*        VIDEOVIEW可用于进行流媒体播放
  4. /*
  5. /*
  6. */
  7. package com.timestech.xplayer;

  8. import java.io.File;
  9. import java.io.IOException;

  10. import android.annotation.SuppressLint;
  11. import android.app.Activity;
  12. import android.content.pm.ActivityInfo;
  13. import android.media.AudioManager;
  14. import android.media.MediaPlayer;
  15. import android.net.Uri;
  16. import android.os.Bundle;
  17. import android.util.Log;
  18. import android.view.SurfaceHolder;
  19. import android.view.SurfaceView;
  20. import android.view.View;
  21. import android.view.Window;
  22. import android.view.WindowManager;
  23. import android.view.WindowManager.LayoutParams;
  24. import android.widget.MediaController;
  25. import android.widget.VideoView;

  26. public class MainActivity extends Activity implements SurfaceHolder.Callback {
  27.         private MediaPlayer xPlayer = null;
  28.         private SurfaceView xSurface = null;
  29.        
  30.         private VideoView xVideoView = null;
  31.        
  32.         private static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
  33.         private static final String videoLocalUrl = "http://192.168.1.179:60000";
  34.         @SuppressLint("SdCardPath")
  35.         private static final String videoPath = "/mnt/sdcard/Movies/L00.mp4";
  36.        
  37.         @SuppressLint("SdCardPath")
  38.         @Override
  39.         protected void onCreate(Bundle savedInstanceState) {
  40.                 super.onCreate(savedInstanceState);
  41.                 //隐藏标题栏
  42.                 this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  43.                 //沉浸模式
  44.                 getWindow().getDecorView().setSystemUiVisibility(
  45.                                                                                                                 View.SYSTEM_UI_FLAG_FULLSCREEN |
  46.                                                                                                                 View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
  47.                                                                                                                 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
  48.                 // 设置横屏 禁止翻转
  49.                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  50.                
  51.                 // 禁止休眠
  52.                 getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
  53.                                 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  54.                
  55.                 setContentView(R.layout.activity_main);
  56. if(false)
  57. {
  58.                 //----------------------------------------videoView播放视频----------------------------------------//
  59.                 //xVideoView = (VideoView) findViewById(R.id.videoView);
  60.                 MediaController  mediaco = new MediaController(this);  
  61.             File file = new File(videoPath);
  62.         if(file.exists()){
  63.                 Log.e("###XPLAYER###", "ok!");
  64.             
  65.                 xVideoView.setVideoURI(Uri.parse(videoLocalUrl));
  66.                 //xVideoView.setVideoPath(file.getAbsolutePath());
  67.                
  68.                 //VideoView与MediaController进行关联  
  69.                 xVideoView.setMediaController(mediaco);
  70.                
  71.             mediaco.setMediaPlayer(xVideoView);
  72.             
  73.             //让VideoView获取焦点  
  74.             xVideoView.requestFocus();
  75.             
  76.             xVideoView.start();
  77.         }
  78.         else
  79.                 Log.e("###XPLAYER###", "Not found this file!");
  80. }
  81. else
  82. {
  83.                 //----------------------------------------surfaceView播放视频----------------------------------------//
  84.                 xSurface = (SurfaceView)findViewById(R.id.surfaceView);
  85.         
  86.         /*
  87.                 LayoutParams lp = (LayoutParams) xSurface.getLayoutParams();        //导致闪退
  88.                 lp.width = 1280;
  89.                 lp.height = 647;
  90.                 xSurface.setLayoutParams(lp);
  91.                 */
  92.             
  93.             SurfaceHolder surfaceHolder = xSurface.getHolder(); //SurfaceHolder是SurfaceView的控制接口
  94.             surfaceHolder.addCallback(this); //因为这个类实现了SurfaceHolder.Callback接口,所以回调参数直接this
  95.                 //surfaceHolder.setFixedSize(1280, 720); //显示的分辨率,不是surface的大小,不设置为视频默认
  96. }

  97.         }

  98.         @SuppressLint("SdCardPath")
  99.         public void surfaceCreated(SurfaceHolder holder) {
  100.                 //必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
  101.                 xPlayer = new MediaPlayer();
  102.                 //xPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  103.                 xPlayer.setDisplay(holder);
  104.         //设置显示视频显示在SurfaceView上
  105.         try {
  106.                 File file = new File(videoPath);
  107.             if(file.exists()){
  108.                     Log.e("###XPLAYER###", "ok!");
  109.                     xPlayer.setDataSource(file.getAbsolutePath());
  110.                     xPlayer.prepare();
  111.                     xPlayer.start();
  112.             }
  113.             else
  114.                     Log.e("###XPLAYER###", "Not found this file!");
  115.         } catch (Exception e) {
  116.             e.printStackTrace();
  117.         }
  118.         }

  119.         @Override
  120.         public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  121.                 // TODO Auto-generated method stub
  122.                
  123.         }

  124.         @Override
  125.         public void surfaceDestroyed(SurfaceHolder arg0) {
  126.                 // TODO Auto-generated method stub
  127.                
  128.         }
  129.        
  130.     protected void onDestroy() {
  131.             if(xPlayer!=null)
  132.             {       
  133.             //Activity销毁时停止播放,释放资源。不做这个操作,即使退出还是能听到视频播放的声音
  134.                 if(xPlayer.isPlaying()) {
  135.                         xPlayer.stop();
  136.                 }
  137.                 xPlayer.release();
  138.             }
  139.         super.onDestroy();
  140.     }
  141. }
复制代码


activity_main.xml
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent" >

  5.     <SurfaceView
  6.         android:id="@+id/surfaceView"
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:layout_gravity="center" />
  10.     <!--  
  11.      <VideoView
  12.              android:id="@+id/videoView"
  13.         android:layout_width="800dp"
  14.         android:layout_height="600dp"
  15.         android:layout_gravity="center" />
  16.         -->
  17. </FrameLayout>
复制代码








回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

友情链接 : 爱板网 电子发烧友论坛 云汉电子社区 粤ICP备14022046号-2
快速回复 返回顶部 返回列表