2013-02-21 15 views
8

Avere un'attività di esempio attivo e funzionante utilizzando YouTubeBaseActivity, YouTubePlayerView e YouTubePlayer era abbastanza semplice. Tuttavia, ho problemi con l'orientamento e non riesco a trovare documentazione o codice di esempio. Quando passo mentre un video è in riproduzione, lo schermo è vuoto.Android YouTube api v3 - orientamento

Quali sono le cose corrette da fare in ciascuno di onCreate(), onPause(), onSaveInstanceState() e onRestoreInstanceState() per far continuare la riproduzione del video?

Grazie

+1

Ho capito da solo - l'errore era che non stavo inizializzando YouTubePlayerView in onCreate() quando è stato chiamato onCreate() durante un ripristino piuttosto che una creazione esplicita. Tutto risolto .. – milleph

risposta

22

Variabili

@SuppressLint("InlinedApi") 
private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9 
     ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT 
     : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT; 

@SuppressLint("InlinedApi") 
private static final int LANDSCAPE_ORIENTATION = Build.VERSION.SDK_INT < 9 
     ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE 
     : ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE; 

private YouTubePlayer mPlayer = null; 
private boolean mAutoRotation = false; 

OnCreate

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mAutoRotation = Settings.System.getInt(getContentResolver(), 
      Settings.System.ACCELEROMETER_ROTATION, 0) == 1; 
} 

Attuare OnInitializedListener

@Override 
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, 
     boolean wasRestored) { 
    mPlayer = player; 
    player.setOnFullscreenListener(this); 

    if (mAutoRotation) { 
     player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION 
       | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI 
       | YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE 
       | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT); 
    } else { 
     player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION 
       | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI 
       | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT); 
    } 
} 

Inplement onConfigurationChanged

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     if (mPlayer != null) 
      mPlayer.setFullscreen(true); 
    } 
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
     if (mPlayer != null) 
      mPlayer.setFullscreen(false); 
    } 
} 

@Override 
public void onFullscreen(boolean fullsize) { 
    if (fullsize) { 
     setRequestedOrientation(LANDSCAPE_ORIENTATION); 
    } else { 
     setRequestedOrientation(PORTRAIT_ORIENTATION); 
    } 
} 

Menifest

<activity 
    android:name="com.sample.android.YouTubePlayerActivity" 
    android:configChanges="keyboardHidden|orientation|screenSize" 
    android:screenOrientation="sensor" 
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > 
</activity> 

ho fatto attività campione che utilizza più recente API di YouTube.

Questa maniglia fonte "Orientamento problema", "media Volume Problem", "Youtube analisi degli URL Problema"

  1. Ecco progetto Git per il campione app

    https://github.com/TheFinestArtist/YouTubePlayerActivity

  2. Ho anche creato l'app di esempio che è possibile scaricare

    https://play.google.com/store/apps/details?id=com.thefinestartist.ytpa.sample

+0

Hai salvato la mia giornata .. cool – Pankaj

+0

Ottima risposta e ottima biblioteca! – aveschini

1

Lasciando questo qui per gli altri. La risposta di The Finest artista ha lavorato anche per me, ma dal momento che sto utilizzando un lettore video all'interno di un frammento e devono sostenere l'AppCompatLibrary così, ecco quello che ero in grado di ottenere di lavoro:

YouTubePlayerActivityFragment.java

public class YouTubePlayerActivityFragment extends YouTubePlayerSupportFragment implements 
    YouTubePlayer.OnFullscreenListener { 

@SuppressLint("InlinedApi") 
private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9 
     ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT 
     : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT; 

@SuppressLint("InlinedApi") 
private static final int LANDSCAPE_ORIENTATION = Build.VERSION.SDK_INT < 9 
     ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE 
     : ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE; 

private YouTubePlayer.OnFullscreenListener fullScreenListener = null; 
private YouTubePlayer yPlayer = null; 
private boolean mAutoRotation = false; 


public static YouTubePlayerActivityFragment newInstance(String videoID, String apiKey) { 
    YouTubePlayerActivityFragment fragment = new YouTubePlayerActivityFragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("video_id", videoID); 
    bundle.putString("api_key", apiKey); 
    fragment.setArguments(bundle); 
    fragment.init(); 
    return fragment; 
} 

public YouTubePlayerActivityFragment() { 

} 

@Override 
public void onCreate(Bundle var1) { 
    super.onCreate(var1); 
    mAutoRotation = Settings.System.getInt(getActivity().getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1; 
    fullScreenListener = this; 
} 

private void init() { 
    initialize(getArguments().getString("api_key"), new YouTubePlayer.OnInitializedListener() { 
     @Override 
     public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) { 
      yPlayer = youTubePlayer; 
      youTubePlayer.setOnFullscreenListener(fullScreenListener); 
      if (mAutoRotation) { 
       youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION 
         | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI 
         | YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE 
         | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT); 
      } else { 
       youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION 
         | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI 
         | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT); 
      } 

      if (!wasRestored) { 
       yPlayer.loadVideo(getArguments().getString("video_id")); 
      } 
     } 

     @Override 
     public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { 

     } 
    }); 
} 

@Override 
public void onFullscreen(boolean isFullSize) { 
    if (isFullSize) { 
     getActivity().setRequestedOrientation(LANDSCAPE_ORIENTATION); 
    } else { 
     getActivity().setRequestedOrientation(PORTRAIT_ORIENTATION); 
    } 
} 
Problemi correlati