使用WindowManager添加到全局布局时,VideoView无法播放视频
问题描述:
我没有找到类似的问题或答案,因此我决定发布一个新问题。使用WindowManager添加到全局布局时,VideoView无法播放视频
我已经创建了一个自定义的RelativeLayout,它起到了内容布局的包装的作用,该包装在所谓包装的初始化过程中被充气并添加到包装中。
除了视频播放,一切似乎都很好,实际上视频根本没有加载(onPrepared永远不会被调用)。我也试着听过可能与它有关的任何错误,但是没有任何错误(OnError从未被调用)。 Log中没有包含任何可能与此问题有关的信息。
这是一个奇怪的问题,因为VideoView在Activities,Fragments等中工作得很好,但是当涉及到将其添加到全局(系统范围)布局(使用WindowManager)时,它开始变得非常奇怪。
甚至可以在全局(系统范围)布局中的VideoView中播放视频吗?
任何帮助,将不胜感激!提前致谢!
代码片段:
CustomRelativeLayout
public class CustomRelativeLayout extends RelativeLayout {
public static final String TAG = "Some Tag";
public CustomRelativeLayout(Context context) {
super(context);
init();
}
//The rest of the constructors
//go here...
private void init() {
WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = LayoutInflater.from(getContext());
View inflatedView = inflater.inflate(R.layout.my_layout, this, false);
VideoView videoView = (VideoView) inflatedView.findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse("http://www.html5videoplayer.net/videos/toystory.mp4"));
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.e(TAG, "The Video is ready to be played!");
mediaPlayer.start();
}
});
//adding the inflated view
addView(inflatedView);
//adding to the windowManager
Point size = new Point();
mWindowManager.getDefaultDisplay().getSize(size);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
size.x,
size.y
);
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
layoutParams.flags = (WindowManager.LayoutParams.FLAG_DIM_BEHIND | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
layoutParams.dimAmount = 0f;
layoutParams.format = PixelFormat.RGBA_8888;
mWindowManager.addView(this, layoutParams);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"/>
</RelativeLayout>
权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
答
View inflatedView = inflater.inflate(R.layout.my_layout, this, false);
//you should add view i think
addView(inflatedView);
VideoView videoView = (VideoView) inflatedView.findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse("http://www.html5videoplayer.net/videos/toystory.mp4"));
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.e(TAG, "The Video is ready to be played!");
mediaPlayer.start();
}
});
但添加了充气视图 –