使用avaudioplayer,UITableView NSURL加载和播放音频 [英] Loading and Playing Audio with avaudioplayer, UITableView NSURL

查看:276
本文介绍了使用avaudioplayer,UITableView NSURL加载和播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个音频对象的表视图,每个对象都有一个url属性.我正在寻找(针对用户)最快的方式来播放每个音频文件.

I have a table view of audio objects, each with a url property. I'm looking for the fastest way (for the user) to play each audio file on touch.

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

I tried getting the data first from the 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];
}

但是它很慢而且反应迟钝.我尝试直接输入网址:

But it's slow and unresponsive. I tried to go straight for the url:

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

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

What's the best way to load and play audio files in this situation? Asynchronously fetching the data? Is AVfoundation the best way to stream down audio?

我应该尝试流式传输吗?

Should I try streaming it?

推荐答案

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

The fastest way would be to set an audio service as such

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

如果您坚持不使用AudioServices,则可以看看OpenAL(Github上有针对iOS的包装器)

If you are insistent on not using AudioServices then you can take a look at OpenAL (there are wrappers on Github for iOS)

这篇关于使用avaudioplayer,UITableView NSURL加载和播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