Android-an extremely simple music player (service usage)

Source: Internet
Author: User

The club's homework has been dragging on for a week, so I am sorry to stop it... I started my head the night before. It was always unable to continue coding because the resources were too large to run .. Today, I accidentally deleted the music Resources in the previous project and added another music ~ To avoid re-running, I added two music and switched back and forth to fulfill the basic requirements ~ It will take a long time for the resource to be too large to wait for it to be installed... Life is really a long waiting process... Let's talk about the problem ~

1. The first job is ps .. It indicates that after learning ps, it seems that there is obsessive-compulsive disorder... No matter how the program is compiled... The interface cannot be ugly .. Otherwise, you will not be in the mood... Common problems for girls... At the beginning, I made a horizontal screen ~ Because the beautiful images are horizontal ~ But there was a problem later .. The music list is obviously better-looking, so you cannot see the Starting Screen horizontally and then put the screen up .. So I had to discard the initial PS results .. Convert it to a vertical one .. The figure is as follows:

Before conversion:



After conversion:



Pretty good ~ At that time, I wanted to turn the horizontal chart into a vertical chart and thought of various methods .. Search on the Internet .. Fortunately, the background color is relatively simple .. After I cut a bit of the above cloud replication, the integration is particularly poor .. I am not satisfied with the method of searching for merged images .. In the end, we are all grief-stricken .. Our God Wu has appeared ~! I listened to my questions .. It takes one second .. He .. Done .. It makes me very ashamed .. It took so long to find the materials .. It's really easy for people to do this .. To sum up, it is better to find Wu Hao for information .. Hey hey ~ The method he used is very simple. Select the above stretch (CTRL + T) and it will be OK ~
Orz ....

2,Then I want to add an animation (Question Time ~!) to the background ~!)~ However, I do not know how to add the information .. After all, the background is configured in XML, and this object is not available .. Eventually, the background is converted into an imageview ~ Let's talk about this imaginview .. I like it very much ~ The imagebutton button is used for playing at the beginning, but the layout_width attribute is used to adjust the size of the button, and the figure is the original size .. Great! Are not complete .. Then I changed it to imageview .. However, there is no orange feedback ..So I still want to know how to adjust the image size in imagebutton .. (Quesion
Time again ~!)
I used PS to make the image smaller .. Good low-end...

3. Intent problems .. Since the initial image is created with an imageview and is transparent, you can directly switch to the second activity,However, if I write startintent directly after the animation effect, the initial screen is the second activity .. (Qustion time ...)I don't know what's going on .. I added the onclick event to imageview ..

4. When playing music,I hope that an image will always be a dynamic swinging effect .. (Q ..)However, I do not know how to write the animation effect in the XML file in anim .. Rotate can only be set to the initial and end angles. I want to set two rotate parameters to alternate .. Then let the circle be rotated .. The results are similar .. But I really don't know how to swing...

5. Delete the previous program every time you run it! The memory of your mobile phone is already a small simulator! I have to delete it to run it again! Indicates that this kind of non-focus is achieved by each programming process (waiting for simulators and so on ..) It takes most of the time .. A waste of time...

6. It cannot be paused during later playback. Pause is indeed called when a pile of information is Output Using Log logs .. It's been around for a long time .. The result is that the onstart of the Service will be called every time I click the button, and it will be created once .. An error occurs .. Therefore, a flag variable is set to create only when the resource is obtained for the first time. If you pause and play again, you will not call create again...


Let's finish the problem .. Let me talk about this idea .. The first step is to enter the page with an animation effect. There is only one imageview. When you click it, you can enter the music list listview to select which song to play .. The putextra key-value pairs in intent are sent to the activity that plays the music according to the selected ID .. In a music activity, different la s are used based on the values transmitted by intent. In fact, the artist's pictures are different .. Then, after obtaining the intent and clicking the putextra button (playing or pausing ..) To the service .. After the Service determines, it calls different methods to operate the mediaplayer object... At the same time, the intent also contains information about the artist from listview (obtained from the first intent .. I was trying to use the previous intent, but it didn't work .. That is the intent passed from the listview to the playback interface. It won't let me continue to use it ..), You can get the music resource...


