`
836811384
  • 浏览: 549032 次
文章分类
社区版块
存档分类
最新评论

AVAudioPlayer播放人iPod列表中选取的歌曲

 
阅读更多

有两种方式在代码中去播放从ipod列表中选取的歌曲

1, 使用AVAudioPlayer, 代码如下:

先弹出选曲界面

MPMediaPickerController *mpPickerController = [[MPMediaPickerControlleralloc] initWithMediaTypes:MPMediaTypeAnyAudio];

[selfpresentViewController:mpPickerController animated:YEScompletion:^{}];

mpPickerController.delegate =self;

[mpPickerControllerrelease];



然后实现它的代理方法

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {

NSLog(@"select mediaItemCollection.items=%@, representativeItem=%@", mediaItemCollection.items, [mediaItemCollection representativeItem]);

// 选到的歌曲

MPMediaItem *selectedSong = [[mediaItemCollectionitems] objectAtIndex:0];

NSURL *songURL = [selectedSongvalueForProperty:MPMediaItemPropertyAssetURL]; // 歌曲的URL通常为ipod-library://item/item.mp3?id=-7350999950458851124

NSString *songTitle = [selectedSongvalueForProperty:MPMediaItemPropertyTitle]; // 歌曲的Title

NSString *songURLString = [songURL absoluteString]; // 转换为NSString

NSLog(@"songURL=%@, songURLString=%@, songTitle=%@", songURL, songURLString, songTitle);

// 进入到使用AVAudioPlayer的界面

[selfdismissViewControllerAnimated:YEScompletion:^{

GameShowsViewController *gameShowsVC = [[GameShowsViewControlleralloc] initWithNibName:[NSStringstringWithFormat:@"%@", (ISIPAD?@"GameShowsView_IPAD":@"GameShowsView")]bundle:nil];

gameShowsVC.selectedMusicInfo = [NSDictionarydictionaryWithObjectsAndKeys:

songURLString,@"SongName",

@"NoPic.png", @"Picture",

@"Heqin", @"Player",

songTitle,@"SongTitle",

nil];

[self.navigationControllerpushViewController:gameShowsVC animated:NO];

[gameShowsVCrelease];

}];

}


// 在播放界面中使用,
上面把NSURL转成了NSString, 接下来可以把这个NSString再回转成NSURL,然后再进行播放

NSURL *fileURL = [NSURLURLWithString:fileName];

bgMusicPlayer = [[AVAudioPlayeralloc] initWithContentsOfURL:fileURLerror:nil];

即可以进行播放

2, 另一种方式是使用 MPMusicPlayerController

MPMusicPlayerController *appMusicPlayer = [MPMusicPlayerControllerapplicationMusicPlayer];

[appMusicPlayer setShuffleMode:MPMusicShuffleModeOff];

[appMusicPlayer setRepeatMode:MPMusicRepeatModeNone];


[appMusicPlayersetQueueWithItemCollection:mediaItemCollection]; // 这里的参数mediaItemCollection直接来自于代理方法,-(void)mediaPicker:(MPMediaPickerController*)mediaPicker didPickMediaItems:即可。

[appMusicPlayerplay];




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics