- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我编写了一个重现该问题的示例应用程序:
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 的高度,而没有考虑原始视频的比例。
在这里:
但是,如果我在 Samsung Galaxy Nexus 上播放视频,视频的比例是正确的。
在这里:
如果我强制视频占据 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();
}
}
}
最佳答案
所以这个问题发生在 VideoView
和 MediaPlayer
+ SurfaceView
上。
当系统询问视频的宽度 x 高度时,它返回 640x480 而它应该返回 852x480。
即
@Override
public void onPrepared(MediaPlayer mp) {
mp.getVideoWidth();
mp.getVideoHeight();
}
这要么是视频容器/编解码器的 MediaPlayer
处理中的错误,要么是视频文件本身的问题。
无论哪种方式,我都通过将我知道的视频宽度和高度添加到我的代码中来规避它。我已经更新了 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/
我编写了一个重现该问题的示例应用程序: https://github.com/blundell/VideoRatioProblemPerDevice VideoView文档状态: Displays a
我是一名优秀的程序员,十分优秀!