对于奇怪的标题感到抱歉,但AVAudioPlayer在再次播放声音时有点奇怪,就在播放声音结束之前。
我可以在播放器没有播放,播放器播放完毕和播放器播放时播放mp3文件。 当正在播放的歌曲即将结束时,我不能播放mp3。这是我的代码,我记录了重要的内容。
-(void) play:(int)p{
if([players[p] isPlaying]){
NSLog(@"is playing");
[players[p] setCurrentTime:0.025f]; //when it's playing, set it to the start of the
//file so it starts playing again. Usually works
if(![players[p] isPlaying]){
NSLog(@"is not playing"); //although I just told the player to play again
//it sometimes returns that it in fact does NOT play
//so it's playing in the first if condition but
//not playing in the second if condition
[players[p] prepareToPlay];
[players[p] play]; //from here I try to resurrect the player numerous times
//but everything fails, I have to call the function again to make it work
[players[p] setCurrentTime:0.025f];
if([players[p] isPlaying]){
NSLog(@"playing"); //never gets called; although I tell it to play so many times
//it just won't do
}else {
NSLog(@"NOTplaying");
[players[p] play];
}
} else {
NSLog(@"is still playing");
}
} else {
NSLog(@"not playing at all");
[players[p] play]; } //if not playing, play. Always works
}
我认为这可能与AVAudioPlayer如何发布文件或者它正在播放但在此之后的毫秒内没有播放任何内容有关。 (在第一个if条件下播放但在此之后停止)。
使用5秒音频文件可以轻松再现发生这种情况的窗口。等到它即将结束然后点击"播放"并且该方法被执行但没有任何东西正在播放。该方法适用于所有其他情况(不播放和播放但不会完成播放)。