문제

장치에있는 앨범의 전체 길이를 가져와야하지만 올바른 결과를 얻지 못합니다. 내가 가진 것은 하나의 앨범의 노래로 배열을 얻는 폴링입니다.

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