videos puedo problema porque poner para pantalla fondos fondo como celular cambiar ajustar android android-mediaplayer live-wallpaper

puedo - problema fondo de pantalla android



¿Cómo ajustar el video en el fondo de pantalla en vivo, por recorte central y ajustando al ancho/alto? (3)

Por lo tanto, aún no pude obtener todos los tipos de escala que me pediste, pero sí pude ponerme en forma xy y el centro de cultivos trabajando con bastante facilidad usando el exo player. El código completo se puede ver en https://github.com/yperess/StackOverflow/tree/50091878 y lo actualizaré a medida que obtenga más. Finalmente, también rellenaré MainActivity para permitirle elegir el tipo de escala como la configuración (lo haré con una simple PreferenceActivity) y leer el valor de las preferencias compartidas en el lado del servicio.

La idea general es que en el fondo, MediaCodec ya implementa fit-xy y center-crop, que en realidad son los únicos 2 modos que necesitarías si tuvieras acceso a una jerarquía de vistas. Este es el caso porque el centro de ajuste, la parte superior del ajuste, la parte inferior del ajuste en realidad solo sería el ajuste xy donde la superficie tiene una gravedad y se escala para coincidir con el tamaño del video * la escala mínima. Para que funcionen, lo que creo que tendrá que ocurrir es que deberíamos crear un contexto OpenGL y proporcionar una SurfaceTexture. Esta SurfaceTexture se puede envolver con un trozo de Surface que se puede pasar al exo player. Una vez que el video está cargado, podemos establecer el tamaño de estos ya que los creamos. También tenemos una devolución de llamada en SurfaceTexture para avisarnos cuando un marco está listo. En este punto, deberíamos poder modificar el marco (es de esperar que solo se utilice una escala y transformación de matriz simples).

Los componentes clave aquí son la creación del reproductor exo:

private fun initExoMediaPlayer(): SimpleExoPlayer { val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter) val trackSelector = DefaultTrackSelector(videoTrackSelectionFactory) val player = ExoPlayerFactory.newSimpleInstance(this@MovieLiveWallpaperService, trackSelector) player.playWhenReady = true player.repeatMode = Player.REPEAT_MODE_ONE player.volume = 0f if (mode == Mode.CENTER_CROP) { player.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING } else { player.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT } if (mode == Mode.FIT_CENTER) { player.addVideoListener(this) } return player }

Luego cargando el video:

