Since it's the first Android app, it's all about Hello world.
1. New Android Project
2. Enter the project name
3. Select the Platform version
4. Select an empty activity
5. Customize your Activity
After clicking Finish, a Hello World program is created that can run directly. Here's how to make this program that can only print Hello world to play local and network video.
The functional directory structure is attached here:
6. Layout files
First you need to re-layout. The design result of the designer is saved in the XML file "Activity_video_view_demo.xml", so it's easy to read the XML file a little bit longer.
<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: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" Android :p addingtop= "@dimen/activity_vertical_margin" android:paddingbottom= "@dimen/activity_vertical_margin" tools: Context= ". Videoviewdemo "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content " Android:textappearance= "? Android:attr/textappearancelarge" android:text= "= Stream Player =" android:i D= "@+id/textview" android:layout_alignparenttop= "true" android:layout_alignparentleft= "true" Android: Layout_alignparentstart= "true" android:layout_alignparentright= "true" android:layout_alignparentend= "true" Android:textisselectable= "false" Android:layout_Alignparentbottom= "false" android:gravity= "Center_horizontal"/> <edittext android:layout_width= "WR Ap_content "android:layout_height=" wrap_content "android:id=" @+id/url "android:layout_below=" @+id/tex TView "android:layout_alignparenttop=" false "android:layout_alignparentleft=" true "android:text=" rtsp ://ipaddr:port/domain "android:layout_alignright=" @+id/textview "android:layout_alignend=" @+id/textView "/&G T <radiogroup android:id= "@+id/radiogroup1" android:layout_width= "Wrap_content" Android:layout_heigh t= "wrap_content" android:orientation= "horizontal" android:layout_below= "@+id/url" Android:layout_alig Nparentleft= "true" android:layout_alignparentstart= "true" android:layout_alignparenttop= "false" > & Lt RadioButton android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android Oid:text= "StreaM "android:id=" @+id/radiobuttonstream "android:layout_below=" @+id/url "Android:layout_ali Gnparentleft= "true" android:layout_alignparentstart= "true" android:checked= "false" Androi d:layout_alignbottom= "@+id/start_play"/> <radiobutton android:layout_width= "Wrap_content" android:layout_height= "51DP" android:text= "File" android:id= "@+id/radiobuttonfile" an Droid:checked= "false" android:layout_alignbottom= "@+id/radiobuttonstream" android:layout_torightof= " @+id/radiobuttonstream "android:layout_below=" @+id/url "/> </RadioGroup> <button Andro Id:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "PLAY" android:id= "@ +id/start_play "android:layout_below=" @+id/url "android:layout_alignright=" @+id/url "android:layout_a Lignend= "@+id/url" android:layout_torightof= "@+id/radiogroup1" android:layout_toendof= "@+id/radiogroup1"/> <VideoView Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:id= "@+id/rtsp_player" android:layout_below= "@+id/start_play" android:layout_alignright= "@+id/url" android:layout_alignend= "@+ Id/url "/></relativelayout>
Layout Designer Effects:
In contrast to an XML file, the outermost layer is a relational layout. It is TextView (used to display the title), EditText (the URL used to enter the file name or stream), and a radiogroup that contains two radiobutton (used to differentiate between the area network stream or the reader file) ; a button (click Start playback); a videoview (video playback area).
The layout_width can be wrap_content (according to the content Adaptive), fill_parent (fill the entire outer space), match_parent (according to the external space adaptive), this when the video playback area loaded video will see the difference. Android:id This property is very important and is the key to invoking layout controls in Java programs.
7. Source code
Package Com.example.chenth.videoviewdemo;import Android.app.activity;import Android.net.uri;import Android.os.bundle;import Android.os.environment;import Android.view.view;import Android.widget.Button;import Android.widget.edittext;import Android.widget.radiobutton;import Android.widget.videoview;public Class Videoviewdemo extends activity {/** Called when the activity is first created. */Button PlayButton; Videoview Videoview; EditText Rtspurl; RadioButton Radiostream; RadioButton Radiofile; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_video_view_demo); Rtspurl = (EditText) This.findviewbyid (R.id.url); PlayButton = (Button) This.findviewbyid (R.id.start_play); Radiostream = (RadioButton) This.findviewbyid (R.id.radiobuttonstream); Radiofile = (RadioButton) This.findviewbyid (r.id.radiobuttonfile); Playbutton.setonclicklistener (New BUtton. Onclicklistener () {public void OnClick (View v) {if (radiostream.ischecked ()) { Playrtspstream (Rtspurl.geteditabletext (). toString ()); } else if (radiofile.ischecked ()) {Playlocalfile (Rtspurl.geteditabletext (). toString ()); } } }); Videoview = (videoview) This.findviewbyid (R.id.rtsp_player); }//play rtsp stream private void Playrtspstream (String rtspurl) {Videoview.setvideouri (Uri.parse (Rtspurl)); Videoview.requestfocus (); Videoview.start (); }//play rtsp stream private void Playlocalfile (String filePath) {Videoview.setvideopath (environment.getexter Nalstoragedirectory () + "/" + FilePath); Videoview.requestfocus (); Videoview.start (); }}In the CREATE function on Eng gets the control from the layout according to the ID, and when PlayButton clicks, it calls different functions according to the RadioButton selection (taking the network stream or playing the local file). (primarily using Android native Videoview, this control plays the network stream using the Videoview.setvideouri () function, which plays local files using the Setvideopath () function).
8. Add Permissions
When debugging this program, there is always a "can ' t play this video" or "Can't Play it" error, I also spent a lot of time, and finally found that the app is not given access to the network and local files permissions. The permissions are configured in the Androidmanifest.xml file.
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "/http Schemas.android.com/apk/res/android "package=" Com.example.chenth.videoviewdemo "> <uses-permission android: Name= "Android.permission.INTERNET"/> <uses-permission android:name= "android.permission.WRITE_EXTERNAL_ STORAGE "/> <application android:allowbackup=" true "android:icon=" @mipmap/ic_launcher "Androi D:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= ". Videoviewdemo "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHE R "/> </intent-filter> </activity> </application></manifest>
Note the two lines of "Uses-permission", which indicate that the application needs to access the network and read and write local files, which correspond to the playback of network streams and local files.
After adding these two lines, when you pack the installation, you can see the app's prompt to request permission from the Android system.
9. Debugging on the simulator
Click on the Run app and you'll see a window to select your device, and a new one if you don't have a ready-made device.
Because I do not know where the root directory of the simulator is located on the PC, it is not possible to test the local files directly in the simulator. In addition, the test play network stream in the simulator is not successful, guess should be the emulator permissions are not set. So go straight to the last step.
10. Packaging and Publishing
Click on the menu bar, build, Generate signed APK. The first time you use it, you will need to create a key and create it. And then all the way to next. Finally copy the generated apk file to the phone, install, done!
Finally on the phone screenshot effect: One is to play the RTSP live streaming, the other is my small friends of the video.
11. Attached: The playback buffer size of Android native Videoview seems to be unable to adjust, which causes the delay of the network RTSP stream to reach 9-10 seconds. Everyone crossing if know what good way, welcome message to inform, thank you!
Android Studio, Videoview-based local files and streaming media Player