How to do unlimited repetition by using AVAudioPlayer in ios

烂漫一生 提交于 2019-12-11 11:43:03

问题


In my app i am using audio. I want to play audio for unlimited time on tapping button. Is it possible?


回答1:


Easiest way is to re-play the player when it reaches the end :

-(void)startPlayer {

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]];
    NSError *error;
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    player.delegate = self;
    if (error) {
        NSLog(@"Error in audioPlayer: %@", [error localizedDescription]);
    } else {
        [aPlayer play];
    }
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer successfully:(BOOL)flag {

    aPlayer.currentTime = 0;
    [aPlayer play];
}

Call [self startPlayer]; once, and it will loop forever !




回答2:


AVAudioPlayer has a numberOfLoops method. Set that to -1 for unlimited repetition.



来源:https://stackoverflow.com/questions/22405959/how-to-do-unlimited-repetition-by-using-avaudioplayer-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!