Mobile audio Seventh Day video playback next video function realization, video progress, power change realization

Source: Internet
Author: User

First look at the following:

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/9C/46/wKiom1luKMDAU8mZAAB6leNHsZ4773.jpg-wh_500x0-wm_ 3-wmp_4-s_3245740799.jpg "title=" qq picture 20170718232530.jpg "alt=" Wkiom1lukmdau8mzaab6lenhsz4773.jpg-wh_50 "/>

Here, the video progress bar will be changed according to the video playback, the button below, play the next, the previous has been implemented.

The code is already hosted on the code cloud, and the small partner who wants to download it can get it from the address below.

Https://git.oschina.net/joy_yuan/MobilePlayer


1, in this picture, the custom display of the power of the column has been the system to cover the status bar, the following is said to implement the method:

When the system power changes, the system sends a broadcast, and all activity that is interested in the broadcast can register the broadcast to receive the broadcast. Here receive system power change, screen lock screen, open screen broadcast, can only use dynamic broadcast, can not use static broadcast, this is to prevent in the lock screen, some programs secretly do things, open screen after it is hidden, causing users do not know.

Define a dynamic broadcast receiving the system's power change broadcast, according to the battery level, to dynamically switch the power of the picture, the specific code snippet is as follows:

Register a power broadcast, such as a lock screen, open screen, power changes such as broadcasts, static registered broadcast recipients do not receive the broadcast, because of the security issues, so you must dynamically register Receiver=new myreceiver (); Intentfilter intentfilter= New Intentfilter ();//When the power changes, this broadcast intentfilter.addaction (intent.action_battery_changed); Registerreceiver (receiver , intentfilter);---------------------------------------------------------------------------------------/** * Broadcast receiver listening for power change */class Myreceiver extends broadcastreceiver{@Override public void onreceive (context context, Intent I  ntent) {int Level=intent.getintextra ("level", 0);    Go to intent. Gets the value of the key level, which defaults to 0 setbattery (level); }}

When you receive the battery level, you can dynamically set the power picture

Method of/** *  Power change  *  @param  level */private void setbattery (int level)  {    if  (level<=0) {         Ivbattery.setimageresource (R.DRAWABLE.IC_BATTERY_0);    }else if  (level<=10) {         ivbattery.setimageresource (R.DRAWABLE.IC_BATTERY_10);     }else if  (level<=20) {         Ivbattery.setimageresource (r.drawable.ic_battery_20);    }else if  (level<=40) {        ivbattery.setimageresource (r.drawable.ic_battery_40);     }else if  (level<60) {         Ivbattery.setimageresource (r.drawable.ic_battery_60);    }else if  (level<=80) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;ivbattery.setimageresource (r.drawable.ic_battery_80);    }else if  (level< =100) {        ivbattery.setimageresource (R.drawable.ic_battery_100);     }}


2, the video progress bar changes

Seekbar itself has a setmax (); This is the maximum value for setting the Seekbar, and the total amount of time we can pass in a video. When you start playing the video, pass an empty handler to update the Seekbar method.

