Android YouTube api v3 – orientación
Obtener una muestra de actividad en marcha utilizando YouTubeBaseActivity, YouTubePlayerView y YouTubePlayer fue bastante simple. Tengo problemas con la orientación y no puedo encontrar ninguna documentación o código de ejemplo. Cuando cambio mientras se está reproduciendo un video, la pantalla está en blanco.
¿Cuáles son las cosas correctas que hacer en cada uno de onCreate (), onPause (), onSaveInstanceState () y onRestoreInstanceState () para que el video continúe reproduciendo?
- NullPointerException en com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart (fuente desconocida)
- Pantalla blanca tras cerrar el video de pantalla completa abierto desde WebView
- Principal java.lang.RuntimeException: No se puede iniciar la actividad ComponentInfo
- API de YouTube de Android "Se ha producido un error al inicializar el reproductor de YouTube"
- Anuncios de Youtube antes del video dirigido
Gracias
- ¿Es posible integrar el canal de youtube en android?
- Excepción de clase no encontrada al ejecutar demostraciones de API de YouTube para Android
- reproducir streaming en VideoView, convertir url a rtsp
- Youtube iframe player no mostrará videos en android 4.x WebView
- Haciendo un perfecto reproductor Youtube de Chromeless en Android
- Youtube API "Se ha producido un error al inicializar el reproductor de YouTube".
- Reproduce vídeos de Youtube en la vista de vídeo en android
- ¿Cuál es el formato del video compartido por YouTube en Android
Variables
@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;
Sobre crecer
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAutoRotation = Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1; }
Implementar 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>
He realizado una actividad de muestra que utiliza la API más reciente de youtube.
Esta fuente maneja " Problema de orientación ", " Problema de volumen de medios ", " Problema de análisis de URL de YouTube "
-
Aquí está el proyecto git para la aplicación de ejemplo
-
También he hecho una aplicación de ejemplo que puedes descargar
https://play.google.com/store/apps/details?id=com.thefinestartist.ytpa.sample
Dejar esto aquí para otros. La respuesta de la mejor artista funcionó para mí también, pero como estoy usando un reproductor de video dentro de un fragmento y tengo que apoyar AppCompatLibrary también, he aquí lo que pude conseguir trabajando:
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); } }
- Android Parcelable – Escriba y lea ArrayList <IA> cuando IA es una interfaz
- Icono de la barra de acciones activado no es el título