日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CenterCrop的Video View

發(fā)布時間:2023/12/9 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CenterCrop的Video View 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

首先,原生的VideoView是繼承SurfaceView加上媒體控制器,不能直接處理。而對TextureView centerCrop的原理很簡單,如下:
在onVideoSizeChanged方法

if (videoWidth == 0 || videoHeight == 0) {return;}float sx = (float) getWidth() / (float) videoWidth;float sy = (float) getHeight() / (float) videoHeight;float maxScale = Math.max(sx, sy);sx = maxScale / sx;sy = maxScale / sy;Matrix matrix = new Matrix();matrix.setScale(sx, sy, getWidth() / 2f, getHeight() / 2f);if (matrix != null) {setTransform(matrix);}

更多的類型可以參考
https://github.com/codeguyFred/Android-ScalableVideoView
在自定義View的入口加上setSurfaceTextureListener監(jiān)聽
但是有些手機onSurfaceTextureAvailable回調(diào)會不夠及時。
如果mMediaPlayer.setSurface(mSurface);中的mSurface是null的你會出現(xiàn)黑屏的情況。
完整代碼如下

/*** https://github.com/codeguyFred/Android-ScalableVideoView* Created by leida on 2019/09/25.* centerCrop視頻,單純展示* 正確使用方法* vBgVideo.post(() -> **);*/ public class ScaleVideoView extends TextureView implements TextureView.SurfaceTextureListener,MediaPlayer.OnVideoSizeChangedListener {protected MediaPlayer mMediaPlayer;private Surface mSurface = null;public ScaleVideoView(Context context) {this(context, null);}public ScaleVideoView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ScaleVideoView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);setSurfaceTextureListener(this);}@Overridepublic void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {mSurface = new Surface(surfaceTexture);if (mMediaPlayer != null) {mMediaPlayer.setSurface(mSurface);}}@Overridepublic void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}@Overridepublic boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {if (mSurface != null) {mSurface.release();mSurface = null;}return true;}@Overridepublic void onSurfaceTextureUpdated(SurfaceTexture surface) {}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();if (mMediaPlayer == null) {return;}if (isPlaying()) {stop();}release();}@Overridepublic void onVideoSizeChanged(MediaPlayer mp, int width, int height) {scaleVideoSize(width, height);}private void scaleVideoSize(int videoWidth, int videoHeight) {if (videoWidth == 0 || videoHeight == 0) {return;}float sx = (float) getWidth() / (float) videoWidth;float sy = (float) getHeight() / (float) videoHeight;float maxScale = Math.max(sx, sy);sx = maxScale / sx;sy = maxScale / sy;Matrix matrix = new Matrix();matrix.setScale(sx, sy, getWidth() / 2f, getHeight() / 2f);if (matrix != null) {setTransform(matrix);}}private void initializeMediaPlayer() {if (mMediaPlayer == null) {mMediaPlayer = new MediaPlayer();} else {reset();}mMediaPlayer.setOnVideoSizeChangedListener(this);mMediaPlayer.setSurface(mSurface);}public void setRawData(@RawRes int id) throws IOException {AssetFileDescriptor afd = getResources().openRawResourceFd(id);setDataSource(afd);}public void setAssetData(@NonNull String assetName) throws IOException {AssetManager manager = getContext().getAssets();AssetFileDescriptor afd = manager.openFd(assetName);setDataSource(afd);}private void setDataSource(@NonNull AssetFileDescriptor afd) throws IOException {setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());afd.close();}public void setDataSource(@NonNull String path) throws IOException {initializeMediaPlayer();mMediaPlayer.setDataSource(path);}public void setDataSource(@NonNull Context context, @NonNull Uri uri,@Nullable Map<String, String> headers) throws IOException {initializeMediaPlayer();mMediaPlayer.setDataSource(context, uri, headers);}public void setDataSource(@NonNull Context context, @NonNull Uri uri) throws IOException {initializeMediaPlayer();mMediaPlayer.setDataSource(context, uri);}public void setDataSource(@NonNull FileDescriptor fd, long offset, long length)throws IOException {initializeMediaPlayer();mMediaPlayer.setDataSource(fd, offset, length);}public void setDataSource(@NonNull FileDescriptor fd) throws IOException {initializeMediaPlayer();mMediaPlayer.setDataSource(fd);}public void prepare(@Nullable MediaPlayer.OnPreparedListener listener)throws IOException, IllegalStateException {mMediaPlayer.setOnPreparedListener(listener);mMediaPlayer.prepare();}public void prepareAsync(@Nullable MediaPlayer.OnPreparedListener listener)throws IllegalStateException {mMediaPlayer.setOnPreparedListener(listener);mMediaPlayer.prepareAsync();}public void prepare() throws IOException, IllegalStateException {prepare(null);}public void prepareAsync() throws IllegalStateException {prepareAsync(null);}public void setOnErrorListener(@Nullable MediaPlayer.OnErrorListener listener) {mMediaPlayer.setOnErrorListener(listener);}public void setOnCompletionListener(@Nullable MediaPlayer.OnCompletionListener listener) {mMediaPlayer.setOnCompletionListener(listener);}public void setOnInfoListener(@Nullable MediaPlayer.OnInfoListener listener) {mMediaPlayer.setOnInfoListener(listener);}public int getCurrentPosition() {return mMediaPlayer.getCurrentPosition();}public int getDuration() {return mMediaPlayer.getDuration();}public int getVideoHeight() {return mMediaPlayer.getVideoHeight();}public int getVideoWidth() {return mMediaPlayer.getVideoWidth();}public boolean isLooping() {return mMediaPlayer.isLooping();}public void setLooping(boolean looping) {mMediaPlayer.setLooping(looping);}public boolean isPlaying() {return mMediaPlayer.isPlaying();}public void pause() {mMediaPlayer.pause();}public void seekTo(int msec) {mMediaPlayer.seekTo(msec);}public void setVolume(float leftVolume, float rightVolume) {mMediaPlayer.setVolume(leftVolume, rightVolume);}public void start() {if (mMediaPlayer != null) {mMediaPlayer.start();}}public void stop() {if (mMediaPlayer != null) {mMediaPlayer.stop();}}public void reset() {if (mMediaPlayer != null) {mMediaPlayer.reset();}}public void release() {if (mMediaPlayer != null) {reset();mMediaPlayer.release();mMediaPlayer = null;}} }

使用方法

vBgVideo.post(() -> playBgVideo()); private void playBgVideo() {try {vBgVideo.setRawData(R.raw.login_bg_video);vBgVideo.setVolume(0, 0);vBgVideo.setLooping(true);vBgVideo.prepare(mp -> vBgVideo.start());} catch (IOException e) {//忽略}}

總結(jié)

以上是生活随笔為你收集整理的CenterCrop的Video View的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。