Video Playback is more complex than audio playback. In addition to audio, you also need to consider a visual component. To solve this problem, Android provides a dedicated View Controller android. widget. videoview encapsulates the creation and initialization processes of mediaplayer. This widget can be used in any layout manager and provides many display options, including scaling and coloring. To implement the playback function, we need to create a videoview widget and set it to the content of the user interface, and then set the view path or
Uri and trigger the START () method.
In addition to button controls, video playback also requires a frame for displaying videos. In this example, the videoview component is used to display the video content. Instead of creating our own button control, a mediacontroller is created to provide these buttons (if you need to create another button, you can also create one by yourself ).
Package video. Player. activties;
Import Android. App. activity;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. widget. mediacontroller;
Import Android. widget. videoview;
Public class mainactivity extends activity {
Private videoview;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Videoview = (videoview) This. findviewbyid (R. Id. videoview );
Mediacontroller MC = new mediacontroller (this );
Videoview. setmediacontroller (MC );
Videoview. setvideopath ("/sdcard/music/test.mp4 ");
Videoview. requestfocus ();
Videoview. Start ();
}
}
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Videoview
Android: Id = "@ + ID/videoview"
Android: layout_width = "320dip"
Android: layout_height = "260dip"
> </Videoview>
</Linearlayout>