3

我正在将一个适用于 aac 音频文件的应用程序移植到 iOS6,我发现了一个奇怪的行为,当我尝试获取(有效)aac 音频文件的持续时间时,它总是返回 0,在 iOS4 和 iOS5 中它可以工作美好的。

¿ AvAudioPlayer 类上是否存在影响持续时间属性的错误?我已经阅读了有关currentTime属性的一些问题。

这是代码:

NSURL* urlFichero = [NSURL fileURLWithPath:rutaFichero];
avaPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: urlFichero error:nil];
segundos = avaPlayer.duration;
NSLog(@"[ControladorFicheros] Fichero: '%@' Duración: '%f'", nombreFichero, segundos);
[avaPlayer stop];
[avaPlayer release];

谢谢 ;)

4

2 回答 2

4

最后的问题是,在最新版本的 API 中,AVAudioPlayer 似乎只在准备好播放文件时才返回文件的正确持续时间,这就是为什么我的解决方案是错误的,获取文件持续时间的正确方法(在秒)如果你不想重现它是:

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) ;

迅速

let asset = AVURLAsset(url: url, options: [AVURLAssetPreferPreciseDurationAndTimingKey: true])
let durationInSeconds = CMTimeGetSeconds(asset.duration)
于 2013-03-18T09:17:48.147 回答
1

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

MPMoviePlayerController *testPlayer = [[MPMoviePlayerController alloc] initWithContentURL:filePath];
[testPlater prepareToPlay];
[testPlater play];
于 2013-03-14T04:46:27.083 回答