iOS6 AVAudioPlayer - AAC音频文件的持续时间总是返回0。

问题描述 投票:3回答:2

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

AvAudioPlayer类是否有任何bug影响到 期限 属性?我读到过一些关于 当前时间 属性。

这是代码。

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

谢谢;)

iphone ios6 avaudioplayer
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) ;

Swift

let asset = AVURLAsset(url: url, options: [AVURLAssetPreferPreciseDurationAndTimingKey: true])
let durationInSeconds = CMTimeGetSeconds(asset.duration)

1
投票

我也注意到了同样的问题。 我的解决方法是使用代替。

MPMoviePlayerController *testPlayer = [[MPMoviePlayerController alloc] initWithContentURL:filePath];
[testPlater prepareToPlay];
[testPlater play];
© www.soinside.com 2019 - 2024. All rights reserved.