我需要获得设备上的专辑的全长,但没有正确的结果。我所拥有的是将一张专辑的歌曲纳入阵列:

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];

因此,以上功能正常,我只是没有正确添加歌曲创作...有什么想法吗?

有帮助吗?

解决方案

因此,我解决了它 - 我的问题是我不知道如何与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];
}

希望这对外面的其他人有帮助。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top