Android development uses VideoView to implement horizontal video playback, border removal, and androidvideoview
A guiding video needs to be played for the project. I thought it was very simple. When I started the project, I found that there was always a flaw. Fortunately, du Niang, the gains are summarized as follows:
I. Video Playback:
It indicates that I want to play the Resources file in the Android project, instead of accessing the SD card to play the video.
1. Put the video to be played in the res/raw directory.
2. The video format must be supported by Android (3gp, mp4, and wmv). It is said that swf is not supported and has not been tried yet. The video file name cannot contain uppercase letters, but must contain lowercase letters, numbers, or underscores.
3. Add the VideoView component to the layout file.
4. Create a String type object and save the uri
5. Call the setVideoURI method of VideoView to set the URI. The parameter is the uri above.
6. Call the start () method to play the video.
The Code is as follows:
Add the following content to the layout file:
<VideoView android:id="@+id/video" android:layout_width="match_parent" android:layout_height="match_parent" />
Set in Activity:
private void playVideo() {video = (VideoView) findViewById(R.id.video);String uri = "android.resource://" + getPackageName() + "/" + R.raw.guide;video.setVideoURI(Uri.parse(uri));video.start();}
The above code implements video playback, but the video is not played in a horizontal screen or full screen as we want, so the second step is required:
2. full screen video playback (remove the border at the bottom ):
To configure VideoView parameters, you need to use the setLayoutParams method. But we can see that VideoView does not have this method when viewing the API. What should we do?
We need to wrap a layout outside the VideoView component in the layout file. Here we use RelativeLayout, and others should also be:
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <VideoView android:id="@+id/video" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
After the parameter is added, you can configure the parameters:
String uri = "android.resource://" + getPackageName() + "/" + R.raw.guide;video.setVideoURI(Uri.parse(uri));RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT);layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);video.setLayoutParams(layoutParams);video.start();
The four addRule Methods eliminate borders, and the LayoutParams constructor achieves full screen.
After the previous step is completed, it is found that although the video can be full screen, the display is completely distorted, not the desired effect. If the mobile phone Enables automatic screen conversion, we will find that the video playing effect after the mobile phone is switched to the landscape screen is what we want. How can I enable the video to be automatically played on a horizontal screen during playback?
3. Set horizontal video playback:
Whether or not each Activity allows screen conversion. The current display method is landscape screen or landscape screen. You can set it in the AndroidManifest. xml file:
For example, when I play a video in the activity Splash, I can add the screenOrientation attribute to her tag to control the screen direction.(Landscape is horizontal, and portrait is vertical):
<activity android:name="com.zsx.activity.Splash" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:screenOrientation="landscape"> </activity>
Sometimes we want some pages to only have portrait or landscape screens. If we worry that the layout will become ugly after the screen is switched, we can set this attribute in the response activity, the user's screen conversion will not change any more! Haha