gpt4 book ai didi

iphone - 为什么返回负值

转载 作者:行者123 更新时间:2023-11-28 23:03:58 26 4
gpt4 key购买 nike

我想显示目录中的音频文件列表及其持续时间,因此要获取我使用的持续时间:

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 会返回一个负值。我想知道为什么,以及如何解决它。

最好的问候

最佳答案

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;

关于iphone - 为什么返回负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9581108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com