2016-07-31 70 views
0

我沒有找到類似的問題或答案,因此我決定發佈一個新問題。使用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" /> 

回答

0
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(); 
    } 
}); 
+0

但添加了充氣視圖 –