在上一篇筆記中提到了MPMoviePlayerController的相關用法,用MPMoviePlayerController做出來的播放器效果實在是單調的可憐,雖然我又在工程加入歌曲響應的專輯照片和歌詞但是效果還是不理想。在《iPhone開發秘籍》裡面看到了MPMusicPlayerController類,發現要做一個播放器竟然只需要三行核心代碼,實在是很無語。隨後我又使用了MPMediaPickerController這個類,直接選擇iPod樂庫裡面的歌曲,形成一個歌曲集合供MPMusicPlayerController類播放。下面是我的代碼:
首先在view的viewWillAppear這個方法裡面進行初始化:
1、MPMusicPlayerController提供兩種播放器類型,一種是applicationMusicPlayer,一種是iPodMusicPlayer,這裡用iPodMusicPlayer。
[cpp]
-(void)viewWillAppear:(BOOL)animated{musicPlayerController = [MPMusicPlayerController iPodMusicPlayer];}
2、這個方法是處理點擊播放(暫停)按鈕的方法。其實調用MPMusicPlayerController的play方法就能放歌了。但是要模仿iPod的效果就要實現以下的幾個效果:
(1)專輯封面
(2)歌詞顯示
(3)navigationBar上顯示歌曲名,專輯名,歌手名。
我們寢室的S胖子他的手機是Motorola ME525 Defy,所以他做的是Android2.2下的播放器。他說如果要顯示歌詞和專輯封面的話要獲取MP3文件的後128.我聽了後有點虛,這貌似有點麻煩,不熟悉Java中的流操作,Object-c中有沒有相似的操作。我決定先查查MPMusicPlayerController的Class Reference看看,居然發現iPhone的SDK已經把這些功能寫好了。具體請看代碼的注釋
[c-sharp]
-(IBAction)playORpause{if ([playButton.title isEqualToString:@"Play"]){[musicPlayerController play];playButton.title = @"Pause";MPMediaItem *nowPlayItem = [musicPlayerController nowPlayingItem];//獲取當前播放的歌曲if (nowPlayItem) {MPMediaItemArtwork *artwork = [nowPlayItem valueForProperty:MPMediaItemPropertyArtwork]; //獲取artwork,這裡是為獲取專輯封面做鋪墊NSString *songtitle = [nowPlayItem valueForProperty:MPMediaItemPropertyTitle]; //獲取歌曲標題NSString *albumTitle = [nowPlayItem valueForProperty:MPMediaItemPropertyAlbumTitle]; //獲取專輯標題NSString *artist = [nowPlayItem valueForProperty:MPMediaItemPropertyArtist]; //獲取歌手姓名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 setTextAlignment:UITextAlignmentCenter];[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 nowPlayingItem];if (nowPlayItem) {MPMediaItemArtwork *artwork = [nowPlayItem valueForProperty:MPMediaItemPropertyArtwork];NSString *songtitle = [nowPlayItem valueForProperty:MPMediaItemPropertyTitle];NSString *albumTitle = [nowPlayItem valueForProperty:MPMediaItemPropertyAlbumTitle];NSString *artist = [nowPlayItem valueForProperty:MPMediaItemPropertyArtist];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 setTextAlignment:UITextAlignmentCenter];[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