Nexus 6 y MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
Hola estoy tratando de lograr una actividad con el fondo de archivo de vídeo. La solución que se muestra a continuación funciona en la mayoría de los dispositivos, excepto nexus 6 Android 5.1. El video funciona, está escalado pero no se recorta, por lo que la relación de aspecto no se conserva.
Probado con Galaxy Note, HTC One m7, Xperia Z3, Nexus 5.
- Android - 360 metadatos de vídeo
- ¿Cómo obtener vídeo de la intención de la cámara y guardarlo en un directorio?
- Marcas de tiempo de presentación incorrectas al buscar hacia atrás a través de vídeo MP4
- MediaPlayer no puede procesar en TextureView después de renderizar imágenes
- SetVideoSize () se bloquea con altas resoluciones
Aquí está el código:
public class MainActivity extends ActionBarActivity implements SurfaceHolder.Callback, OnClickListener { private static final int VIDEO_START_HACK_DELAY = 0; private static final String BG_VIDEO_PATH = "/raw/construction"; private SurfaceView mSurface; private Button mTour, mLogin; private MediaPlayer mMediaPlayer; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start_main); setupUI(); } private void setupUI() { mMediaPlayer = new MediaPlayer(); mSurface = (SurfaceView) findViewById(R.id.surface); SurfaceHolder holder = mSurface.getHolder(); holder.addCallback(this); mTour = (Button) findViewById(R.id.tour); mLogin = (Button) findViewById(R.id.login); mTour.setOnClickListener(this); mLogin.setOnClickListener(this); } @Override public void surfaceCreated(SurfaceHolder holder) { startVideo(holder); } @SuppressLint("NewApi") public void startVideo(SurfaceHolder holder) { String path = "android.resource://" + getPackageName() + BG_VIDEO_PATH; Uri uri = Uri.parse(path); try { android.view.ViewGroup.LayoutParams lp = mSurface.getLayoutParams(); mMediaPlayer.setDisplay(holder); mMediaPlayer.setDataSource(this, uri); mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); mMediaPlayer.setLooping(true); mMediaPlayer.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mHandler.postDelayed(new Runnable() { @Override public void run() { mMediaPlayer.start(); } }, VIDEO_START_HACK_DELAY); } }); mMediaPlayer.prepareAsync(); } catch (Exception e) { Log.e("VID", "Problem starting video bg", e); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } @Override public void onBackPressed() { mSurface.setVisibility(View.INVISIBLE); mMediaPlayer.stop(); super.onBackPressed(); } @Override protected void onDestroy() { mMediaPlayer.stop(); mMediaPlayer.release(); super.onDestroy(); } }
Aquí está el archivo de diseño:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <SurfaceView android:id="@+id/surface" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="40dp" android:gravity="center" android:orientation="vertical" > <Button android:id="@+id/tour" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" android:padding="10dp" android:text="@string/ma_tour"/> <Button android:id="@+id/login" android:layout_height="wrap_content" android:layout_width="wrap_content" android:padding="10dp" android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" android:text="@string/ma_log_in"/> </LinearLayout> </RelativeLayout>
Target sdk se establece en 22. ¿Has experimentado tal comportamiento y puedes dar algún consejo?
- Buscando en YouTube HTML5 video sobre Android WebView
- Grabación de vídeo en Android usando JavaCV (Actualizado 2014 02 17)
- Aplicación de efectos en el video que se está reproduciendo
- Cómo subir un video desde un teléfono Android (como Android) mientras se está grabando
- ANDROID: ¿Cómo puedo descargar un archivo de vídeo a una tarjeta SD?
- Cómo poner subtítulos en videos para móviles con Handheld.PlayFullScreenMovie en la unidad
- ¿Cómo incrustar texto durante la grabación de vídeo en Android?
- Intención de tomar video en android
Aquí es cómo lo hice en una de mis aplicaciones:
-
Establezca OnPreparedListener en el objeto MediaPlayer.
MediaPlayer.setOnPreparedListener (this);
-
Cuando el
public void onPrepared(MediaPlayer mp)
es llamado, redimensione elSurfaceView
para que cree la ilusión de vídeo recortado. Log.v (LOG_TAG, "onPrepared llamado para MediaPlayer"); // Ajusta el tamaño del video para que encaje en la pantalla int videoWidth = mInitialVideoPlayer.getVideoWidth (); Int videoHeight = mInitialVideoPlayer.getVideoHeight (); Float videoProportion = (float) videoWidth / (float) videoHeight;ViewGroup.LayoutParams lp = mSplashVideoSurfaceView.getLayoutParams(); // We always want to fill entire view port. So, we keep the smallest dimension and proportionately // scale the other dimension. if (videoWidth < videoHeight) { lp.width = UiUtils.sScreenWidth; lp.height = (int) ((float) UiUtils.sScreenWidth / videoProportion); } else { lp.width = (int) (videoProportion * (float) UiUtils.sScreenHeight);If you are targetting API level 14 and above, <code>TextureView</code> allows you to play cropped video lp.height = UiUtils.sScreenHeight; } Log.v(LOG_TAG, "old video size: " + videoWidth + ", " + videoHeight); Log.v(LOG_TAG, "screen size: " + UiUtils.sScreenWidth + ", " + UiUtils.sScreenHeight); Log.v(LOG_TAG, "new video size: " + lp.width + ", " + lp.height); mSplashVideoSurfaceView.setLayoutParams(lp);
- Datos que faltan en la página de análisis de comercio electrónico mejorada de Android
- Com.android.builder.testing.api.DeviceException: com.android.ddmlib.InstallException: No se pudo instalar todo