问题
THE PLATFORM: Developing in Eclipse using Android SDK 16.
THE PROBLEM: I have a VideoView element that is supposed to fill the entire screen in 480x800 (PORTRAIT ORIENTATION) and it plays fine, but will not orient to portrait. It sticks in landscape mode and the aspect ratio is skewed to fit that way.
WHAT I HAVE ATTEMPTED:
- using android:screenOrientation="portrait" in manifest
- using android:orientation="vertical" in the container and the VideoView itself
- using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); right before I call setContentView
- trying to set the layout width and height to 480dpx800dp instead of fill_parent
- trying to set the VideoView width and height to 480px x 800dp instead of fill_parent
- switching off the auto rotate display
THE XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="480dp"
android:layout_height="800dp"
android:background="#000000"
android:orientation="vertical" >
<VideoView
android:id="@+id/opening_video"
android:layout_width="480dp"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="800dp"
android:orientation="vertical" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/transparent_button"
android:contentDescription="@string/back_button_desc"
android:background="@android:color/transparent" />
</RelativeLayout>
THE CODE:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_start_screen);
VideoView videoView = (VideoView) findViewById(R.id.opening_video);
Uri pathToVideo = Uri.parse("android.resource://com.example.consumerengage/" + R.raw.opening_tablet);
videoView.setVideoURI(pathToVideo);
videoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
videoView.requestFocus();
videoView.start();
// boolean isPlaying = videoView.isPlaying() ;
// videoView.stopPlayback();
// videoView.clearFocus();
addListenerOnButton();
}
THE QUESTION: Nothing I have attempted has been successful. How can I get my 480x800 video to play in portrait orientation?
回答1:
Are you certain your video is is 480x800 and not 800x480? 800x480 is a standard resolution, if you record a video in portrait with a phone the output will still be 800x480 and to have normal playback the video should be turned 90 degrees. What you are describing is what would happen if you try to play a 800x480 video in a 480x800 videoView. If this is the case you could rotate it with video editing software like Windows Live Moviemaker or iMovie if you are on Mac.
回答2:
You are using dp
where you should be using px
:
<VideoView
android:id="@+id/opening_video"
android:layout_width="480px"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="800px"
android:orientation="vertical" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/transparent_button"
android:contentDescription="@string/back_button_desc"
android:background="@android:color/transparent" />
Or, alternatively (and better) - just use match_parent
(or fill_parent
):
<VideoView
android:id="@+id/opening_video"
android:layout_width="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="match_parent"
android:orientation="vertical" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/transparent_button"
android:contentDescription="@string/back_button_desc"
android:background="@android:color/transparent" />
回答3:
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<VideoView
android:id="@+id/opening_video"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/transparent_button"
android:contentDescription="@string/back_button_desc"
android:background="@android:color/transparent" />
</RelativeLayout>
Hope this helps. And also check your manifest file too.
回答4:
Here's what I ended up doing:
The issue was the OS version. If it was ICS (4.0) and above, it would play the video on its side, so upon detecting android.os.Build.VERSION.SDK_INT of 14 or greater, I load the video that is built on its side so it plays the right way.
This is a very silly workaround, and I never really got the answer to my question. The video should not play like that.
来源:https://stackoverflow.com/questions/13116538/android-videoview-not-playing-portrait-orientation