iOS6 AVAudioPlayer - AAC audio file duration always return 0

烂漫一生 提交于 2019-12-05 23:07:40

Finally the problem is that in newest versions of the API, AVAudioPlayer appears to only returns the correct duration of a file when it is ready for play it, that's why my solution was wrong, the correct way to get the duration of a file (in seconds) if you don't want to reproduce it is:

AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:anURI_ToResource 
                  options:[NSDictionary dictionaryWithObjectsAndKeys:
                  [NSNumber numberWithBool:YES], 
                  AVURLAssetPreferPreciseDurationAndTimingKey,
                  nil]] autorelease];

 NSTimeInterval durationInSeconds = 0.0;
if (asset) 
     durationInSeconds = CMTimeGetSeconds(asset.duration) ;

I noticed the same problem. My solution is to use instead.

MPMoviePlayerController *testPlayer = [[MPMoviePlayerController alloc] initWithContentURL:filePath];
[testPlater prepareToPlay];
[testPlater play];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!