将 currentTime 设置为 0.0 后 AVAudioPlayer 随机不播放

AVAudioPlayer randomly not playing after setting currentTime to 0.0

我有一个 AVAudioPlayer 实例(许多实例之一)被迫一次又一次地重新开始播放,通常是在它结束之前。声音大约有 1.3 秒,大约每 0.7 秒重复一次(当它超过其持续时间的一半时)。

问题是有时(可能占所有情况的 5%)声音不播放。

该文件是单声道AAC(我尝试了两个不同的短文件,效果相似)。

确切的代码如下所示:

- (void)restartPlay:(AVAudioPlayer *)player position:(CGPoint)p volume:(float)volume loops:(NSInteger)loops;
{
    NSString *filename = [self debug_resolveSoundName:player];

    DLogShort(@"\n[restartPlay...] in:   %@, %@, t:%f, vol:%f, pan:%f, loops:%li", filename, player.isPlaying?@"playing":@"not playing", player.currentTime, player.volume, player.pan, player.numberOfLoops);

    player.currentTime = 0.0;

    DLogShort(@"[restartPlay...] mid1:   %@, %@, t:%f, vol:%f, pan:%f, loops:%li", filename, player.isPlaying?@"playing":@"not playing", player.currentTime, player.volume, player.pan, player.numberOfLoops);

    [player play];

    DLogShort(@"[restartPlay...] mid2:   %@, %@, t:%f, vol:%f, pan:%f, loops:%li", filename, player.isPlaying?@"playing":@"not playing", player.currentTime, player.volume, player.pan, player.numberOfLoops);

    player.pan = [self getPan:p];
    player.volume = volume;
    player.numberOfLoops = loops;

    DLogShort(@"[restartPlay...] out:   %@, %@, t:%f, vol:%f, pan:%f, loops:%li \n", filename, player.isPlaying?@"playing":@"not playing", player.currentTime, player.volume, player.pan, player.numberOfLoops);
}

输出:

[restartPlay...] in:   audioFile1, playing, t:0.704104, vol:1.000000, pan:0.332738, loops:0
[restartPlay...] mid1:   audioFile1, not playing, t:0.000000, vol:1.000000, pan:0.332738, loops:0
[restartPlay...] mid2:   audioFile1, not playing, t:0.000000, vol:1.000000, pan:0.332738, loops:0
[restartPlay...] out:   audioFile1, not playing, t:0.000000, vol:1.000000, pan:0.311913, loops:0 

奇怪的是,声音的 属性 isPlaying 最初是 YES 并且在将 currentTime 设置为零之后(如我所料,样本的最开始)它的状态player.isPlaying 变为 NO。然后在调用 -play 之后它仍然没有播放(isPlaying 仍然 NO 而且我没有听到它)。

下一次在同一个 AVAudioPlayer 实例上调用同一个方法修复了这个问题(再次播放)。

这可能有助于停止或暂停,然后更改当前时间,然后播放。