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.
[Cpp]
-(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.
[C-sharp]
-(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)]; imageVie