2014-10-08 34 views
5

Ho riprodotto correttamente il video di YouTube utilizzando youtube player api. Ma ho bisogno di farlo funzionare in background sul pulsante press.I schiena Googled un sacco ma non trovò nulla Please help me per ottenere this.thanks in anticipoRiproduci video di YouTube in background utilizzando youtube player api

ecco la mia 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()"); 
      } 

     } 

} 

risposta

1

ho cercato questo argomento e ho scoperto che non esiste un modo ufficiale e che non sarà conforme allo this page sul codice Google.

In esso propongono di creare un giocatore.

Nascondere il lettore.

Disattiva il lettore.

Chiama seekTo() con il tuo videoId e offset opzionale.

Attendere il cambiamento di stato in "riproduzione".

Premere pausa.

Chiama seekTo (originalOffset, false).

Attiva il lettore.

Mostra il giocatore. (o qualsiasi altra cosa).

Forse non fa quello che ti serve ma forse puoi cambiare quello che ti serve per raggiungere il tuo obiettivo usando questo metodo.

+0

Il problema con questa soluzione è che il player di youTube si fermerà una volta e l'attività/frammento è in background, ne ho bisogno per continuare a giocare – BoazGarty

+0

Non so molto di quello che sto per dire ora. Che ne dici di utilizzare un servizio che gestisce il buffering e poi passarlo all'attività. Lo esaminerò con te e se qualcuno trova qualcosa, lo pubblicherebbe qui. – Abkarino

+0

forse questo può aiutarti. https://groups.google.com/forum/#!topic/android-developers/3sEonjD0Nqk – Abkarino

0

Controllare lo stato del lettore quando si fa clic sul pulsante Indietro, penso che sarà in pausa e in stato di buffering. puoi provare a riaggiustarlo sugli ascoltatori, ascoltatore onbuffering, chiamare playvideo/cambiare stato per riprodurre video. Ho provato e ha funzionato per iOS, non so i limiti della piattaforma in questo contesto.

+0

È comunque possibile che il lettore si fermi e riproduca di nuovo. e ho bisogno che continui a suonare come lo schermo è in primo piano. – BoazGarty

+0

il video non viene fermato ma messo in pausa, quindi riproduce da dove è stato messo in pausa, prova a farlo funziona @david – Rakii

+0

So che è stato messo in pausa L'ho già implementato, ho bisogno che il video continui a giocare e non si fermi per ~ secondi e poi giocare. – BoazGarty

Problemi correlati