我需要获取设备上的专辑的全长但却没有得到正确的结果。我所拥有的是使用一张专辑的歌曲获得一个数组:
MPMediaPropertyPredicate *albumNamePredicate = [MPMediaPropertyPredicate predicateWithValue:albumTitle
forProperty: MPMediaItemPropertyAlbumTitle];
MPMediaQuery *myAlbumQuery = [[MPMediaQuery alloc] init];
[myAlbumQuery addFilterPredicate: albumNamePredicate];
songsAlbumList = [myAlbumQuery items];
为了得到一首歌的长度,我用这个:
NSNumber *songTrackLength = [song valueForProperty:MPMediaItemPropertyPlaybackDuration];
int minutes = floor([songTrackLength floatValue] / 60);
int seconds = trunc([songTrackLength floatValue] - minutes * 60);
TracklengthLabel.text = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
所以上面的工作正常,我只是没有正确添加歌曲...任何想法?
答案 0 :(得分:0)
所以我解决了 - 我的问题是我不知道如何正确地用NSNumbers做数学 - 我不知道我在寻找那个,这就是为什么我没有要求它。这是我想出的用于计算设备上相册长度的代码:
- (void)fullAlbumLength
{
for (int i=0; i < songsAlbumList.count; i++)
{
if (addLength == NULL) // addLength and addLengthNew are NSNumber variables
{
addLength = [[self.albumTracksList objectAtIndex:i] valueForProperty: @"playbackDuration"];
}
else
{
addLengthNew = [[self.albumTracksList objectAtIndex:i] valueForProperty: @"playbackDuration"];
addLength = [NSNumber numberWithFloat:([addLength floatValue] + [addLengthNew floatValue])];
}
}
fullminutes = floor([addLength floatValue] / 60); // fullminutes is an int
fullseconds = trunc([addLength floatValue] - fullminutes * 60); // fullseconds is an int
fullLength.text = [NSString stringWithFormat:@"%02d:%02d", fullminutes, fullseconds];
}
希望这对其他人有帮助。