gpt4 book ai didi

ios4 - 你如何使用 MPMediaItem Property Persistent IS 在 iPhone Music Player Framework 中播放音乐?

转载 作者:行者123 更新时间:2023-12-05 01:32:34 25 4
gpt4 key购买 nike

我的代码成功地为我的整个音乐库编目了歌曲名称和 ID。但是,它实际上不会使用这种方法播放歌曲,并且控制台会显示以下内容:

消息播放状态超时。

消息 nowPlayingItem 超时。

self.musicPlayer = [MPMusicPlayerController applicationMusicPlayer];

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
SongName = [[NSMutableArray alloc] init];
SongItem = [[NSMutableArray alloc] init];
NSString *songTitle;
NSString *songID;
//Collect names & ID for entire music library & put into arrays
for (MPMediaItem *song in itemsFromGenericQuery) {
songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
[SongName addObject:songTitle];
songID = [song valueForProperty: MPMediaItemPropertyPersistentID];
[SongItem addObject:songID];
}

NSLog (@"%@", [SongName objectAtIndex:1]);
NSLog (@"%@", [SongItem objectAtIndex:1]);
// Play the second song in the list
MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:[NSArray arrayWithObject:[SongItem objectAtIndex:1]]];
[self.musicPlayer setQueueWithItemCollection:collection];
[self.musicPlayer play];

最佳答案

我再一次回答我自己的问题。问题是 collectionWithItems: 需要一组 MPMediaItems,而不是一组 MPMediaItemPropertyPersistentID。以下是可能遇到相同问题的任何人的工作代码:

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
SongItem = [[NSMutableArray alloc] init];
for (MPMediaItem *song in itemsFromGenericQuery) {
NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
//NSLog (@”%@”, songTitle);
songID = [song valueForProperty: MPMediaItemPropertyPersistentID];
//NSLog (@”%@”, songID);
[SongItem addObject:songID];
}

//Choose the first indexed song
NSString *selectedTitle = [SongItem objectAtIndex:0];

//Use the MPMediaItemPropertyPersistentID to play the song
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:selectedTitle forProperty:MPMediaItemPropertyPersistentID];
MPMediaQuery *mySongQuery = [[MPMediaQuery alloc] init];
[mySongQuery addFilterPredicate: predicate];
[musicPlayer setQueueWithQuery:mySongQuery];
[musicPlayer play];

关于ios4 - 你如何使用 MPMediaItem Property Persistent IS 在 iPhone Music Player Framework 中播放音乐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4570858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com