In the previous note, I mentioned the usage of mpmovieplayercontroller. The effect of the player made by mpmovieplayercontroller is really monotonous, although I added album photos and lyrics for song response in the project, the effect was still unsatisfactory. I saw the mpmusicplayercontroller class in "iPhone development cheat book" and found that a player only needs three lines of core code, which is really speechless. Then I used the mpmediapickercontroller class to directly select the songs in the IPOD music library to form a music set for mpmusicplayercontroller class to play. The following is my code:
First, initialize the object in view's viewwillappear method:
1. mpmusicplayercontroller provides two types of players: applicationmusicplayer and ipodmusicplayer. ipodmusicplayer is used here.-(Void) viewwillappear :( bool) animated {musicplayercontroller = [mpmusicplayercontroller ipodmusicplayer];}
2. This method is used to process the click (pause) button. In fact, you can call the play method of mpmusicplayercontroller to play songs. However, to imitate the iPod effect, you must achieve the following effects:
(1) album art
(2) display the lyrics
(3) The navigationbar displays the song name, album name, and singer name.
Our bedroom's fat man's cell phone is Motorola me525 defy, so he is working on a player under android2.2. He said that if you want to display the lyrics and album covers, you need to get the last 128 of the MP3 file. after hearing this, I am a little confused. It seems a little troublesome. I am not familiar with stream operations in Java and whether there are similar operations in object-C. I decided to first check the mpmusicplayercontroller class reference and find that the iPhone SDK has already completed these functions. For more information, see the code comments.-(Ibaction) playorpause {If ([playbutton. title isequaltostring: @ "play"]) {[musicplayercontroller play]; playbutton. title = @ "pause"; mpmediaitem * nowplayitem = [musicplayercontroller nowplayingitem]; // obtain the currently played song if (nowplayitem) {mpmediaitemartwork * artwork = [nowplayitem valueforproperty: Unknown]; // obtain artwork. Here, nsstring * songtitle = [nowplayitem valueforproperty: mpmediaitempropertytitle] is used to obtain the album art. // obtain the song title nsstring * albumtitle = [nowplayitem valueforproperty: album]. // obtain the album title nsstring * artist = [nowplayitem valueforproperty: mpmediaitempropertyartist]; // obtain the artist name uilabel * titlelabel = [[uilabel alloc] initwithframe: cgrectmake (0, 0,200,200)] songtitle = [songtitle stringbyappendingstring: @ "/N"]; songtitle = [songtitle stringbyappendingstring: albumtitle]; songtitle = [songtitle stringbyappendingstring: @ "/N"]; songtitle = [songtitle stringbyappendingstring: artist]; titlelabel. backgroundcolor = [uicolor clearcolor]; [titlelabel setnumberoflines: 3]; [titlelabel sethighlighted: Yes]; [titlelabel updated: Updated]; [titlelabel settextcolor: [uicolor whitecolor]; [titlelabel setfont: [uifont systemfontofsize: 12.0]; [titlelabel settext: songtitle]; self. navigationitem. titleview = titlelabel; [titlelabel release]; uiimage * image = [artwork imagewithsize: cgsizemake (300,200)]; imageview. image = image; self. view. backgroundcolor = [uicolor blackcolor];} return;} If ([playbutton. title isequaltostring: @ "pause"]) {[musicplayercontroller pause]; playbutton. title = @ "play"; mpmediaitem * nowplayitem = [musicplayercontroller playback]; If (nowplayitem) {audio * artwork = [nowplayitem valueforproperty: audio]; nsstring * songtitle = [nowplayitem valueforproperty: required]; nsstring * albumtitle = [nowplayitem valueforproperty: Required]; nsstring * artist = [nowplayitem valueforproperty: Required]; uilabel * titlelabel = [[uilabel alloc] failed: cgrectmake (0, 0,200,200)]; songtitle = [songtitle stringbyappendingstring: @ "/N"]; songtitle = [songtitle encoding: albumtitle]; songtitle = [songtitle stringbyappendingstring: @ "/N"]; songtitle = [songtitle stringbyappendingstring: artist]; titlelabel. backgroundcolor = [uicolor clearcolor]; [titlelabel setnumberoflines: 3]; [titlelabel sethighlighted: Yes]; [titlelabel updated: Updated]; [titlelabel settextcolor: [uicolor whitecolor]; [titlelabel setfont: [uifont systemfontofsize: 12.0]; [titlelabel settext: songtitle]; self. navigationitem. titleview = titlelabel; [titlelabel release]; uiimage * image = [artwork imagewithsize: cgsizemake (300,200)]; imageview. image = image; self. view. backgroundcolor = [uicolor blackcolor];} return ;}}