Android 在不水平拉伸(stretch)视频的情况下将 VideoView 方向从纵向切换为横向

标签 android video

我有一个应用程序可以播放外部存储中的视频。这些视频在纵向模式下看起来不错,但在横向模式下会被拉伸(stretch)。有什么简单的方法可以设置横向模式视频播放宽度以适合视频?

这是我的视频播放 Activity :

public class PlayVideoActivity extends Activity implements OnCompletionListener {

    private VideoView video;
    private MediaController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_play_video);
        video = (VideoView) findViewById(R.id.vwPlayVideo);
        controller = new MediaController(this);

        video.setOnCompletionListener(this);


        Bundle extras = getIntent().getExtras();
        if(extras != null){
            String videoPath = extras.getString("videoPath");
            video.setVideoPath(videoPath);
            controller.setMediaPlayer(video);
            video.setMediaController(controller);
            video.requestFocus();
            video.start();

            if(savedInstanceState != null){
                video.seekTo(savedInstanceState.getInt("currentPos"));
            }
        }   
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        finish();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if(video.isPlaying()){
            outState.putInt("currentPos", video.getCurrentPosition());
        }   
        super.onSaveInstanceState(outState);
    }

}

编辑:activity_play_video.xml:

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

    <VideoView
        android:id="@+id/vwPlayVideo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

最佳答案

在您的 .xml 中尝试以下操作:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <VideoView
        android:id="@+id/vwPlayVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" />

</LinearLayout>

这不会改变视频比例。 要使 VideoView 填满整个屏幕,请尝试以下操作:

<RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

     <VideoView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:alignParentTop="true"
          android:alignParentLeft="true"
          android:alignParentRight="true"
          android:alignParentBottom="true"/>

</RelativeLayout>

关于Android 在不水平拉伸(stretch)视频的情况下将 VideoView 方向从纵向切换为横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590538/

相关文章:

java - 在 Android 中持续运行 Socket.io 服务

安卓NDK。从 .PVR 文件加载 ETC1 压缩纹理

java - 扩展模型的良好实践?

css - 在 iOS 设备上查看 HTML5 视频而无需全屏

python - python中的视频和音频处理库

java - 有没有办法强制应用程序在使用主页按钮后始终从 Activity1 开始?

android - 如何以编程方式在 Android 上录制高速视频 (60-120fps)

java - ffmpeg 在 android 上的视频 http

jquery - 当我从其他页面(分开)再次访问主页时,如何自动使视差网站主页视频声音静音?

java - 用于MainActivity和广播接收器之间通信的接口(interface)