本文介绍了Android的波泰特视频方向错误的VideoView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我捕捉到这样一个Android设备上的纵向新的视频:

I capture a new video in PORTRAIT orientation on an Android device like this:

Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 1886);

和它给了我这个文件:/mnt/sdcard/DCIM/Camera/video-2012-02-02-10-45-48.mp4

and it gives me this file: "/mnt/sdcard/DCIM/Camera/video-2012-02-02-10-45-48.mp4"

然后我玩这样的:

private VideoView videoView = (VideoView) findViewById(R.id.videoView);
String videoUrl = "/mnt/sdcard/DCIM/Camera/video-2012-02-02-10-45-48.mp4";
videoView.setMediaController(new MediaController(this));
videoView.setVideoURI(Uri.parse(videoUrl));
videoView.start();

下面是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<VideoView
    android:id="@+id/videoView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true" />

</RelativeLayout>

当我在标准Android库发挥它的方向是正确的。但是,当我在VideoView播放视频之上,它的旋转90度。景观的伟大工程,唯一的问题是纵向视频。

When I play it in the standard Android gallery, the orientation is correct. But when I play the video in the VideoView above, it's rotated 90 degrees. Landscape works great, the only problem are portrait videos.

我如何可以旋转视频在VideoView?
此外,如何以编程方式确定方向?

How can I rotate this video in the VideoView?
Also, how can I programmatically determine the orientation?

推荐答案

您需要初步判断捕捉到的视频的方向。大多数新的智能手机采用横向的摄像头虽然有版本其在使用肖像。要确定方向,你可以把帧的长度和宽度,然后对它们进行比较。当你开始检查是否该活动定位的视频,并根据定向性的变化。

You need to initially determine the orientation of the captured video. Most new smartphones use landscape orientation for the camera although there are versions which use portrait. To determine the orientation you can take the length and width of the frame then compare them. When you start to check whether that activity orientation video, and depending on the change of orientation activities.

code例:

public class MainActivity extends ActionBarActivity {

    String videoUrl = "/mnt/sdcard/DCIM/100ANDRO/MOV_9195.mp4";
    int videoWidth;
    int videoHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getVideoAspectRatio();
        if (isVideoLandscaped()) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

        setContentView(R.layout.activity_main);
        VideoView videoView = (VideoView) findVewById(R.id.videoView);
        videoView.setMediaController(new MediaController(this));
        videoView.setVideoURI(Uri.parse(videoUrl));
        videoView.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void getVideoAspectRatio() {
        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(this, Uri.parse(videoUrl));
        String height = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
        String width = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
        videoWidth = Integer.parseInt(width);
        videoHeight = Integer.parseInt(height);
    }

    private boolean isVideoLandscaped() {
        if (videoWidth > videoHeight) {
            return true;
        } else return false;
    }
}

不要忘了隐藏动作条的风格,或以编程方式在活动。

Don't forget to hide the ActionBar in styles, or programmatically in activity.

这篇关于Android的波泰特视频方向错误的VideoView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:05