Question

J'ai besoin d'obtenir toute la longueur d'un album qui se trouve sur un appareil mais n'obtenez pas le résultat correct. Ce que j'ai, c'est le suivant pour obtenir un tableau avec les chansons d'un album:

MPMediaPropertyPredicate *albumNamePredicate = [MPMediaPropertyPredicate predicateWithValue:albumTitle
                                 forProperty: MPMediaItemPropertyAlbumTitle];
MPMediaQuery *myAlbumQuery = [[MPMediaQuery alloc] init];
[myAlbumQuery addFilterPredicate: albumNamePredicate];
songsAlbumList = [myAlbumQuery items];

Pour obtenir la longueur d'une chanson, j'utilise ceci:

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

Donc, ce qui précède fonctionne bien, je n'obtiens pas un ajout correct des chants ... des idées?

Était-ce utile?

La solution

Alors je l'ai résolu - mon problème était que je ne savais pas comment faire correctement le calcul avec les nsnumbers - je ne savais pas que je cherchais cela, c'est pourquoi je ne l'ai pas demandé. Voici le code que j'ai trouvé pour calculer la longueur d'un album sur votre appareil:

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

J'espère que cela est utile pour quelqu'un d'autre là-bas.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top