使用a MPMusicPlayerController是否有办法查询曲目长度?
使用.currentPlaybackTime我可以弄清楚我进入赛道的距离,但我无法知道赛道有多长.
我[MPMediaItem valueForProperty:MPMediaItemPropertyAssetURL]为某些曲目返回null,因此使用AVAsset对我不起作用.
所以我想显示来自 URL 的图像。
我知道我首先需要对其进行友好的 URL 编码 -
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
Run Code Online (Sandbox Code Playgroud)
例如
if var strUrl = nowplaying.data.first?.track.imageurl {
strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
}
Run Code Online (Sandbox Code Playgroud)
但是,我如何才能将其放入 MPMediaItemPropertyArtwork 中。
我尝试了许多使用第三部分翠鸟插件的不同方法 - 但没有成功。
如果有人可以告诉我如何让下图显示那就太好了
http://covers.drn1.com.au/az_B101753_Wish You Were Here_Brain Purist.jpg
我使用的是MPMusicPlayerController打MPMediaItems的MPMediaItemCollection.如何在MPMediaItems完成比赛时发射一个事件?
谢谢!InterDev中
我正在尝试在iOS设备上获取歌曲的歌词,我在网络上找到的示例和stackoverflow显示获得歌曲的MPMediaItem(即使用[MPMediaQuery songsQuery]和MPMediaItemPropertyPersistentID作为谓词)然后检索歌词使用:
[mediaItem valueForProperty:MPMediaItemPropertyLyrics]
Run Code Online (Sandbox Code Playgroud)
问题是,这只有在您首次在iPod音乐应用程序中打开歌曲并查看其中的歌词时才有效.即使您在下次同步时这样做,也可能会再次停止工作.
我们怎样才能获得可靠的歌词访问权限?
我正在尝试播放iPod播放列表中的歌曲.至于播放列表也可能有视频,我想只播放其中的歌曲.
MPMusicPlayerController *mp = [MPMusicPlayerController applicationMusicPlayer];
MPMediaQuery *query = [MPMediaQuery songsQuery];
query.groupingType = MPMediaGroupingPlaylist;
NSArray *playlists = [query collections];
BOOL found = NO;
for(MPMediaPlaylist *playlist in playlists)
{
NSString *name = [playlist valueForProperty:MPMediaPlaylistPropertyName];
if([name caseInsensitiveCompare:@"MyPlaylist"] == NSOrderedSame && [playlist count] > 0)
{
MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:playlist.items];
[mp setQueueWithItemCollection:collection];
found = YES;
NSLog(@"items count %d", [playlist count]);
for (MPMediaItem *item in [collection items]){
NSLog(@"%@", [item valueForKey:MPMediaItemPropertyMediaType]);
}
break;
}
}
Run Code Online (Sandbox Code Playgroud)
在我加载1首歌曲和2个视频的播放列表后,输出为:
items count 3
256
256
1
我不明白为什么我也会收到视频,如果查询是为了检索歌曲,而媒体项目的媒体类型是MPMediaTypeMovie(256或1 << …
我是Swift编程的初学者,他试图重新编写我在网上找到的来自Persistent ID的歌曲.原始代码可在以下网站找到:
我想在Swift 2中编写这段代码(我不会使用Objective-C),但我缺乏翻译它的知识和经验.
代码:
MPMediaItem *song;
MPMediaPropertyPredicate *predicate;
MPMediaQuery *songQuery;
predicate = [MPMediaPropertyPredicate predicateWithValue: MyPersistentIdString forProperty:MPMediaItemPropertyPersistentID];
songQuery = [[MPMediaQuery alloc] init];
[songQuery addFilterPredicate: predicate];
if (songQuery.items.count > 0)
{
//song exists
song = [songQuery.items objectAtIndex:0];
CellDetailLabel = [CellDetailLabel stringByAppendingString:[song valueForProperty: MPMediaItemPropertyTitle]];
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
1)这个代码是否可以重写为Swift,它是否仍然足够流动而没有折旧?
2)我如何使用函数调用此代码?
我正在尝试检索本地 iOS 设备上的所有艺术家,以及对于每个艺术家,该艺术家可用的歌曲数量。
我目前正在以一种直接的方式执行此操作,通过查询所有艺术家和每个艺术家,计算其收藏中的项目(歌曲)数量:
MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query setGroupingType:MPMediaGroupingArtist];
NSArray *collections = [query collections];
for (MPMediaItemCollection *collection in collections)
{
MPMediaItem *representativeItem = [collection representativeItem];
int songs = [[collection items] count];
// do stuff here with the number of songs for this artist
}
Run Code Online (Sandbox Code Playgroud)
但是,这似乎效率不高,或者至少比我预期的要慢。
在有数百位艺术家的演示 iPhone 4 设备上,上述代码运行大约需要 7 秒。当我注释掉获取“收集项目”计数的行时,时间减少到 1 秒。
所以我想知道是否有比我上面做的更快的方法来检索艺术家的歌曲数?
2011 年 9 月 27 日更新。我发现我可以使用以下方法简化艺术家的歌曲计数检索:
int songs = [collection count];
Run Code Online (Sandbox Code Playgroud)
而不是我在做什么:
int songs = [[collection items] count];
Run Code Online (Sandbox Code Playgroud)
然而,实际上这对性能几乎没有影响。
我借了一个 iPhone 3G …
我有以下几点:
MPMediaPropertyPredicate *titlePredicate = [MPMediaPropertyPredicate predicateWithValue:textString
forProperty:MPMediaItemPropertyTitle
comparisonType:MPMediaPredicateComparisonContains];
NSSet *predicateSet = [NSSet setWithObject:titlePredicate];
MPMediaQuery *searchQuery = [[MPMediaQuery alloc] initWithFilterPredicates:predicateSet];
NSArray *itemsFromTextQuery = [searchQuery items];
for (MPMediaItem *song in itemsFromTextQuery)
{
[arrayOfSongItems addObject:song];
}
Run Code Online (Sandbox Code Playgroud)
效果很好,但只搜索了曲目的标题。我希望它返回标题、艺术家和专辑名称的结果。
我试图检索iPod库中歌曲的曲目编号.
基本上我已经查询了所有相册,如下所示:
NSArray *albums = [MPMediaQuery albumsQuery].collections;
Run Code Online (Sandbox Code Playgroud)
检索到一张专辑:
MPMediaItemCollection *album = self.albums[0];
Run Code Online (Sandbox Code Playgroud)
迭代歌曲并尝试记录曲目编号
for (MPMediaItem *mediaItem in self.album.items)
{
NSLog(@"Track Number %@", [mediaItem valueForKey:MPMediaItemPropertyAlbumTrackCount]);
}
Run Code Online (Sandbox Code Playgroud)
但这给了我以下错误
Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<MPConcreteMediaItem 0x1cd818d0> valueForUndefinedKey:]: this class is
not key value coding-compliant for the key albumTrackCount.'
Run Code Online (Sandbox Code Playgroud)
如果我尝试其他键MPMediaItemPropertyTitle,MPMediaItemPropertyAlbumTitle或者MPMediaItemPropertyArtist工作正常.有什么我不知道为什么我无法访问MPMediaItemPropertyAlbumTrackCount.
MPNowPlayingInfoCenter我一直在尝试更新使用中的项目显示的艺术作品,MPMediaItemPropertyArtwork如以下摘自Apple 文档的代码所示
if let image = UIImage(named: "image_here") {
nowPlayingInfo[MPMediaItemPropertyArtwork] =
MPMediaItemArtwork(boundsSize: image.size) { size in
return image
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,这设置了一个较小的图像,如下图红色方块所示。我试图弄清楚如何在较大的黄色方块中设置图像,但我找不到任何区分两者的文档。
编辑
Vint 下面的回答澄清了较小的图标是应用程序图标。然而,我成功地设置了 nowPlayingInfo 字典,因为我的其他元数据正在到达锁定屏幕控件,例如标题(模糊)、持续时间、已播放时间)。这只是艺术品似乎不起作用。
mpmediaitem ×10
ios ×7
iphone ×3
mpmediaquery ×3
swift ×2
audio ×1
avfoundation ×1
events ×1
ios5 ×1
ipod ×1
swift2 ×1
xcode ×1