Below and code ~~ The layout file code will not be passed .. The listview is actually a little troublesome .. You also need to write a layout for it .. It looks nice to see it ~~









Dynamic effects cannot be captured .. That is, rotate a circle .. Nothing nice .. ...




Paste the code of the activity and service in the playlist ~~ These two are critical ~~

Package COM. musicplayer; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. menu; import android. view. menuitem; import android. view. view; import android. view. view. onclicklistener; import android. view. animation. animation; import android. view. animation. animationutils; import android. widget. imageview; public class musicplay extends activity {private animation anim; private imageview PIC; private imageview last; private imageview next; private imageview pause; private imageview play; private imageview stop; private intent = new intent (); Private onclicklistener playlistener = new onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubintent. putextra ("choice", "play"); intent. setclass (musicplay. this, musicservice. class); startservice (intent) ;}}; private onclicklistener pauselistener = new onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubintent. putextra ("choice", "pause"); intent. setclass (musicplay. this, musicservice. class); startservice (intent) ;}}; private onclicklistener stoplistener = new onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubintent. putextra ("choice", "stop"); intent. setclass (musicplay. this, musicservice. class); startservice (intent) ;}}; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); intent oldintent = getintent (); string singer = oldintent. getstringextra ("name"); If (Singer. equals ("Taylor") {setcontentview (R. layout. taylor); PIC = (imageview) findviewbyid (R. id. taylor); intent. putextra ("Singer", "Taylor");} else if (Singer. equals ("dingdang") {setcontentview (R. layout. dingdang); PIC = (imageview) findviewbyid (R. id. dingdang); intent. putextra ("Singer", "dingdang");} anim = animationutils. loadanimation (this, R. anim. picture); pic. startanimation (anim); last = (imageview) findviewbyid (R. id. last); next = (imageview) findviewbyid (R. id. next); pause = (imageview) findviewbyid (R. id. pause); stop = (imageview) findviewbyid (R. id. stop); play = (imageview) findviewbyid (R. id. play); play. setonclicklistener (playlistener); pause. setonclicklistener (pauselistener); stop. setonclicklistener (stoplistener) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// todo auto-generated method stubmenu. add ("exit"); return Super. oncreateoptionsmenu (menu) ;}@ overridepublic Boolean onoptionsitemselected (menuitem item) {// todo auto-generated method stubif (item. getitemid () = 0) {stopservice (intent); finish ();} return Super. onoptionsitemselected (item );}}

package com.MusicPlayer;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;import android.util.Log;public class MusicService extends Service {boolean flag = true;private MediaPlayer player;@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubLog.v("tag", "on bind");return null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Log.v("tag", "on create");}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.v("tag", "on destroy");}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);Log.v("tag", "on start");String choice = intent.getStringExtra("choice");String singer = intent.getStringExtra("singer");Log.v("choice", choice);if(singer.equals("taylor")&&flag){Log.v("tag", "taylor");player = MediaPlayer.create(this, R.raw.jumpthenfall);flag = false;}else if(singer.equals("dingdang")&&flag){Log.v("tag", "dingdang");player = MediaPlayer.create(this, R.raw.caibutou);flag = false;}if(choice.equals("play")){player.start();}else if(choice.equals("pause")){Log.v("tag", "pause");player.pause();}else if(choice.equals("stop")){Log.v("tag", "choice");player.stop();player.release();flag = true;}}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubLog.v("tag", "on start command");return super.onStartCommand(intent, flags, startId);}}

Last .. .. I have not made a switch between songs .. Put the first two .. You don't want to get it .. In fact, when you add a listening event to the button, you can just upload a tag in the intent, or upload the id value from the first upload of the listview activity, next, after the last operation, you can get the value of the ID plus ++ or --, and then pass it to the service ~ It is sent and transmitted .. Now I am sleepy and want to go to bed .. I feel like these little things are going to happen ~ Just learn something different ~ No need to do physical work again ~ Some development projects will be physically active in the future ~~ Wash away ~ \ (Too many rows )/~~~

Related Article

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.