2015-01-17 2 views
0

두 개의 mp3 파일이 있습니다. 하나는 앱 번들에 있고 다른 하나는 사용자의 문서 디렉토리에 있습니다. mp3 파일의 재생 시간을 가져오고 싶습니다.AVURLAsset은 문서 디렉토리에서 mp3 기간을 가져올 수 없지만 응용 프로그램 번들의 mp3와 잘 작동합니다.

AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:newPath] options:nil]; 
[audioAsset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{ 
    CMTime audioDuration = audioAsset.duration; 
    float audioDurationSeconds = CMTimeGetSeconds(audioDuration); 

    NSLog(@"duration:%f",audioDurationSeconds); 
}]; 

는 그 코드는 응용 프로그램 번들에있는 MP3 파일과 잘 작동하지만 그것은 단지 "시간 : 0.000000"을 기록 문서 디렉토리에있는 MP3 작동하지 않습니다. 왜?

답변

1

다음은 문서 디렉토리에서 mp3 파일과 연결된 경로를 작성하는 방법입니다.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Professor_and_the_Plant.mp3"]; 

NSURL *mp3_url = [[NSURL alloc] initFileURLWithPath:path]; 
+0

나는 문서 디렉토리 경로를 얻는 방법을 알고있다. 문제는 AVURLAsset이 문서 디렉토리에서 mp3와 함께 작동하지 않는다는 것입니다. –

+0

그것은 나를 위해 일했고, 나는 같은 결과를 얻었다. – gabbler

+1

감사합니다. 당신 말이 맞습니다. 내 문제는 [NSURL URLWithString :]을 사용하고 있으므로 잘못된 URL이 표시됩니다. –

0

NSURLAsset의 기본값은 빠르게 작동합니다. 길이 또는 메타 데이터를 쉽게 사용할 수없는 경우 (즉, ID3 헤더에서), 값을 산출하지 못합니다. 더 오래 걸리는 경우에도, 정확한 데이터를 요구하기 위해, 당신은 그것을 줄 수있는 옵션이 있습니다 (. 나는 다시 그것을 폐기 유지하지 않아도 내가 정적의 옵션을 붙어)

static NSDictionary *options; 
    if (!options) { 
     NSNumber *val = [[NSNumber alloc] initWithBool: YES]; 
     options = [[NSDictionary alloc] initWithObjectsAndKeys:val, AVURLAssetPreferPreciseDurationAndTimingKey, nil]; 
    } 
    AVAsset *asset = [[AVURLAsset alloc] initWithURL:url options:options]; 

문서의 앱 대 앱 번들에 태그가 다르게 지정되어있는 경우 문제가 될 수 있습니다.

관련 문제