【发布时间】:2014-10-27 23:51:10
【问题描述】:
我有一个从外部存储播放视频的应用程序。视频在纵向模式下看起来不错,但在横向模式下会被拉伸。有没有简单的方法来设置横向模式视频播放宽度以适应视频?
这是我的视频播放活动:
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!
-
检查我的更新答案@wutk3ks
-
对 VideoView 的宽度和高度都尝试 wrap_content。
-
嘿,谢谢,成功了!还添加了gravity="center" 以更好地对齐它。我认为这很容易,但我认为它会比这更多的代码:P 将其添加为答案,我会接受它。 @wutk3ks