2013年8月20日星期二

Android floating window Advanced - PIP, floating video (attachedDemo)

 

Android continues today on a top-level window, floating window advanced applications. Previous mainly on the services and how to use the WindowManager WindowManager write a top-level window. Today is to talk about how to play the video with the top-level window, this feature is very useful for many embedded devices. For example, mobile phones are now a number of peace-board video player can be achieved PIP feature to show only part of the small window, the user can continue to operate other functions. When you receive a text message when watching the video, you just enter the PIP function, you can continue to play the video, but you can go Operation messaging functions.

 

 

(PS: the new QQ group, who are interested can join together to discuss: Android group: 322599434)

 

 

1, MediaPlayer video playback

 

video playback functions using the Android native player, following a brief MediaPlayer use. MediaPlayer is actually very easy to use, but it supported formats are not many, but here is mainly to demonstrate how to implement PIP function. Since the latter is intended that this PIP feature added to the VLC player inside, so we can solve the problem much supported formats.

 
  
   
//Edited by mythou
//http://www.cnblogs.com/mythou/
  
// 播放界面使用surfaceview实现         
surfaceView = (SurfaceView) mlayoutView.findViewById(R.id.myView);
//获取surfaceHolder,控制surfaceview
surfaceHolder = surfaceView.getHolder();
//回调,检测surfaceview的三种状态
surfaceHolder.addCallback(this);
//surfaceview的显示源类型
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//mediaplayer初始化
mediaPlayer = new MediaPlayer();
//设置不同的监听接口
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(
this);
mediaPlayer.setOnPreparedListener(
this);
mediaPlayer.setOnSeekCompleteListener(
this);
mediaPlayer.setOnVideoSizeChangedListener(
this);
String filePath
= "/mnt/card/test.mp4";// "/mnt/sdcard/test.mp4";//
// 本地地址和网络地址都可以
try
{
mediaPlayer.setDataSource(filePath);
}
catch (IllegalArgumentException e)
{
// TODO: handle exception
Log.v(LOGCAT, e.getMessage());
onExit();
}
 
 

above is initialized surfaceView and MeidaPlayer code, surfaceview inside the android application development is very important, basically designed video or animation type of interface, you need to use it. If you do not know a friend, recommended good to familiarize yourself with the use of this class. Here we look at surfaceview callback interface:

 
  
   
//Edited by mythou
//http://www.cnblogs.com/mythou/
  
  @Override 
public void surfaceCreated(SurfaceHolder holder)
{
Log.v(LOGCAT,
"suc calles");
mediaPlayer.setDisplay(holder);
// 若无次句,将只有声音而无图像
try
{
       //播放视频
mediaPlayer.prepare();
}
catch (IllegalStateException e)
{
onExit();
}
catch (IOException e)
{
onExit();
}
  }
 
 

surfaceview inside one of the above is the callback interface, here we are ready to start processing the video playback, as well as inside the surfaceview set to the MediaPlayer, so that they both coordination. MediaPlayer playback function is implemented, and the screen display is dependent surfaceview implementation.

 

 

2, set WindowManager window

 
  
  
  
//Edited by mythou
//http://www.cnblogs.com/mythou/
  
  public void initWindow() 
{
// 获取WindowManager
wm = (WindowManager) context.getApplicationContext().getSystemService(
"window");
// 设置LayoutParams(全局变量)相关参数
// wmParams = ((MyApplication)getApplication()).getMywmParams();
wmParams = new WindowManager.LayoutParams();
/**
* 以下都是WindowManager.LayoutParams的相关属性 具体用途可参考SDK文档
*/
wmParams.type
= /*LayoutParams.TYPE_SYSTEM_ALERT | */LayoutParams.TYPE_SYSTEM_OVERLAY; // 设置window type
// 设置图片格式,效果为背景透明
wmParams.format = PixelFormat.TRANSPARENT;
// 设置Window flag

wmParams.flags
= LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE
| LayoutParams.FLAG_LAYOUT_NO_LIMITS;
/*
* 下面的flags属性的效果形同"锁定"。 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。
* wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL |
* LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE;
*/
wmParams.gravity
= Gravity.LEFT | Gravity.TOP; // 调整悬浮窗口至左上角
// 以屏幕左上角为原点,设置x、y初始值
currentDisplay = wm.getDefaultDisplay();
WIDTH
= currentDisplay.getWidth();
HEIGHT
= currentDisplay.getHeight();
wmParams.x
= (WIDTH - VIEW_WIDTH) / 2;
wmParams.y
= 0;
// 设置悬浮窗口长宽数据
wmParams.width = VIEW_WIDTH;
wmParams.height
= VIEW_HEIGHT;
}
 
 

above is used to set the floating window WindowManager process interfaces with us on a consistent approach, specific knowledge of friends, you can look at my previous article.

 

 

3, interface bindings screen

 

Here we look at how to put our video player interface to bind to the top-level display View above the previous one has been introduced, only need to call addview method can be.

 
  
   
//Edited by mythou
//http://www.cnblogs.com/mythou/
  
MyFloatView sFloatView; 
ViewGroup fView;
//绑定XML配置的界面
fView = (ViewGroup) View.inflate(context, R.layout.main, null);
// 显示myFloatView图像
sFloatView = new MyFloatView(fView);
sFloatView.bindViewListener();
sFloatView.showLayoutView();
 
 

above is configured through an XML to display interface, XML interface which defines Surfaceview class, the last in FloatView inside showLayoutView call WindowManager of addView added to the window service inside.

 
  
public void showLayoutView() 
{
wm.addView(mlayoutView, wmParams);
}
 
 

displayed above is probably bound processes, specific detailed code, see Demo code inside. The Demo is in CSDN above download, wanted to write one, but since there are developers have to share it, I would not re-write their own, the Demo I have some bugs, original downloaded after import is associated with a project results in an error, fix the problem. Download my code below directly into you can use. Then set about video path.

 

This is just a very simple function, video path is hardcoded, just to let everyone know how to achieve PIP function. This function can be transplanted into our multimedia player to achieve the above picture in picture video playback.

 

 

Test Demo : MoviewView2013-8-10.rar

 

 

Related Articles:

 

Android achieve top-level window, floating window (attached Demo)

 

 

Edited by mythou

 

original blog, reproduced, please indicate the source: http://www.cnblogs.com/mythou/p/3250302.html

 
  
 
 

没有评论:

发表评论