如何从iPhone的iTunes库中播放歌曲
问题描述:
嗨,我需要从iTunes库中播放歌曲。我已经通过了“苹果ipod库访问指南”并获得了代码。如何从iPhone的iTunes库中播放歌曲
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSLog(@"Logging items from a generic query...");
NSArray *itemsFromGenericQuery = [everything items];
MPMediaItem *song;
for (song in itemsFromGenericQuery)
{
NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
NSLog (@"%@", songTitle);
}
//assign a playback queue containing all media items on the device
[myPlayer setQueueWithQuery:everything];//setQueueWithQuery:everything];
//start playing from the begining
[myPlayer play];
但是这将从库列表最开始播放。当我从列表中选择时,我需要播放一首歌曲。谁能帮我请...
谢谢, 石斌。
答
使用MPMediaPickerController
实例,您可以从iPod库的歌曲列表,专辑列表等中进行选择。以下是一个选择iPod中所有歌曲并显示在模式视图控制器中的示例。
- (IBAction) selectSong: (id) sender
{
MPMediaPickerController *picker =
[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = NO;
picker.prompt = NSLocalizedString (@"Select any song from the list", @"Prompt to user to choose some songs to play");
[self presentModalViewController: picker animated: YES];
[picker release];
}
现在你需要实现委托来存储歌曲到你的本地变量。这里,selectedSongCollection
是MPMediaItemCollection
的一个实例。
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
[self dismissModalViewControllerAnimated: YES];
selectedSongCollection=mediaItemCollection;
}
您与选择的歌曲完成后,实现委托驳回选择器:
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker
{
[self dismissModalViewControllerAnimated: YES];
}
答
您正在为音乐播放器分配所有歌曲的播放列表,因此它当然会从头开始播放整个列表。如果您希望用户从iPod库中选择特定的歌曲,请使用MPMediaPickerController
。
答
在我的情况我不能使用MPMediaPickerController
。
我简短回答的问题就是看看[musicplayer setNowPlayingItem:item]
这里是从我下面执行一些代码。
// Create a new query
MPMediaQuery *query = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *mpp = [MPMediaPropertyPredicate predicateWithValue:@"a" forProperty:MPMediaItemPropertyTitle comparisonType:MPMediaPredicateComparisonContains];
[query addFilterPredicate:mpp];
// Retrieve the results and reload the table data
DATAENV.songCollections = [NSMutableArray arrayWithArray:query.collections];
//populate cell rows with
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MPMediaItem *item = [[[DATAENV.songCollections objectAtIndex:indexPath.row] items] lastObject];
titleLbl = [item valueForProperty:MPMediaItemPropertyTitle];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MPMediaItem *item = [[[self.songCollections objectAtIndex:indexPath.row] items] lastObject];
[PLAYER setNowPlayingItem:item];
[PLAYER play];
}
凡PLAYER/DATAENV是我的单身
#define PLAYER [[AudioController sharedAudioController_instance] musicPlayer]
#define DATAENV [DataEnvironment sharedDataEnvironment_instance]
我很抱歉。但什么是选择宋体?它是什么类型的? – crazyoxygen 2011-07-29 05:06:38
@crazyoxygen:正如我在答案中已经提到的那样,selectedSongCollection是MPMediaItemCollection的一个实例。 – 2011-07-29 06:08:59
我可以结合本地通知声音吗? – crazyoxygen 2011-07-29 09:19:29