Play Youtube video in background using YouTube api player

I successfully played the video on YouTube using the youtube player api . But I need to run it in the background on the back button. I searched a lot, but found nothing. Please help me achieve this. Thanks in advance.

here is my code -

 public class FullscreenDemoActivity extends YouTubeFailureRecoveryActivity implements View.OnClickListener, YouTubePlayer.OnFullscreenListener { // private MyPlaybackEventListener myPlaybackEventListener; private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9 ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT; private LinearLayout baseLayout; private YouTubePlayerView playerView; public YouTubePlayer player; private Button fullscreenButton; private CompoundButton checkbox; private View otherViews; private boolean fullscreen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fullscreen_demo); baseLayout = (LinearLayout) findViewById(R.id.layout); playerView = (YouTubePlayerView) findViewById(R.id.player); fullscreenButton = (Button) findViewById(R.id.fullscreen_button); checkbox = (CompoundButton) findViewById(R.id.landscape_fullscreen_checkbox); otherViews = findViewById(R.id.other_views); playerView.initialize(DeveloperKey.DEVELOPER_KEY, this); } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { this.player = player; setControlsEnabled(); // Specify that we want to handle fullscreen behavior ourselves. player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT); player.setOnFullscreenListener(this); if (!wasRestored) { player.cueVideo("avP5d16wEp0"); } int controlFlags = player.getFullscreenControlFlags(); setRequestedOrientation(PORTRAIT_ORIENTATION); controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE; player.setFullscreenControlFlags(controlFlags); player.play(); } @Override protected YouTubePlayer.Provider getYouTubePlayerProvider() { return playerView; } @Override public void onClick(View v) { player.setFullscreen(!fullscreen); } private void doLayout() { LinearLayout.LayoutParams playerParams = (LinearLayout.LayoutParams) playerView .getLayoutParams(); if (fullscreen) { // When in fullscreen, the visibility of all other views than the // player should be set to // GONE and the player should be laid out across the whole screen. playerParams.width = LayoutParams.MATCH_PARENT; playerParams.height = LayoutParams.MATCH_PARENT; otherViews.setVisibility(View.GONE); } else { otherViews.setVisibility(View.VISIBLE); ViewGroup.LayoutParams otherViewsParams = otherViews .getLayoutParams(); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { playerParams.width = otherViewsParams.width = 0; playerParams.height = WRAP_CONTENT; otherViewsParams.height = MATCH_PARENT; playerParams.weight = 1; baseLayout.setOrientation(LinearLayout.HORIZONTAL); } else { playerParams.width = otherViewsParams.width = MATCH_PARENT; playerParams.height = WRAP_CONTENT; playerParams.weight = 0; otherViewsParams.height = 0; baseLayout.setOrientation(LinearLayout.VERTICAL); } setControlsEnabled(); } } private void setControlsEnabled() { checkbox.setEnabled(player != null && getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); fullscreenButton.setEnabled(player != null); } @Override public void onFullscreen(boolean isFullscreen) { fullscreen = isFullscreen; doLayout(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // System.out.println(getScreenOrientation()); doLayout(); } private final class MyPlaybackEventListener implements PlaybackEventListener { private void updateLog(String prompt){ System.out.println(prompt); }; @Override public void onBuffering(boolean arg0) { updateLog("onBuffering(): " + String.valueOf(arg0)); } @Override public void onPaused() { updateLog("onPaused()"); } @Override public void onPlaying() { updateLog("onPlaying()"); } @Override public void onSeekTo(int arg0) { updateLog("onSeekTo(): " + String.valueOf(arg0)); } @Override public void onStopped() { player.loadVideo("avP5d16wEp0"); player.cueVideo("avP5d16wEp0"); player.play(); updateLog("onStopped()"); } } } 
+5
source share
2 answers

I searched for this topic and I found that there is no official way and will not match this page in Google code.

In it, they propose to create a player.

Hide player.

Turn off the player.

Call seekTo () with your video and extra offset.

Wait for the status change to β€œplay”.

Press pause.

Call seekTo (originalOffset, false).

Turn on the player.

Show the player. (or something else).

It may not do what you need, but perhaps you can change what you need to achieve your goal using this method.

+1
source

Check the status of the player when you press the "Back" button, I think that it will be in a paused and buffered state. you can try to fix it on the listeners, listen to the listening, call playvideo / change state to play the video. I tried and worked on iOS, I do not know the platform limitations in this context.

0
source

Source: https://habr.com/ru/post/1204256/


All Articles