Service Broadcast simple music playing Function

Source: Internet
Author: User

There are two imagebutton types on the activity, which respectively control the playback, pause, and stop.

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Start = (imagebutton) findviewbyid (R. Id. Play );
Stop = (imagebutton) findviewbyid (R. Id. Stop );
Start. setonclicklistener (this );
Stop. setonclicklistener (this );
Activitycycler = new activitycycler (); // broadcast receiver, mainly responsible for changing the playback/pause button icon
Intentfilter Filter = New intentfilter ();
Filter. addaction (" Com. Sunny. Update ");
Registerreceiver (activityreceiver, filter );
Intent intent = new intent (this, myservice. Class );// Start the service. The service is used to control the playing of music and will not stop because the activity exits.
Startservice (intent );
}

Activitycycler is used as follows to convert the playback/pause button icons.Com. Sunny. UpdateIntent:

Public class Activitycycler Extends Broadcastreceiver {
@ Override
Public void onreceive (context, intent ){
// Todo auto-generated method stub
Int update = intent. getintextra ("Update",-1 );
Switch (update ){
Case 1:
Status = 1;
Break;
Case 2:
Start. setimageresource (R. drawable. Pause );
Status = 2;
Break;
Case 3:
Start. setimageresource (R. drawable. Play );
Status = 3;
Break;
}
}
}

It is the registration time of two buttons, and the broadcast control music playing with the sending action com. Sunny. Control is received by the broadcast receiver in the service:

Public void onclick (view v ){
// Todo auto-generated method stub
Intent intent =New intent ("com. Sunny. control" );
Switch (V. GETID ()){
Case R. Id. Play :
Intent. putextra ("action", 1 );
Sendbroadcast (intent );
Break;
Case R. Id. Stop:
Intent. putextra ("action", 2 );
Sendbroadcast (intent );
Break;
}
}

In myservice, The oncreate () method at startup is as follows:

Public void oncreate (){
// Todo auto-generated method stub
Status = 1; // The Current Status. 1 has no sound. 2 is playing. 3 is paused.
Servicereceiver = newServicereceiver();//Broadcast receiver to control music playback
Intentfilter filter = new intentfilter ();
Filter. addaction ("Com. Sunny. Control");
Registerreceiver (servicereceiver, filter );
Super. oncreate ();
}

The broadcast receiver servicereceiver is as follows:

Class servicereceiver extends broadcastreceiver {

@ Override
Public void onreceive (context, intent ){
// Todo auto-generated method stub
IntAction= Intent. getintextra ("action",-1); // filter intent actionCom. Sunny. Control broadcast, controlled according to the Action Parameter
Switch ( Action ){
Case 1:
If (status = 1) {// In the playing status
MP = mediaplayer. Create (context, R. Raw. intro );
Status = 2;
Intent sendintent = new intent ("com. Sunny. Update ");
Sendintent. putextra ("Update", 2 );
Sendbroadcast (sendintent );
MP. Start ();
}
Else if (status = 2) {// In the paused status
MP. Pause ();
Status = 3;
Intent sendintent = new intent ("com. Sunny. Update ");
Sendintent. putextra ("Update", 3 );
Sendbroadcast (sendintent );
} Else if (status = 3) {// play the video again after the pause
MP. Start ();
Status = 2;
Intent sendintent = new intent ("com. Sunny. Update ");
Sendintent. putextra ("Update", 2 );
Sendbroadcast (sendintent );

}
Break;
Case 2:
If (status = 2 | status = 3 ){
MP. Stop ();
Status = 1;
Intent sendintent = new intent ("com. Sunny. Update ");
Sendintent. putextra ("Update", 1 );
Sendbroadcast (sendintent );
}
}
}

}

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.