2012-03-06 84 views
0

我想顯示的目錄及其持續時間內的音頻文件的列表,以便讓我使用時間:爲什麼回負值

AVAudioPlayer * sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil]; 

NSLog([NSString stringWithFormat:@"%f", sound.duration]); 

cell.detailTextLabel.text =[NSString stringWithFormat:@"Duration: %.2f sec", sound.duration]; 

但有時sound.duration返回負值。我想知道爲什麼,以及如何解決它。

問候

回答

0

希望這將幫助你.. 在你的代碼,你在打印的NSLog沒有分配和錯誤的格式,以便它在零下給。所以檢查一次。

AVAudioPlayer * sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:@"/Users/apple/Desktop/LabelTextDemo/LabelTextDemo/song2.caf"] error:nil]; 

NSLog(@"%@",[NSString stringWithFormat:@"%.2f", sound.duration]); 
+0

在此行cell.detailTextLabel.text =問題[的NSString stringWithFormat:@ 「持續時間:%.2f秒」,sound.duration]; – AMH 2012-03-06 09:56:12

+0

lblLabel2.text = [NSString stringWithFormat:@「%。2f」,sound.duration];我喜歡這樣做,它的工作 – Kartik 2012-03-06 10:03:28

+0

仍然是相同的錯誤 – AMH 2012-03-06 10:28:10

2
AudioFileID      audioFile; 
CFDictionaryRef piDict = nil; 
UInt32 piDataSize = sizeof(piDict); 

if (noErr != AudioFileOpenURL((CFURLRef)[NSURL fileURLWithPath:filePath], 0x01, kAudioFileCAFType, &audioFile)) 
{ 
    return [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithString:filePath], @"FilePath", @"Unknown",@"Title",@"Unknown",@"Album",@"Unknown",@"Artist",@"0", @"Duration", nil]; 
} 
if(noErr != AudioFileGetProperty(audioFile, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict)) 
{ 
    AudioFileClose(audioFile); 
    return [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithString:filePath], @"FilePath", @"Unknown",@"Title",@"Unknown",@"Album",@"Unknown",@"Artist",@"0", @"Duration", nil]; 
} 

AudioFileClose(audioFile); 

if(piDict) 
{ 
    NSString * duration = @"0"; 
    CFStringRef  durationString = nil; 
    if (piDict && CFDictionaryGetValueIfPresent(piDict, CFSTR(kAFInfoDictionary_ApproximateDurationInSeconds), (const void **) &durationString)) 
    { 
     duration = (NSString*)durationString; 
     duration = [duration stringByReplacingOccurrencesOfString:@"," withString:@""]; 
    } 
    if (duration) 
     [songInfomationDictionary setObject:duration forKey:@"Duration"]; 


    CFRelease(piDict); 
    //get duration as string 
} 
return nil; 
+2

它很高興添加一些解釋以及代碼牆 – jrturton 2012-03-07 09:35:43

相關問題