[Android] simple music player (Introduction to using service and broadcast to play music files)

Source: Internet
Author: User

Use the service to play a song in the sdcard and broadcast to notify the activity update interface in the service. The following three final figures are: initialization, playback, and pause.

There are two buttons on the interface, which use the linearlayout layout. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <button <br/> Android: id = "@ + ID/btnplayorpause" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "play" <br/> Android: layout_weight = "1" <br/> Android: onclick = "clickhandle" <br/> <button <br/> Android: Id = "@ + ID/btnstop" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "stop" <br/> Android: layout_weight = "1" <br/> Android: onclick = "clickhandle" <br/> </linearlayout> 

1. The main activity contains four parts: oncreate () method, ondestroy () method, broadcastreceiver class updateuireceiver, and clickhandl () method for handling button events ().

The oncreate () method registers a broadcast. Be sure to register in oncreate (). Remember to cancel the registration in the ondestroy () method.

@ Override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> btnstartorpause = (button) findviewbyid (R. id. btnplayorpause); <br/> btnstop = (button) findviewbyid (R. id. btnstop); </P> <p> // register broadcastreceiver in oncreate () <br/> // ondestroy () <br/> // The following "giuz. aggreger. music "to be in manifest. register in XML <br/> intentfilter filter = new intentfilter ("giuz. aggreger. music "); <br/> registerreceiver (updatuireceiver, filter); </P> <p>} 

Unregister in the ondestroy () method. Otherwise, an exception is reported when you exit.

@ Override <br/> protected void ondestroy () {<br/> super. ondestroy (); <br/> unregisterreceiver (updatuireceiver); <br/>} 

The broadcast class is defined in the activity (it can also be written as a separate class ).

// Define a broadcastreceiver <br/> private broadcastreceiver updatuireceiver = new broadcastreceiver () {<br/> // when the service sends a broadcast, this method returns the value <br/> @ override <br/> Public void onreceive (context, intent) {<br/> // update interface. Here, change the value of the button <br/> // get the value returned by intent. 0 indicates playing, 1 indicates paused, and 2 indicates stopped. <br/> int backflag = intent. getextras (). getint ("backflag"); <br/> switch (backflag) {<br/> case 0: <br/> btnstartorpause. settext ("pause"); <br/> break; <br/> case 1: <br/> case 2: <br/> btnstartorpause. settext ("play"); <br/> break; <br/>}< br/> }; 

Button Processing Event

// Process button events <br/> Public void clickhandle (view v) {<br/> switch (v. GETID () {<br/> case R. id. btnplayorpause: <br/> intent = new intent (audioacti. this, giuz. service. myaudioservice. class); <br/> bundle bundle2service = new bundle (); <br/> bundle2service. putstring ("audiopath", audio_path); // The audio_path to be defined earlier <br/> // bc_receiver must also be defined earlier, and in manifest. register in XML <br/> bundle2service. putstring ("bc_receiver", bc_receiv Er); <br/> intent. putextras (bundle2service); <br/> startservice (intent); // enable the Service <br/> break; <br/> case R. id. btnstop: <br/> If (intent! = NULL) {<br/> stopservice (intent); // stop the service <br/>}< br/> break; <br/>}< br/>} 

2. There are several methods in service: onstart (), ondestroy (), and sendbc4updateui.

Define the following objects first

Private mediaplayer = NULL; <br/> private intent intent2bc = NULL; <br/> private bundle bundle2bc = NULL; <br/> private string audiopath = NULL; <br/> private string bc_receiver = NULL; 

In the onstart () method, you can use the mediaplayer. isplaying () method to determine whether the current music is playing or paused, and use broadcast to pass the corresponding value to the activity to update the interface.

@ Override <br/> Public void onstart (intent, int startid) {<br/> super. onstart (intent, startid); <br/> audiopath = intent. getextras (). getstring ("audiopath"); <br/> bc_cycler = intent. getextras (). getstring ("bc_receiver"); <br/> // 1. playing <br/> // pause the video, and change the value of the button to "play" on the notification page. (If the video is being played, the value of the button is "pause ") <br/> If (mediaplayer! = NULL & mediaplayer. isplaying () {<br/> mediaplayer. pause (); <br/> sendbc4updateui (1); // update the interface <br/>}< br/> // 2. suspending <br/> else {<br/> If (mediaplayer = NULL) {<br/> mediaplayer = new mediaplayer (); // if it is stopped, null <br/> try {<br/> mediaplayer. setdatasource (audiopath); // set the path of the file to be played <br/> mediaplayer. prepare (); <br/>}catch (illegalargumentexception e) {<br/> E. printstacktrace (); <br/>} catch (illegalstateexception e) {<br/> E. printstacktrace (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> mediaplayer. start (); <br/> sendbc4updateui (0); // update the interface </P> <p >}</P> <p>} 

In the ondestroy () method, release the mediaplayer.

@ Override <br/> Public void ondestroy () {<br/> If (mediaplayer! = NULL) {<br/> mediaplayer. release (); // release required when stopped <br/> sendbc4updateui (2); // update interface <br/>}< br/> super. ondestroy (); <br/>} 

In the sendbc4updateui () method, broadcast is sent.

Private void sendbc4updateui (INT flag) {<br/> intent2bc = new intent (bc_receiver); // bc_receiver is defined previously, <br/> // if the following sentence is missing, turn it off and re-open the player and click "stop". <br/> intent2bc. setflags (intent. flag_activity_new_task); <br/> bundle2bc = new bundle (); <br/> bundle2bc. putint ("backflag", flag); // pass the flag back <br/> intent2bc. putextras (bundle2bc); <br/> sendbroadcast (intent2bc); // send the broadcast <br/> // onreceiver () of the updateuireceiver in the activity after the broadcast is sent () method to update the interface <br/>} 

3. Finally, register manifest. XML as follows.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> .... <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <service android: Name = "giuz. service. myaudioservice "> </service> <br/> <activity .... <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "8"/> <br/> </manifest> 

Later, you can make an MP3 player through improvement.

From: http://www.cnblogs.com/giuz/archive/2010/10/31/1865470.html

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.