ios删除avaudioplayer循环延迟

时间:2015-10-19 17:02:59

标签: ios objective-c audio avaudioplayer

我有一个加载声音文件和循环的avaudioplayer。问题是,每次文件循环时,都会出现暂停/延迟,这会使音乐听起来很糟糕。

我发现这个问题有类似的问题,但适用于2名玩家。我有一个播放器,每个循环暂停1秒:AVAudioPlayer eliminating one second pause between sound files

以下是我的音频播放器设置:

@interface HomeViewController ()

@property (strong, nonatomic) DataController *dataController;

@end

@implementation HomeViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.dataController = [DataController sharedInstance];

        [self createAndStartAudioPlayer];


}

- (void) viewWillAppear:(BOOL)animated{

        [self.dataController.player play];
}

- (void) createAndStartAudioPlayer {

    NSTimeInterval shortStartDelay = 0.01;            // seconds
    NSTimeInterval now = self.dataController.player.deviceCurrentTime;

    [self.dataController.player       playAtTime: now + shortStartDelay];

        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"jingle-loop" ofType:@"mp3"];
        NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
        self.dataController.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL fileTypeHint:AVFileTypeMPEGLayer3 error:nil];
        self.dataController.player.numberOfLoops = -1; //infinite

    [self.dataController.player prepareToPlay];


}

如何停止这1秒的暂停并播放循环,就像连续的歌一样?

1 个答案:

答案 0 :(得分:0)

先生。评论中的T实际上是正确的。解决方案是修复由车库带自动添加的空白区域。