我有一个应用程序可以播放外部存储中的视频。这些视频在纵向模式下看起来不错,但在横向模式下会被拉伸(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/