Android VideoView 中纵向视频方向错误

发布于 2025-01-02 12:08:36 字数 1156 浏览 2 评论 0原文

我在 Android 设备上以肖像方向捕获一个新视频,如下所示:

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”

然后我像这样播放:

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 度。横向效果很好,唯一的问题是纵向视频。

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

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);

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

Then I play it like this:

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();

Here's my layout file:

<?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>

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习惯成性 2025-01-09 12:08:36

您需要首先确定捕获视频的方向。大多数新型智能手机都使用横向相机,但也有一些版本使用纵向相机。要确定方向,您可以获取框架的长度和宽度,然后进行比较。当您开始检查该活动是否定向视频,并根据活动定向的变化。

代码示例:

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;
    }
}

不要忘记在样式中或以编程方式在活动中隐藏 ActionBar。

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 Example:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文