使用avaudioplayer,UITableView NSURL加载和播放音频

时间:2012-09-14 00:32:09

标签: objective-c ios avfoundation nsdata nsurl

我有一个音频对象的表格视图,每个都有一个url属性。我正在寻找最快的方式(对于用户来说)在触摸时播放每个音频文件。

我尝试从网址首先获取数据:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSURL *audioURL = [NSURL URLWithString:(NSString *)[[[self.objects objectAtIndex:indexPath.row] objectForKey:@"audioFile"] url]];
    NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
    NSError *error;

    self.player = [[AVAudioPlayer alloc] initWithData:audioData error:&error];
    self.player.delegate = self;
    [self.player play];
}

但它缓慢且反应迟钝。我试图直接去寻找网址:

self.player = [[AVAudioplayer alloc] initWithContentsOfURL:audioURL]

在这种情况下加载和播放音频文件的最佳方法是什么?异步获取数据? AVfoundation是流式传输音频的最佳方式吗?

http://developer.apple.com/library/ios/#documentation/MusicAudio/Reference/AudioStreamReference/Reference/reference.html#//apple_ref/doc/uid/TP40006162

我应该尝试播放它吗?

1 个答案:

答案 0 :(得分:2)

最快的方法是设置音频服务

CFBundleRef mainbundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainbundle, CFSTR("soundhere"), CFSTR("extensionofsound"), NULL);
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject);

如果您坚持不使用AudioServices,那么您可以查看OpenAL(Github for iOS上有包装器)