Domanda

Devo ottenere l'intera lunghezza di un album che si trova su un dispositivo ma non ottenere il risultato corretto. Quello che ho è il follwing per ottenere un array con le canzoni di un album:

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

Per ottenere la lunghezza di una canzone, lo uso:

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

Quindi quanto sopra funziona bene, non ricevo un'aggiunta corretta delle dimorazioni di canzoni ... qualche idea?

È stato utile?

Soluzione

Quindi l'ho risolto - il mio problema era che non sapevo come fare correttamente i matematici con i nsnumbers - non sapevo che lo stavo cercando, ecco perché non lo chiedevo. Ecco il codice che mi è venuto in mente per calcolare la lunghezza di un album sul tuo dispositivo:

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

Spero che questo sia utile per qualcun altro là fuori.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top