iphone - MPMediaItemPropertyPersistentID 的 NSNumber 到 NSString 并再次返回

标签 iphone objective-c cocoa-touch mpmediaquery

我正在使用以下代码循环播放 iPhone 音乐库中的所有歌曲:

NSArray * songs = [[NSArray alloc] initWithArray:[[MPMediaQuery songsQuery] collections]];

for (MPMediaItemCollection * item in songs){

    NSString * persistentID = [[[item representativeItem] valueForProperty:MPMediaItemPropertyPersistentID] stringValue];
    // Do something with it.
}

[songs release];

非常基本的东西。

我将 PersistentID 作为 NSString 获取,因为我需要将其写入 XML 文件(以便通过网络传输到另一台设备)。因此,我不能将其保留为 NSNumber

然后,另一台设备将通过再次传回 PersistentID 请求 iPhone 播放轨道。

此时,iPhone 有一个 NSString 它应该播放的轨道的 PersistentID。

再次循环遍历每首歌曲并比较 PersistentID 直到找到我想要的轨道会很麻烦,所以我尝试使用 MPMediaPropertyPredicate 让 iPhone 搜索我。

我正在使用以下代码进行搜索:

MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate predicateWithValue:persistentID forProperty:MPMediaItemPropertyPersistentID];

MPMediaQuery * songsQuery = [[MPMediaQuery alloc] init];
[songsQuery addFilterPredicate:predicate];

if ([[songsQuery items] count]){

    MPMediaItem * item = [[songsQuery items] objectAtIndex:0];
    // Play item.
}

[songsQuery release];

persistentID 是之前的 NSString

奇怪的是,这对某些歌曲有效,对其他歌曲无效。即,有时 items 数组不为空,即使我传递的是 NSString,而不是 NSNumber

我想知道是否有办法将我的 NSString 转换回它来自的 NSNumber,我该怎么做。

更新:我试过 NSNumberFormatter,我也试过类似的方法:

[NSNumber numberWithFloat:[persID floatValue]];

我已经尝试了所有标准方法,但没有成功。

最佳答案

这工作得很好,到目前为止没有问题:

unsigned long long ullvalue = strtoull([persistentID UTF8String], NULL, 0);
NSNumber * numberID = [[NSNumber alloc] initWithUnsignedLongLong:ullvalue];

MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate predicateWithValue:numberID forProperty:MPMediaItemPropertyPersistentID];
[numberID release];

// And so on.

希望这对遇到此问题的其他人有所帮助。

关于iphone - MPMediaItemPropertyPersistentID 的 NSNumber 到 NSString 并再次返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4725674/

相关文章:

iphone - Xcode 4 构建随机卡住

ios - 'NSString' 没有可见的@interface 声明选择器 URLEncodeUsingEncoding

ios - 在 UIView 角上创建 mask

cocoa-touch - 使用 NSFileWrapper 写入与使用 NSFileManager 复制

iphone - UIBarButtonItem 不显示

iphone - 地址可达性 - 服务器和端口 - iOS 5

iphone - 如何在 iPhone SDK 的 TableView 中实现 "Load More Records"?

ios - 如何知道用户之前是否登录过?使用 parse.com

objective-c - 初始化与数据 : expected struct NSData * warning

ios - Objective-C 2.0 中的点运算符