AVAudioPlayer 在播放新声音时冻结 UI
AVAudioPlayer Freezes the UI when new sound is played
我是 iOS 的新手,我从早上开始就在寻找这个问题的解决方案,但还没有找到解决方案,我有超过 6000 个在线声音文件,我想使用 AVAudioPlayer
当用户点击单元格时,所以我在 didSelectRowAtIndexPath:
中实现了它
像这样:
-(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
,因为它对我来说似乎很容易使用
-(void)AVAudioPlayerDidFinishPlaying:(AVAudioPlayer *)Player successfully:(BOOL)flag
这就是为什么我想用 AVAudioPlayer
来实现它
希望你能理解我的问题,
抱歉我的英语不好。
从不使用
NSData *soundData = [NSData dataWithContentsOfURL:url];
这会阻止您的 UI。
您需要使用NSURLSession
异步下载数据。
我是 iOS 的新手,我从早上开始就在寻找这个问题的解决方案,但还没有找到解决方案,我有超过 6000 个在线声音文件,我想使用 AVAudioPlayer
当用户点击单元格时,所以我在 didSelectRowAtIndexPath:
中实现了它
像这样:
-(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
,因为它对我来说似乎很容易使用
-(void)AVAudioPlayerDidFinishPlaying:(AVAudioPlayer *)Player successfully:(BOOL)flag
这就是为什么我想用 AVAudioPlayer
希望你能理解我的问题, 抱歉我的英语不好。
从不使用
NSData *soundData = [NSData dataWithContentsOfURL:url];
这会阻止您的 UI。
您需要使用NSURLSession
异步下载数据。