[英]Android Landscape VideoView in portrait mode
I have an activity which should display a VideoView in landscape, but the activity itself must not be in landscape mode, so this is what i have. 我有一个活动,该活动应该以横向显示VideoView,但是活动本身不能处于横向模式,所以这就是我所拥有的。
<activity
android:name=".gui.VideoPlayer"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
</activity>
<?xml version="1.0" encoding="utf-8"?>
Activity 活动
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<VideoView
android:id="@+id/myvideoview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
</LinearLayout>
(The activity itself must remain in portrait mode because the device will be put in a case which covers the navigation and statusbars, but in landscape mode it wont cover them any more) (活动本身必须保持为纵向模式,因为设备将放置在覆盖导航和状态栏的外壳中,但是在横向模式下,它将不再覆盖它们)
I know this is not the optimal answer but I had the same issue a while ago and found a pretty convenient workaround that might help you too. 我知道这不是最佳答案,但前一段时间我遇到了同样的问题,并且找到了一个非常方便的解决方法,它也可能会对您有所帮助。
Try to use a TextureView instead of VideoView because when you rotate it, the content inside rotates as well. 尝试使用TextureView而不是VideoView,因为旋转它时,里面的内容也会旋转。
<TextureView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_texture_view"
android:rotation="90"
android:scaleX="1.8"
/>
Next you'll have to create a SurfaceTextureListener and set it to your view like below: 接下来,您必须创建一个SurfaceTextureListener并将其设置为您的视图,如下所示:
TextureView.SurfaceTextureListener mTextureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
Surface s = new Surface(surfaceTexture);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(currentPageVideoUrl);
mMediaPlayer.setSurface(s);
mMediaPlayer.prepare();
}catch (Exception e){e.printStackTrace();}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
};
And now just set the listener to your TextureView: 现在,只需将侦听器设置为您的TextureView:
mTextureView.setSurfaceTextureListener(mTextureListener);
All that's left to do is to call this to start the video: 剩下要做的就是调用它来开始播放视频:
mMediaPlayer.start();
BONUS:If you want to make more complex adjustments like adjusting the video aspect ratio You can check this library for more details. 奖金:如果您想进行更复杂的调整,例如调整视频宽高比,则可以查看此库以获取更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.