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

时间:2018-09-26 23:52:05

标签: ios objective-c avaudioplayer

我有一个AVAudioPlayer实例(很多实例),通常被迫一遍又一遍地重启播放。声音大约有1.3s,大约每0.7s重复一次(当声音持续约一半时)。

问题在于有时(可能占所有情况的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实例上对同一方法的下一次调用可解决此问题(再次播放)。

1 个答案:

答案 0 :(得分:0)

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