gpt4 book ai didi

android - VideoVideo - 视频宽度 x 高度 - MediaPlayer 问题

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

我编写了一个重现该问题的示例应用程序:

https://github.com/blundell/VideoRatioProblemPerDevice

VideoView文档状态:

Displays a video file. ... takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling

问题是 Samsung Galaxy S3 对视频做了一些奇怪的事情并且不遵守视频的比例。(也发生在 HTC 设备上)。

全屏 fragment 的 Activity :

我发现当视频在 Samsung Galaxy S3 上播放时,它会以不正确的比例播放。看起来它已经被拉伸(stretch)到 View 的高度,而没有考虑原始视频的比例。

在这里: s3

但是,如果我在 Samsung Galaxy Nexus 上播放视频,视频的比例是正确的。

在这里: n

如果我强制视频占据 fragment 的全部大小,它在 S3 上看起来没问题(因为屏幕的比例就是视频的比例)。但是我不想这样做,因为它搞砸了在其他地方使用的 fragment ,即平板电脑

代码是:

带有带有 VideoView 的 fragment 的 Activity。可以在这里看到:GITHUB CODE LINK

如果你想要一些代码,这里是 hte VideoPlayerFragment:

public class VideoPlayerFragment extends Fragment {

private static final String TAG = "VideoPlayer";

private FitVideoView videoView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_video_player, container, false);

videoView = (FitVideoView) root.findViewById(R.id.surface);

return root;
}

public void playVideo() {
Uri uri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.test_vid);
Log.d(TAG, "Uri is: " + uri);
setVideoLocation(uri);
if (!videoView.isPlaying()) {
videoView.start();
}
}

private void setVideoLocation(Uri uri) {
try {
videoView.setVideoURI(uri);
} catch (Exception e) {
Log.e(TAG, "VideoPlayer uri was invalid", e);
Toast.makeText(getActivity(), "Not found", Toast.LENGTH_SHORT).show();
}
}

public void pauseVideo() {
if (videoView.isPlaying()) {
videoView.pause();
}
}
}

最佳答案

所以这个问题发生在 VideoViewMediaPlayer + SurfaceView 上。

当系统询问视频的宽度 x 高度时,它返回 640x480 而它应该返回 852x480。

@Override
public void onPrepared(MediaPlayer mp) {
mp.getVideoWidth();
mp.getVideoHeight();
}

这要么是视频容器/编解码器的 MediaPlayer 处理中的错误,要么是视频文件本身的问题。

GSpot Details

无论哪种方式,我都通过将我知道的视频宽度和高度添加到我的代码中来规避它。我已经更新了 Git Repo有了这个修复。 黑客警报

这是我发现 different size details of my video 的问题的链接.

您可以将此修复应用到 VideoView 或直接应用到 MediaPlayer + SurfaceView 您的选择。这是 VideoView 的答案:

public class FitVideoView extends VideoView {

private final int mVideoWidth = 853;
private final int mVideoHeight = 480;
private boolean applyFix = true;

public FitVideoView(Context context) {
super(context);
}

public FitVideoView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public FitVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (applyFix) { // A Toggle so I can see both results
// This doesn't ask the video for it's size, but uses my hardcoded size
applyFix(widthMeasureSpec, heightMeasureSpec);
} else {
// This asks the video for its size (which gives an incorrect WxH) then does the same code as below
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

private void applyFix(int widthMeasureSpec, int heightMeasureSpec) {
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
if (mVideoWidth > 0 && mVideoHeight > 0) {
if (mVideoWidth * height > width * mVideoHeight) {
Log.d("TAG", "image too tall, correcting");
height = width * mVideoHeight / mVideoWidth;
} else if (mVideoWidth * height < width * mVideoHeight) {
Log.d("TAG", "image too wide, correcting");
width = height * mVideoWidth / mVideoHeight;
} else {
Log.d("TAG", "aspect ratio is correct: " + width + "/" + height + "=" + mVideoWidth + "/" + mVideoHeight);
}
}
Log.d("TAG", "setting size: " + width + 'x' + height);
setMeasuredDimension(width, height);
}
}

关于android - VideoVideo - 视频宽度 x 高度 - MediaPlayer 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14296093/

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