/** * Ready to play when listening */class Myonpreparedlistener implements Mediaplayer.onpreparedlistener {@Override public void O  Nprepared (MediaPlayer MP) {Videoview.start ();//start playback duration = Videoview.getduration ();   Get Video Total duration Seekbarvideo.setmax (duration);    Set the maximum playback progress//Send a message to update the video Progress Handler.sendemptymessage (PROGRESS); }}

Then in the handler, according to the handler what type, to do the update progress of the operation. The Videoview.getcurrentposition () method gets the current video progress, and then Seekbar has a Setpositon () to set the progress, last every second, and then send a handler to update the progress.

/** *  defines a Handler to process information  */public handler handler=new handler () {      @Override     public void handlemessage (message msg)  {         super.handlemessage (msg);         switch  (msg.what) {            case  PROGRESS:                 //updates the video progress message according to the updated video progress messages that were sent after the time of playback                  int currentposition=videoview.getcurrentposition ();                 seekbarvideo.setprogress (currentPosition);                 //every second to refresh the progress      &Nbsp;          removemessages (PROGRESS);                 handler.sendemptymessagedelayed ( progress,1000);                 break;        }    }};


3, the system time changes:

In the previous aspect of the video progress bar transformation, the use of handler, in the handler in a second to refresh the handler, so here is also the update system time put in the handler.

/** *  defines a Handler to process information  */public handler handler=new handler () {      @Override     public void handlemessage (message msg)  {         super.handlemessage (msg);         switch  (msg.what) {            case  PROGRESS:                 //updates the video progress message according to the updated video progress messages that were sent after the time of playback                  int currentposition=videoview.getcurrentposition ();                 seekbarvideo.setprogress (currentPosition);                 //set the current playback time, Make it appear on the player &NBSP;&NBSP;&NBSP;&NBSp;            tvcurrenttime.settext ( Utils.stringfortime (currentposition));                 //set the system playback time, a second refresh                  tvsystemtime.settext (GetSystemTime ());                 //one second to refresh your progress                  removemessages (PROGRESS);                 handler.sendemptymessagedelayed (PROGRESS, ;                break;)         }    }};/** *  Get system Time  *   @return  */private strinG getsystemtime ()  {    simpledateformat format=new simpledateformat (" HH:mm:ss ",  locale.getdefault ());     return format.format (New date ());}

It should be noted here that the method of acquiring the system time, the SimpleDateFormat () method, and its package is java.text.SimpleDateFormat; Be aware that the Android system may automatically lead us to other packages resulting in an error


4, to achieve the next video function to play, we have to get a list of video data, then in the videopager we through the bundle, the list is written to the bundle, it should be noted that the entity class must be Mediaitem serialized, That is, the class implements the Serializable interface, otherwise it will result in an error.

In Videopager, pass in list to intent as follows

The object is serialized and uploaded to the Systemvideoplayer Intent intent=new Intent (context, systemvideoplayer.class); Bundle Bundle=new Bundle (); Bundle.putserializable ("Mediallist", (arraylist<mediaitem>) medialist);  Intent.putextras (bundle); Intent.putextra ("position", position); The location of the clicked video is passed and the video context.startactivity (intent) is conveniently positioned to play.

Get the list in the Systemvideoplayer class

Gets the list data from the Videopager, which contains the information for the media listing medialist = (arraylist<mediaitem>) getintent (). Getserializableextra (" Mediallist ");p osition = Getintent (). Getintextra (" position ", 0);


After getting to list and position, it is very convenient to play the previous, next video

Public void onclick (view v)  {    if  ( v ==  btnvoice )  {        // handle clicks for  btnVoice    } else if  ( v == switchPlayer )   {        // handle clicks for switchplayer     } else if  ( v == btExit )  {         // Handle clicks for btExit    Exit          finish ();    } else if  ( v ==  btvideopre )  {        // handle clicks for  btVideoPre   play the previous video         if  (medialist!=null &&medialist.size () >0){            position-=1;             if  (position<=0) {                 position=medialist.size () -1;             }             mediaitem mediaitem=medialist.get (position);             tvname.settext (Mediaitem.getname ());             videoview.setvideopath (Mediaitem.getdata ());         }else if  (uri!=null) {             tvname.settext (Uri.tostring ());             Videoview.setvideouri (URI);        }    } else if  (  v == btvideostartpause )  {        // handle  clicks for btVideoStartPause   play pause and start          if  (Videoview.isplaying ()) {             The video is playing, then pause             videoview.pause ();             //button picture set to pause              btvideostartpause.setbackgroundresource (R.drawable.bt_video_play_ Selector);        }else{             //video on pause, set to play              videoview.start ();             //button picture set to play              btvideostartpause.setbackgroundresource (R.drawable.bt_video_pause_ selector);             }    }  else if  ( v == btNext )  {         // Handle clicks for btNext    Play Next      if   (Medialist!=null&&medialist.size () >0) {          position+=1;         if  (Position>=medialist.size ()) {              position=0;          }              mediaitem mediaitem&Nbsp;= medialist.get (position);              tvname.settext (Mediaitem.getname ());              videoview.setvideopath (Mediaitem.getdata ());     }else if  (uri! =null) {         //Sets the previous Next button gray           tvname.settext (Uri.tostring ());          Videoview.setvideouri (URI);          btnext.setenabled (false);          btnext.setbackgroundresource (R.drawable.btn_next_gray);      }    } else if  ( v ==  btvideoswitchscreen )  {        // handle clicks  for btvideoswitchscreen    }} 

What you do here is loop play, that is, after playing to the last one, press Next and the first video will be played.

This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1948770

Mobile audio Seventh Day video playback next video function realization, video progress, power change realization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.