gpt4 book ai didi

android - 在 TextureView 中旋转视频/媒体播放器

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:19 25 4
gpt4 key购买 nike

我正在使用 camera2,长按缩略图后会显示照片/视频的预览。另外,我根据拍摄照片时相机的方向旋转它。例如,如果我以 90º 拍摄图片,我的预览也会旋转 90º。

一切正常,我正在使用 customContainer 并使用 onLayout 和 OnMeasure 根据屏幕大小、宽高比和方向创建预览。它适用于照片。当我尝试对视频做同样的事情时,我的问题出现了,它们只能在 0º 下工作。

我尝试旋转包含我的 MediaPlayer 的 TextureView,但此后我的 onLayout 变得疯狂,不可能找到 (l,t,r,b) 组合来正确测量它。

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>

<com.android.camera.ui.common.ThumbnailContainer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preview_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded_rectangle_thumbnail_preview"
android:visibility="invisible">



<TextureView
android:id="@+id/show_video_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"/>

<ImageView
android:id="@+id/image_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:visibility="invisible"

/>
</com.android.camera.ui.common.ThumbnailContainer>

这是我的 Surface 代码:

    @Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Log.i(TAG, "InicializoSurface. Width: " + width + " HEIGHT:" + height);
Log.i(TAG, "InicializoSurface. Width: " + mVideoView.getMeasuredWidth() + " HEIGHT:" + mVideoView.getMeasuredHeight());
Log.i(TAG, "View transform. Width: " + mVideoView.getWidth() + " HEIGHT:" + mVideoView.getHeight());


mMediaSurface = new Surface(mVideoView.getSurfaceTexture());
initializeMediaPlayer();

}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {

if (mMediaPlayer != null) {
// Make sure we stop video and release resources when activity is destroyed.
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {

}
//////////
private void initializeMediaPlayer(){

mMediaPlayer = new CustomMediaPlayer();
Uri uri = Uri.parse(mCameraDataAdapter.getList().get(0).getPath());

try {
mMediaPlayer.setDataSource(mActivity, uri);
mMediaPlayer.setSurface(mMediaSurface);
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnPreparedListener(mMediaPlayer);
mMediaPlayer.setOnCompletionListener(mMediaPlayer);


} catch (IOException e) {
e.printStackTrace();
}


}


///////////
mVideoView.setVisibility(View.VISIBLE);

// mVideoView.setTranslationX(-200);
// mVideoView.setTranslationY(-200);
Log.i(TAG, "X: " + mVideoView.getX() + "Y: " + mVideoView.getY());


if (mVideoView.isAvailable()) {
onSurfaceTextureAvailable(mVideoView.getSurfaceTexture(), mVideoView.getWidth(), mVideoView.getHeight());
}

if (mMediaPlayer == null) {
initializeMediaPlayer();
}

// mMediaPlayer.mVideoHolder = mVideoView.getHolder();
// mMediaPlayer.setDisplay(mMediaPlayer.mVideoHolder);

if (mMediaPrepared) {
Log.i(TAG,"Comienzo Video");
mMediaPlayer.start();
}

最后是我的 CustomView 中的 onMeasure/OnLayout

      @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int width;
int height;
int wantedWidth = 0;
int wantedHeight = 0;

if(mWidth == 0 && mHeight == 0 ){
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight =MeasureSpec.getSize(heightMeasureSpec);
}

width = mWidth;
height = mHeight;





if (mOrientation == 0 || mOrientation == 180) {

wantedWidth = width - (int)(mMargin * 2);

mVideo.measure(MeasureSpec.makeMeasureSpec(wantedWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int) (wantedWidth * mVideoAspectRatio), MeasureSpec.EXACTLY));
wantedHeight = (mViewTop.getLayoutParams().height) * 2 + (int) (wantedWidth * mAspectRatio);

} else {
Log.e(TAG, "Real Width = " + width + " real Height = " + height);

wantedHeight = width - 2 * mViewTop.getLayoutParams().height - (int)(mMargin * 2);

mVideo.measure(MeasureSpec.makeMeasureSpec(wantedHeight, MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec((int) (wantedHeight * mAspectRatio), MeasureSpec.EXACTLY));
//
wantedWidth =(int) (wantedHeight * mAspectRatio) ;
wantedHeight = width - (int)(mMargin * 2);


}

Log.e(TAG, "onMeasure: " + wantedWidth + "x" + wantedHeight);
setMeasuredDimension(MeasureSpec.makeMeasureSpec(wantedWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(wantedHeight, MeasureSpec.EXACTLY));
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int w = getMeasuredWidth();
int h = getMeasuredHeight();

int viewHeight = mViewBottom.getMeasuredHeight();
int imageViewHeight = mImage.getMeasuredHeight();

int wantedHeight = 0;
// w = w - (int) (2 * mMargin);
if (mOrientation == 0 || mOrientation == 180) {

mVideo.layout(0,wantedHeight,w,wantedHeight + imageViewHeight);


}else{
mVideo.layout(viewHeight,0,r-viewHeight - (int) mMargin,w);
}
}

我一直在寻找其他帖子 Android MediaRecorder making rotated video并且我看到无法旋转 textureView,但我无法相信我可以如此轻松地旋转图像并且必须在此期间进行战斗以将视频旋转 90 度。

最佳答案

感谢@pskink 在帖子中的评论,我找到了他的解决方案。最后,我使用矩阵来旋转视频容器(纹理 View )。 pskink给我的方法是下一个:

  private void setupMatrix(int width, int height, int degrees, boolean isHorizontal) {
Log.d(TAG, "setupMatrix for " + degrees + " degrees");
Matrix matrix = new Matrix();
//The video will be streched if the aspect ratio is in 1,5(recording at 480)
RectF src;
if (isHorizontal)
//In my case, I changed this line, because with my onMeasure() and onLayout() methods my container view is already rotated and scaled, so I need to sent the inverted params to the src.
src = new RectF(0, 0,mThumbnailContainer.getmWidth(), mThumbnailContainer.getmHeight());
else
src = new RectF(0, 0, mThumbnailContainer.getmWidth(),mThumbnailContainer.getmHeight());
RectF dst = new RectF(0, 0, width, height);
RectF screen = new RectF(dst);
Log.d(TAG, "Matrix: " + width + "x" + height);
Log.d(TAG, "Matrix: " + mThumbnailContainer.getmWidth() + "x" + mThumbnailContainer.getmHeight());
matrix.postRotate(degrees, screen.centerX(), screen.centerY());
matrix.mapRect(dst);

matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
matrix.mapRect(src);

matrix.setRectToRect(screen, src, Matrix.ScaleToFit.FILL);
matrix.postRotate(degrees, screen.centerX(), screen.centerY());

mVideoView.setTransform(matrix);
}

终于成功了,看起来非常棒。有了这个,我已经能够根据我的设备屏幕和用于录制视频或拍照的纵横比完全动态地旋转和缩放任何视频。

关于android - 在 TextureView 中旋转视频/媒体播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35872995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com