override fun onSurfaceCreated(holder: SurfaceHolder) { super.onSurfaceCreated(holder) if (mode == Mode.FIT_CENTER) { // We need to somehow wrap the surface or set some scale factor on exo player here. // Most likely this will require creating a SurfaceTexture and attaching it to an // OpenGL context. Then for each frame, writing it to the original surface but with // an offset exoMediaPlayer.setVideoSurface(holder.surface) } else { exoMediaPlayer.setVideoSurfaceHolder(holder) } val videoUri = RawResourceDataSource.buildRawResourceUri(R.raw.small) val dataSourceFactory = DataSource.Factory { RawResourceDataSource(context) } val mediaSourceFactory = ExtractorMediaSource.Factory(dataSourceFactory) exoMediaPlayer.prepare(mediaSourceFactory.createMediaSource(videoUri)) }

ACTUALIZAR:

Lo tengo funcionando, necesitaré mañana para limpiarlo antes de publicar el código, pero aquí hay una vista previa ...

Lo que terminé haciendo, básicamente, tomando GLSurfaceView y destrozándolo. Si miras la fuente, lo único que falta y que hace que sea imposible de usar en un fondo de pantalla es el hecho de que solo inicia el GLThread cuando está conectado a la ventana. Por lo tanto, si replica el mismo código pero permite iniciar manualmente el subproceso GL, puede continuar. Después de eso, solo necesitas hacer un seguimiento de qué tan grande es tu pantalla frente al video después de escalar a la escala mínima que se ajusta y cambiar el cuadrante en el que dibujas.

Problemas conocidos con el código: 1. Hay un pequeño error con el GLThread que no he podido solucionar. Parece que hay un simple problema de tiempo en el que cuando el hilo se detiene, recibo una llamada a signallAll() que en realidad no está esperando nada. 2. No me molesté en modificar dinámicamente el modo en el renderizador. No debería ser demasiado difícil. Agregue una escucha de preferencias cuando cree el Motor y luego actualice el renderizador cuando cambie el tipo de scale_type .

ACTUALIZACIÓN: Todos los problemas han sido resueltos. signallAll() estaba lanzando porque me faltó un cheque para ver si realmente tenemos el bloqueo. También agregué un servicio de escucha para actualizar el tipo de escala dinámicamente, de modo que ahora todos los tipos de escala usan GlEngine.

¡DISFRUTAR!

Fondo

Estoy haciendo un fondo de pantalla en vivo que puede mostrar un video. Al principio, pensé que esto iba a ser muy difícil, por lo que algunas personas sugirieron usar soluciones OpenGL u otras soluciones muy complejas (como esta ).

De todos modos, para esto, he encontrado varios lugares que hablan de eso, y en base a esta biblioteca de github (que tiene algunos errores), finalmente lo hice funcionar.

El problema

Aunque he logrado mostrar un video, no puedo encontrar la manera de controlar cómo se muestra en comparación con la resolución de la pantalla.

Actualmente, siempre se puede estirar hasta el tamaño de la pantalla, lo que significa que este (video tomado desde here ):

llega a mostrar como esto:

La razón es la relación de aspecto diferente: 560x320 (resolución de video) vs 1080x1920 (resolución del dispositivo).

Nota: soy muy consciente de las soluciones de escalado de videos, que están disponibles en varios repositorios de Github (como here ), pero estoy preguntando por un fondo de pantalla en vivo. Como tal, no tiene una Vista, por lo que es más limitado sobre cómo hacer las cosas. Para ser más específicos, una solución no puede tener ningún tipo de diseño, un TextureView o un SurfaceView, o cualquier otro tipo de Vista.

Lo que he intentado

Intenté jugar con varios campos y funciones de SurfaceHolder, pero hasta ahora no he tenido suerte. Ejemplos:

Aquí está el código actual que he hecho (proyecto completo disponible here ):

class MovieLiveWallpaperService : WallpaperService() { override fun onCreateEngine(): WallpaperService.Engine { return VideoLiveWallpaperEngine() } private enum class PlayerState { NONE, PREPARING, READY, PLAYING } inner class VideoLiveWallpaperEngine : WallpaperService.Engine() { private var mp: MediaPlayer? = null private var playerState: PlayerState = PlayerState.NONE override fun onSurfaceCreated(holder: SurfaceHolder) { super.onSurfaceCreated(holder) Log.d("AppLog", "onSurfaceCreated") mp = MediaPlayer() val mySurfaceHolder = MySurfaceHolder(holder) mp!!.setDisplay(mySurfaceHolder) mp!!.isLooping = true mp!!.setVolume(0.0f, 0.0f) mp!!.setOnPreparedListener { mp -> playerState = PlayerState.READY setPlay(true) } try { //mp!!.setDataSource(this@MovieLiveWallpaperService, Uri.parse("http://techslides.com/demos/sample-videos/small.mp4")) mp!!.setDataSource(this@MovieLiveWallpaperService, Uri.parse("android.resource://" + packageName + "/" + R.raw.small)) } catch (e: Exception) { } } override fun onDestroy() { super.onDestroy() Log.d("AppLog", "onDestroy") if (mp == null) return mp!!.stop() mp!!.release() playerState = PlayerState.NONE } private fun setPlay(play: Boolean) { if (mp == null) return if (play == mp!!.isPlaying) return when { !play -> { mp!!.pause() playerState = PlayerState.READY } mp!!.isPlaying -> return playerState == PlayerState.READY -> { Log.d("AppLog", "ready, so starting to play") mp!!.start() playerState = PlayerState.PLAYING } playerState == PlayerState.NONE -> { Log.d("AppLog", "not ready, so preparing") mp!!.prepareAsync() playerState = PlayerState.PREPARING } } } override fun onVisibilityChanged(visible: Boolean) { super.onVisibilityChanged(visible) Log.d("AppLog", "onVisibilityChanged:" + visible + " " + playerState) if (mp == null) return setPlay(visible) } } class MySurfaceHolder(private val surfaceHolder: SurfaceHolder) : SurfaceHolder { override fun addCallback(callback: SurfaceHolder.Callback) = surfaceHolder.addCallback(callback) override fun getSurface() = surfaceHolder.surface!! override fun getSurfaceFrame() = surfaceHolder.surfaceFrame override fun isCreating(): Boolean = surfaceHolder.isCreating override fun lockCanvas(): Canvas = surfaceHolder.lockCanvas() override fun lockCanvas(dirty: Rect): Canvas = surfaceHolder.lockCanvas(dirty) override fun removeCallback(callback: SurfaceHolder.Callback) = surfaceHolder.removeCallback(callback) override fun setFixedSize(width: Int, height: Int) = surfaceHolder.setFixedSize(width, height) override fun setFormat(format: Int) = surfaceHolder.setFormat(format) override fun setKeepScreenOn(screenOn: Boolean) {} override fun setSizeFromLayout() = surfaceHolder.setSizeFromLayout() override fun setType(type: Int) = surfaceHolder.setType(type) override fun unlockCanvasAndPost(canvas: Canvas) = surfaceHolder.unlockCanvasAndPost(canvas) } }

Las preguntas

Me gustaría saber cómo ajustar la escala del contenido en función de lo que tenemos para ImageView, a la vez que mantenemos la relación de aspecto:

  1. Cultivo central: se ajusta al 100% del contenedor (la pantalla en este caso), recortando los lados (arriba y abajo o izquierda y derecha) cuando sea necesario. No estira nada. Esto significa que el contenido parece estar bien, pero no se puede mostrar todo.
  2. ajuste centro - estirar para ajustar ancho / alto
  3. centrado interior: se establece como tamaño original, centrado y se estira para ajustarse al ancho / alto solo si es demasiado grande.

Puede usar Glide para GIF y la carga de imágenes, así como las opciones de escalado que desee. Basado en el documento https://bumptech.github.io/glide/doc/targets.html#sizes-and-dimensions y https://futurestud.io/tutorials/glide-image-resizing-scaling this.

Glide v4 requiere Android Ice Cream Sandwich (API nivel 14) o superior.

Me gusta :

public static void loadCircularImageGlide(String imagePath, ImageView view) { Glide.with(view.getContext()) .load(imagePath) .asGif() .override(600, 200) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio .error(R.drawable.create_timeline_placeholder) .fitCenter() // scaling options .transform(new CircularTransformation(view.getContext())) // Even you can Give image tranformation too .into(view); }


Puedes lograr esto con un TextureView. (SurfaceView no funcionará tampoco). He encontrado un código que lo ayudará a lograrlo.
En esta demostración, puede recortar el video en tres tipos de centro, superior e inferior .

TextureVideoView.java

public class TextureVideoView extends TextureView implements TextureView.SurfaceTextureListener { // Indicate if logging is on public static final boolean LOG_ON = true; // Log tag private static final String TAG = TextureVideoView.class.getName(); private MediaPlayer mMediaPlayer; private float mVideoHeight; private float mVideoWidth; private boolean mIsDataSourceSet; private boolean mIsViewAvailable; private boolean mIsVideoPrepared; private boolean mIsPlayCalled; private ScaleType mScaleType; private State mState; public enum ScaleType { CENTER_CROP, TOP, BOTTOM } public enum State { UNINITIALIZED, PLAY, STOP, PAUSE, END } public TextureVideoView(Context context) { super(context); initView(); } public TextureVideoView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public TextureVideoView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } private void initView() { initPlayer(); setScaleType(ScaleType.CENTER_CROP); setSurfaceTextureListener(this); } public void setScaleType(ScaleType scaleType) { mScaleType = scaleType; } private void updateTextureViewSize() { float viewWidth = getWidth(); float viewHeight = getHeight(); float scaleX = 1.0f; float scaleY = 1.0f; if (mVideoWidth > viewWidth && mVideoHeight > viewHeight) { scaleX = mVideoWidth / viewWidth; scaleY = mVideoHeight / viewHeight; } else if (mVideoWidth < viewWidth && mVideoHeight < viewHeight) { scaleY = viewWidth / mVideoWidth; scaleX = viewHeight / mVideoHeight; } else if (viewWidth > mVideoWidth) { scaleY = (viewWidth / mVideoWidth) / (viewHeight / mVideoHeight); } else if (viewHeight > mVideoHeight) { scaleX = (viewHeight / mVideoHeight) / (viewWidth / mVideoWidth); } // Calculate pivot points, in our case crop from center int pivotPointX; int pivotPointY; switch (mScaleType) { case TOP: pivotPointX = 0; pivotPointY = 0; break; case BOTTOM: pivotPointX = (int) (viewWidth); pivotPointY = (int) (viewHeight); break; case CENTER_CROP: pivotPointX = (int) (viewWidth / 2); pivotPointY = (int) (viewHeight / 2); break; default: pivotPointX = (int) (viewWidth / 2); pivotPointY = (int) (viewHeight / 2); break; } Matrix matrix = new Matrix(); matrix.setScale(scaleX, scaleY, pivotPointX, pivotPointY); setTransform(matrix); } private void initPlayer() { if (mMediaPlayer == null) { mMediaPlayer = new MediaPlayer(); } else { mMediaPlayer.reset(); } mIsVideoPrepared = false; mIsPlayCalled = false; mState = State.UNINITIALIZED; } /** * @see MediaPlayer#setDataSource(String) */ public void setDataSource(String path) { initPlayer(); try { mMediaPlayer.setDataSource(path); mIsDataSourceSet = true; prepare(); } catch (IOException e) { Log.d(TAG, e.getMessage()); } } /** * @see MediaPlayer#setDataSource(Context, Uri) */ public void setDataSource(Context context, Uri uri) { initPlayer(); try { mMediaPlayer.setDataSource(context, uri); mIsDataSourceSet = true; prepare(); } catch (IOException e) { Log.d(TAG, e.getMessage()); } } /** * @see MediaPlayer#setDataSource(java.io.FileDescriptor) */ public void setDataSource(AssetFileDescriptor afd) { initPlayer(); try { long startOffset = afd.getStartOffset(); long length = afd.getLength(); mMediaPlayer.setDataSource(afd.getFileDescriptor(), startOffset, length); mIsDataSourceSet = true; prepare(); } catch (IOException e) { Log.d(TAG, e.getMessage()); } } private void prepare() { try { mMediaPlayer.setOnVideoSizeChangedListener( new MediaPlayer.OnVideoSizeChangedListener() { @Override public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { mVideoWidth = width; mVideoHeight = height; updateTextureViewSize(); } } ); mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mState = State.END; log("Video has ended."); if (mListener != null) { mListener.onVideoEnd(); } } }); // don''t forget to call MediaPlayer.prepareAsync() method when you use constructor for // creating MediaPlayer mMediaPlayer.prepareAsync(); // Play video when the media source is ready for playback. mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mIsVideoPrepared = true; if (mIsPlayCalled && mIsViewAvailable) { log("Player is prepared and play() was called."); play(); } if (mListener != null) { mListener.onVideoPrepared(); } } }); } catch (IllegalArgumentException e) { Log.d(TAG, e.getMessage()); } catch (SecurityException e) { Log.d(TAG, e.getMessage()); } catch (IllegalStateException e) { Log.d(TAG, e.toString()); } } /** * Play or resume video. Video will be played as soon as view is available and media player is * prepared. * * If video is stopped or ended and play() method was called, video will start over. */ public void play() { if (!mIsDataSourceSet) { log("play() was called but data source was not set."); return; } mIsPlayCalled = true; if (!mIsVideoPrepared) { log("play() was called but video is not prepared yet, waiting."); return; } if (!mIsViewAvailable) { log("play() was called but view is not available yet, waiting."); return; } if (mState == State.PLAY) { log("play() was called but video is already playing."); return; } if (mState == State.PAUSE) { log("play() was called but video is paused, resuming."); mState = State.PLAY; mMediaPlayer.start(); return; } if (mState == State.END || mState == State.STOP) { log("play() was called but video already ended, starting over."); mState = State.PLAY; mMediaPlayer.seekTo(0); mMediaPlayer.start(); return; } mState = State.PLAY; mMediaPlayer.start(); } /** * Pause video. If video is already paused, stopped or ended nothing will happen. */ public void pause() { if (mState == State.PAUSE) { log("pause() was called but video already paused."); return; } if (mState == State.STOP) { log("pause() was called but video already stopped."); return; } if (mState == State.END) { log("pause() was called but video already ended."); return; } mState = State.PAUSE; if (mMediaPlayer.isPlaying()) { mMediaPlayer.pause(); } } /** * Stop video (pause and seek to beginning). If video is already stopped or ended nothing will * happen. */ public void stop() { if (mState == State.STOP) { log("stop() was called but video already stopped."); return; } if (mState == State.END) { log("stop() was called but video already ended."); return; } mState = State.STOP; if (mMediaPlayer.isPlaying()) { mMediaPlayer.pause(); mMediaPlayer.seekTo(0); } } /** * @see MediaPlayer#setLooping(boolean) */ public void setLooping(boolean looping) { mMediaPlayer.setLooping(looping); } /** * @see MediaPlayer#seekTo(int) */ public void seekTo(int milliseconds) { mMediaPlayer.seekTo(milliseconds); } /** * @see MediaPlayer#getDuration() */ public int getDuration() { return mMediaPlayer.getDuration(); } static void log(String message) { if (LOG_ON) { Log.d(TAG, message); } } private MediaPlayerListener mListener; /** * Listener trigger ''onVideoPrepared'' and `onVideoEnd` events */ public void setListener(MediaPlayerListener listener) { mListener = listener; } public interface MediaPlayerListener { public void onVideoPrepared(); public void onVideoEnd(); } @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { Surface surface = new Surface(surfaceTexture); mMediaPlayer.setSurface(surface); mIsViewAvailable = true; if (mIsDataSourceSet && mIsPlayCalled && mIsVideoPrepared) { log("View is available and play() was called."); play(); } } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }

Después de eso, use esta clase como el siguiente código en MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ActionBar.OnNavigationListener { // Video file url private static final String FILE_URL = "http://techslides.com/demos/sample-videos/small.mp4"; private TextureVideoView mTextureVideoView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initActionBar(); if (!isWIFIOn(getBaseContext())) { Toast.makeText(getBaseContext(), "You need internet connection to stream video", Toast.LENGTH_LONG).show(); } } private void initActionBar() { ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); actionBar.setDisplayShowTitleEnabled(false); SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_dropdown_item); actionBar.setListNavigationCallbacks(mSpinnerAdapter, this); } private void initView() { mTextureVideoView = (TextureVideoView) findViewById(R.id.cropTextureView); findViewById(R.id.btnPlay).setOnClickListener(this); findViewById(R.id.btnPause).setOnClickListener(this); findViewById(R.id.btnStop).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnPlay: mTextureVideoView.play(); break; case R.id.btnPause: mTextureVideoView.pause(); break; case R.id.btnStop: mTextureVideoView.stop(); break; } } final int indexCropCenter = 0; final int indexCropTop = 1; final int indexCropBottom = 2; @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { switch (itemPosition) { case indexCropCenter: mTextureVideoView.stop(); mTextureVideoView.setScaleType(TextureVideoView.ScaleType.CENTER_CROP); mTextureVideoView.setDataSource(FILE_URL); mTextureVideoView.play(); break; case indexCropTop: mTextureVideoView.stop(); mTextureVideoView.setScaleType(TextureVideoView.ScaleType.TOP); mTextureVideoView.setDataSource(FILE_URL); mTextureVideoView.play(); break; case indexCropBottom: mTextureVideoView.stop(); mTextureVideoView.setScaleType(TextureVideoView.ScaleType.BOTTOM); mTextureVideoView.setDataSource(FILE_URL); mTextureVideoView.play(); break; } return true; } public static boolean isWIFIOn(Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); return (networkInfo != null && networkInfo.isConnected()); } }

y el archivo activity_main.xml del diseño para eso está abajo

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.example.videocropdemo.crop.TextureVideoView android:id="@+id/cropTextureView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_margin="16dp" android:orientation="horizontal"> <Button android:id="@+id/btnPlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play" /> <Button android:id="@+id/btnPause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pause" /> <Button android:id="@+id/btnStop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" /> </LinearLayout> </RelativeLayout>

La salida del código para el recorte central parece