Search code examples
androidandroid-youtube-apiyoutubeplayer

Android - YouTube Api player on floating window using service


I like to implement YouTube Player as a floating window (on top all apps) on Android.

I saw many apps that do the same like this one

And I try to Understand how to implement it.

I tried this :

 wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
 LayoutInflater li = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
 View vi = li.inflate(R.layout.youtube_view,null);
 YouTubePlayerView youTubeView = vi.findViewById(R.id.youtube_view);
 youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);

But get This error :

Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity  which extends YouTubeBaseActivity as its context

And it is obvious - as I extends Service and not YouTubeBaseActivity

Did all those floating YouTube apps use webview as the player container ? or did i miss better solution using the official Youtube Player API


Solution

  • They are using a WebView-based player. You can find an opensource implementation here: android-youtube-player