Category: C #, Android, VS2015;
Date Created: 2016-03-11 I. INTRODUCTION
The examples in this section are similar to the audio playback examples in the previous section, and are the simplest examples, such as not taking into account the phone-breaking situation during video playback and not taking into account complex controls. In short, if you want to further study complex examples, first of all, the core of the simple example to understand, otherwise you even basic design ideas do not know, directly look at complex examples or actual project code is certainly "less", the main reason is that you do not extrapolate, will only copy, You'll faint if you change the function a little bit.
Common ways to achieve video playback are:
1, using Videoview and Mediacontroller class to achieve video playback
Commonly used in layout files with Videoview combined with Mediacontroller to achieve control over it. This section mainly demonstrates its basic usage.
The Videoview control can download video files from a variety of different sources, such as file systems, websites, and play the video. The control provides a variety of control options, such as controlling the size of the video interface, zooming, coloring, and so on.
If finer-grained control is required, it can also be implemented using the MediaPlayer class.
2, using Surfaceview and MediaPlayer class to achieve video playback
This is the approach provided by earlier versions, with more code implementations. Interested in this realization may refer to other information. Ii. Examples of basic usage
1. Operation
The following is the video playback effect that you see when you click the "Play Video under Raw" button.
After you click the Stop playing button, you can continue to click the "Play video under SD card download" button to observe, this is no longer here.
2. Design steps
(1) Add video file
Because it is only for demonstration purposes, the example raw folder and the SD card under the download folder are stored in the same video file (Videoviewdemo.mp4).
For more on how to copy files to an SD card, see the FAQ.
(2) Add Ch2002main.axml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"> <ButtonAndroid:id= "@+id/ch2002_btnrawstart"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Play video under Raw" /> <ButtonAndroid:id= "@+id/ch2002_btnsdcardstart"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Play video under SD card Dwonload" /> <ButtonAndroid:id= "@+id/ch2002_btnstop"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Stop playing" /> <VideoviewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/ch2002_videoview1"Android:layout_margin= "20DP" /></LinearLayout>
(3) Add Ch2002MainActivity.cs
usingAndroid.app;usingandroid.content;usingAndroid.os;usingAndroid.widget;usingandroid.net;namespacemydemos.srcdemos{[Activity (Label="Example 20-2 basic usage of video playback")] [Intentfilter (New[] {intent.actionmain}, Categories =New[] {ch. Mydemoscategory})] Public classch2002mainactivity:activity {videoview mvideoview; Button Btnrawstart, Btnsdcardstart, btnstop; protected Override voidOnCreate (Bundle savedinstancestate) {Base. OnCreate (savedinstancestate); Setcontentview (Resource.Layout.ch2002Main); Mvideoview= findviewbyid<videoview>(RESOURCE.ID.CH2002_VIDEOVIEW1); Btnrawstart= findviewbyid<button>(Resource.Id.ch2002_btnRawStart); Btnsdcardstart= findviewbyid<button>(Resource.Id.ch2002_btnSdcardStart); Uri Rawuri= Uri.parse ($"Android.resource://{packagename}/{resource.raw.videoviewdemo}"); Uri Sdcarduri= Uri.parse ($"{Environment.externalstoragedirectory.name}/download/videoviewdemo.mp4"); Btnrawstart.click+=Delegate{startvideo (Rawuri); }; Btnsdcardstart.click+=Delegate{startvideo (Sdcarduri); }; Btnstop= findviewbyid<button>(Resource.Id.ch2002_btnStop); Btnstop.click+=Delegate{mvideoview.stopplayback (); Changeplayingstate (true,false); }; } Private voidStartvideo (Uri uri) {Mvideoview.setvideouri (URI); Mvideoview.setmediacontroller (NewMediacontroller ( This)); Mvideoview.start (); Changeplayingstate (false,true); } Private voidChangeplayingstate (BOOLStartenabled,BOOLstopenabled) {btnrawstart.enabled= Btnsdcardstart.enabled =startenabled; Btnstop.enabled=stopenabled; } }}
"Android" 20.2 video playback