iPhone开发笔记(2)MPMusicPlayerController和MPMediaPickerController打造如iPod功能类似的播放器
在上一篇笔记中提到了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
补充:移动开发 , IOS ,