2016-11-29 135 views
0

我是iOS新手,自上午以来我一直在寻找解决此问题的解决方法,但未找到解决方案,我有超过6000个在线声音文件,我想使用它AVAudioPlayer当用户单击该单元格,所以我实现它didSelectRowAtIndexPath: 这样的:AVAudioPlayer播放新声音时冻结UI

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", [soundsArray objectAtIndex:(int)indexPath.row]]; 
    NSData *soundData = [NSData dataWithContentsOfURL:url]; 
    _audioPlayer = [[AVAudioPlayer alloc] initWithData:soundData error:NULL]; 
     [_audioPlayer play]; 
    } 

的问题是,每当我对任何行点击,整个UI冻结,直到它需要加载完整的声音时文件来自互联网, 更大的文件,它需要更长的时间。

我试着用AVPlayer和它工作正常,但我需要AVAudioPlayer becouse似乎容易我 -(void)AVAudioPlayerDidFinishPlaying:(AVAudioPlayer *)Player successfully:(BOOL)flag

这就是工作,为什么我要与AVAudioPlayer

来实现它,我希望你明白我的问题, 对不起,我的英语不好。

+0

先停止已经播放的歌曲是很好的做法,您必须在加载另一首歌曲之前先停止播放器尝试。 – vaibhav

回答

1

切勿使用

NSData *soundData = [NSData dataWithContentsOfURL:url]; 

这将阻止你的用户界面。

您需要使用NSURLSession异步下载数据。

+0

ow ..我会现在尝试 –

+0

非常感谢@Rajat –

+0

很高兴它帮助你:) – Rajat

相关问题