【问题标题】:Loading and Playing Audio with avaudioplayer, UITableView NSURL使用 avaudioplayer、UITableView NSURL 加载和播放音频
【发布时间】:2012-09-07 03:42:49
【问题描述】:

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

我尝试先从 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

我应该尝试流式传输吗?

【问题讨论】:

    标签: objective-c ios avfoundation nsdata nsurl


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 2017-03-30
      相关资源
      最近更新 更多