We will not elaborate on the live broadcasting information here. We should be very familiar with live broadcasting and there are various live broadcasting in our life. How can they achieve it? In fact, we should develop a simple and not simple live broadcasting, you only need two addresses: 1. live video address 2. Player. For live video addresses, we can use a lot of software to connect to them. There are also many open-source players. The most common one is FFMPEG, however, if the development workload of FFmpeg is large, we can use third-party player libraries, such as VLC and vitamio. Here we use the vitamio library.
First, create a project named "live". After the project is established, we need to configure the environment required by vitamio. There are a lot of online environments, so we will not write them out here. After adding the dependency library, add a main interface, here I have added only one editview and button. The configuration is as follows:
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".Live" > <EditText android:id="@+id/live_url" android:layout_width="match_parent" android:layout_height="100dp" /> <Button android:id="@+id/play" android:layout_width="120dp" android:layout_height="60dp" android:layout_below="@id/live_url" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:text="Play" > </Button></RelativeLayout>
Main Interface Class:
package com.jwzhangjie.live;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class Live extends Activity {public static final String DEFAULTPATH = "http://ipadlive.cntv.soooner.com/cctv_p2p_hdcctv6.m3u8";EditText Live_Url;Button PlayBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_live);Live_Url = (EditText)findViewById(R.id.live_url);Live_Url.setText(DEFAULTPATH);PlayBtn = (Button)findViewById(R.id.play);PlayBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(Live.this, JieVideoPlayer.class);String path = Live_Url.getText().toString();if (path == null) {path = DEFAULTPATH;}intent.putExtra("path", path);startActivity(intent);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.live, menu);return true;}}
Playing interface class:
Package COM. jwzhangjie. live; import Io. vov. vitamio. libschecker; import Io. vov. vitamio. mediaplayer; import Io. vov. vitamio. mediaplayer. oncompletionlistener; import Io. vov. vitamio. mediaplayer. oninfolistener; import Io. vov. vitamio. widget. mediacontroller; import Io. vov. vitamio. widget. videoview; import android. annotation. suppresslint; import android. app. activity; import android. content. context; import android. cont Ent. PM. activityinfo; import android. content. res. configuration; import android. media. audiomanager; import android.net. uri; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. util. log; import android. view. display; import android. view. gesturedetector. simpleongesturelistener; import android. view. gesturedetector; import android. view. motionevent; import android. view. view; Import android. view. viewgroup; import android. view. windowmanager; import android. widget. imageview; @ handle ("handlerleak") public class jievideoplayer extends activity implements oncompletionlistener, oninfolistener {private string mpath; private string mtitle; private videoview mvideoview; private view callback; private imageview moperationbg; private imageview moperationpercent; privat E audiomanager maudiomanager;/** sound */private int mmaxvolume;/** current sound */private int mvolume =-1; /** current brightness */private float mbrightness =-1f;/** current scaling mode */private int mlayout = videoview. video_layout_zoom; private gesturedetector mgesturedetector; private mediacontroller mmediacontroller; private view mloadingview; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancesta Te); If (! Libschecker. checkvitamiolibs (this) return ;//~~~ Obtain the stream playback address and logo //~~~ Bind the control setcontentview (R. layout. videoview); mpath = getintent (). getstringextra ("path"); mvideoview = (videoview) findviewbyid (R. id. surface_view); mvolumebrightnesslayout = findviewbyid (R. id. operation_volume_brightness); moperationbg = (imageview) findviewbyid (R. id. operation_bg); moperationpercent = (imageview) findviewbyid (R. id. operation_percent); mloadingview = findviewbyid (R. id. video_loading );//~~~ Bind the event mvideoview. setoncompletionlistener (this); mvideoview. setoninfolistener (this );//~~~ Bind data maudiomanager = (audiomanager) getsystemservice (context. audio_service); mmaxvolume = maudiomanager. getstreammaxvolume (audiomanager. stream_music); If (mpath. startswith ("http:") {mvideoview. setvideouri (URI. parse (mpath);} else {mvideoview. setvideopath (mpath);} // set the display name mmediacontroller = new mediacontroller (this); mmediacontroller. setfilename (mtitle); mvideoview. setmediacontroller (mmediacontroller); mvid Eoview. requestfocus (); mgesturedetector = new gesturedetector (this, new mygesturelistener (); setrequestedorientation (activityinfo. screen_orientation_landscape);} @ overrideprotected void onpause () {super. onpause (); If (mvideoview! = NULL) mvideoview. Pause () ;}@ overrideprotected void onresume () {super. onresume (); If (mvideoview! = NULL) mvideoview. Resume () ;}@ overrideprotected void ondestroy () {super. ondestroy (); If (mvideoview! = NULL) mvideoview. stopplayback () ;}@ overridepublic Boolean ontouchevent (motionevent event) {If (mgesturedetector. ontouchevent (event) return true; // process the gesture to end the switch (event. getaction () & motionevent. action_mask) {Case motionevent. action_up: endgesture (); break;} return Super. ontouchevent (event);}/** gesture end */private void endgesture () {mvolume =-1; mbrightness =-1f; // hide mdismisshandler. removemessages (0); mdism Isshandler. sendemptymessagedelayed (0,500);} private class mygesturelistener extends simpleongesturelistener {/** double-click */@ overridepublic Boolean ondoubletap (motionevent e) {If (mlayout = videoview. video_layout_zoom) mlayout = videoview. video_layout_origin; elsemlayout ++; If (mvideoview! = NULL) mvideoview. setvideolayout (mlayout, 0); Return true;}/** slide */@ suppresswarnings ("deprecation") @ overridepublic Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {float moldx = e1.getx (), moldy = e1.gety (); int y = (INT) e2.getrawy (); display disp = getwindowmanager (). getdefadisplay display (); int login wwidth = Disp. getwidth (); int returns wheight = Disp. getheight (); If (Mold X> rotate wwidth * 4.0/5) // slide onvolumeslide (moldy-y)/rotate wheight) on the Right; else if (moldx <1_wwidth/5.0) // slide onbrightnessslide (moldy-y)/windowheight) on the left; return Super. onscroll (E1, E2, distancex, distancey) ;}/ ** timed hide */private handler mdismisshandler = new handler () {@ overridepublic void handlemessage (Message MSG) {mvolumebrightnesslayout. setvisibility (view. gone) ;}};/*** slide to change the sound size ** @ Pa Ram percent */private void onvolumeslide (float percent) {If (mvolume =-1) {mvolume = maudiomanager. getstreamvolume (audiomanager. stream_music); If (mvolume <0) mvolume = 0; // displays moperationbg. setimageresource (R. drawable. video_volumn_bg); mvolumebrightnesslayout. setvisibility (view. visible);} int Index = (INT) (percent * mmaxvolume) + mvolume; If (index> mmaxvolume) Index = mmaxvolume; else if (index <0) Index = 0; // change sound maudiomanager. setstreamvolume (audiomanager. stream_music, index, 0); // change progress? Viewgroup. layoutparams Lp = moperationpercent. getlayoutparams (); LP. width = findviewbyid (R. id. operation_full ). getlayoutparams (). width * index/mmaxvolume; moperationpercent. setlayoutparams (LP);}/*** slide to change brightness ** @ Param percent */private void onbrightnessslide (float percent) {If (mbrightness <0) {mbrightness = getwindow (). getattributes (). screenbrightness; If (mbrightness <= 0.00f) mbrightness = 0.50f; I F (mbrightness <0.01f) mbrightness = 0.01f; // displays moperationbg. setimageresource (R. drawable. video_brightness_bg); mvolumebrightnesslayout. setvisibility (view. visible);} windowmanager. layoutparams LPA = getwindow (). getattributes (); LPA. screenbrightness = mbrightness + percent; If (LPA. screenbrightness> 1.0f) LPA. screenbrightness = 1.0f; else if (LPA. screenbrightness <0.01f) LPA. screenbrightness = 0.01f; getw Indow (). setattributes (LPA); viewgroup. layoutparams Lp = moperationpercent. getlayoutparams (); LP. width = (INT) (findviewbyid (R. id. operation_full ). getlayoutparams (). width * LPA. screenbrightness); moperationpercent. setlayoutparams (LP) ;}@ overridepublic void onconfigurationchanged (configuration newconfig) {If (mvideoview! = NULL) mvideoview. setvideolayout (mlayout, 0); super. onconfigurationchanged (newconfig) ;}@ overridepublic void oncompletion (mediaplayer player) {log. E ("Tet", "playback completed");} private void stopplayer () {If (mvideoview! = NULL) mvideoview. Pause ();} private void startplayer () {If (mvideoview! = NULL) mvideoview. Start ();} private Boolean isplaying () {return mvideoview! = NULL & mvideoview. isplaying? �� Automatic REPLAY: used to automatically pause and Resume playback */private Boolean needresume; @ overridepublic Boolean oninfo (mediaplayer arg0, int arg1, int down_rate) {Switch (arg1) {Case mediaplayer. media_info_buffering_start?? If (isplaying () {stopplayer (); needresume = true;} mloadingview. setvisibility (view. visible); break; Case mediaplayer. media_info_buffering_end: // The cache is complete. continue playing? If (needresume) startplayer (); mloadingview. setvisibility (view. gone); break; Case mediaplayer. media_info_download_rate_changed: // displays the download speed log. E ("test", "download rate:" + down_rate); // mloadingperce. settext ("buffering ?.. "+" Buffer completed? + Down_rate); // mlistener. ondownloadratechanged (arg2); break;} return true ;}}
Configuration of the playback interface:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <io.vov.vitamio.widget.VideoView android:id="@+id/surface_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <LinearLayout android:id="@+id/video_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center_vertical" > <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/video_loading_perce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="7.0dip" android:text="@string/video_layout_loading" android:textColor="@color/white" android:textSize="20.0sp" /> </LinearLayout> <FrameLayout android:id="@+id/operation_volume_brightness" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#00000000" android:orientation="horizontal" android:padding="0dip" android:visibility="invisible" > <ImageView android:id="@+id/operation_bg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/video_volumn_bg" /> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:paddingBottom="25dip" > <ImageView android:id="@+id/operation_full" android:layout_width="94dip" android:layout_height="wrap_content" android:layout_gravity="left" android:src="@drawable/video_num_bg" /> <ImageView android:id="@+id/operation_percent" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_gravity="left" android:scaleType="matrix" android:src="@drawable/video_num_front" /> </FrameLayout> </FrameLayout></RelativeLayout>
Playback effect: