In fact, the Android MediaPlayer itself can play online video, MediaPlayer combined with Surfaceview play online video is also a good choice (if you do not have performance or user experience on the requirements), about mediaplayer+ Surfaceview Play online video I will give cheese a detailed introduction, here I would like to say a third-party video playback control Vitamio use, this control is very convenient to use, and performance is good, OK, nonsense not much to say, first to see one of our today.
OK, this is a network video, when the video is buffered, the user will be prompted to the current percentage of buffering and the current download speed, we have to see how this effect is achieved.
1. Preparatory work1. Video URL
http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com/D046015255134077DDB3ACA0D7E68D45.flv
2. Download Vitamio Library
Vitamiohttps://github.com/yixia/vitamiobundle
Vitamio website Address Https://www.vitamio.org/?locale=zh-CN
In fact, the official website does not have any eggs, even a demo are not, we just need to go to GitHub download Vitamio library files can be.
3. Import the Vitamio Library
Import the library, this is very simple, and cheese import other libraries in the same way, do not repeat (here are questions welcome message discussion).
4. Reference the library file in the app you've created2. Play Network Video
After the first step of the work is completed, we can start using Vitamio, first in the layout file to refer to Vitamio, as follows:
[Java]View Plaincopy print?
- <io.vov.vitamio.widget.videoview
- android:id="@+id/vitamio"
- Android:layout_width="Match_parent"
- android:layout_height="200DP"
- android:layout_margintop="24DP"/>
Then in the activity to find the Vitamio,vitamio must be initialized before use, call the Vitamio.initialize (this) method to initialize it, the method has a return value indicating whether the initialization is successful, When the initialization is successful, we will do further work. The code is as follows:[Java]View Plaincopy print?
- if (Vitamio.initialize (this)) {
- Videoview Videoview = (videoview) Findviewbyid (R.id.vitamio);
- Videoview.setvideouri (Uri.parse ("http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/ hc.yinyuetai.com/d046015255134077ddb3aca0d7e68d45.flv "));
- Mediacontroller controller = new Mediacontroller (this);
- Videoview.setmediacontroller (Controller);
- Videoview.start ();
- }
Vitamio in the Videoview and the use of Android Videoview basically consistent, is the import package is different, Setvideouri method to set a network playback address, Mediacontroller is a playback controller (this thing is not to be set, look at the requirements), after setting these, directly call Videoview's Start method to play the video (pay attention to add network access rights).
3. Display buffer percentage and download speed
Finish the second part, video playback is no problem, but what if cheese want to show the buffer percentage and download rate during video buffering? This is also simple. First, the buffer percentage and download rate are displayed on two TextView, and the two TextView are shown in the upper left corner of Videoview, so let's see what the full layout file should look like:
[Java]View Plaincopy print?
- <?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"
- tools:context="Lenve.vitamio.MainActivity" >
- <io.vov.vitamio.widget.videoview
- android:id="@+id/vitamio"
- Android:layout_width="Match_parent"
- android:layout_height="200DP"
- android:layout_margintop="24DP"/>
- <textview
- android:id="@+id/buffer_percent"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:layout_aligntop="@id/vitamio"
- android:layout_marginleft="12DP"
- android:textcolor="#e6ff01"/>
- <textview
- android:id="@+id/net_speed"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:layout_aligntop="@id/vitamio"
- android:layout_marginleft="12DP"
- android:layout_torightof="@id/buffer_percent"
- android:textcolor="#04fa00"/>
- </RelativeLayout>
The two controls are then found in the activity:[Java]View Plaincopy print?
- TextView showing the buffer percentage
- Percenttv = (TextView) Findviewbyid (r.id.buffer_percent);
- Show TextView of Download speed
- NETSPEEDTV = (TextView) Findviewbyid (r.id.net_speed);
Then call the two listeners in Videoview, and the first one is as follows:[Java]View Plaincopy print?
- Videoview.setonbufferingupdatelistener (new Mediaplayer.onbufferingupdatelistener () {
- @Override
- public void Onbufferingupdate (MediaPlayer MP, int percent) {
- Percenttv.settext ("buffered:" + percent + "%");
- }
- });
This method represents the listening buffer percentage, and the percent parameter inside indicates the current buffer percentage.
The second listener is as follows:
[Java]View Plaincopy print?
- Videoview.setoninfolistener (new Mediaplayer.oninfolistener () {
- @Override
- Public Boolean oninfo (MediaPlayer MP, Int. What, int extra) {
- switch (what) {
- //Start buffering
- Case Mediaplayer.media_info_buffering_start:
- Percenttv.setvisibility (view.visible);
- Netspeedtv.setvisibility (view.visible);
- Mp.pause ();
- Break ;
- //Buffer End
- Case Mediaplayer.media_info_buffering_end:
- Percenttv.setvisibility (View.gone);
- Netspeedtv.setvisibility (View.gone);
- Mp.start ();
- Break ;
- //Is buffering
- Case mediaplayer.media_info_download_rate_changed:
- Netspeedtv.settext ("Current speed:" + extra + "kb/s");
- Break ;
- }
- return true;
- }
- });
This listener we can use to listen to the entire process of buffering, what parameters indicate the timing of buffering, extra parameter indicates the current download speed, according to what parameters we can determine whether the current buffer or buffer end or buffering, we will start buffering, the top left corner of the two control display , while the player pauses playback, the buffer at the end of the top left corner of the two controls hidden, while the player began to play, is buffering when we are to display the current download speed.
OK, this is Vatamio a basic usage, Vitamio most of the APIs are consistent with the native Videoview API, I won't go into this.
SOURCE download (due to the CSDN upload file size restrictions, I only uploaded my code module, you download the demo after you need to download Vitamio and introduced, engineering can run) http://download.csdn.net/detail/ u012702547/9510217
Above.
Play the Android online video playback control Vitamio use