Loading and Playing Audio with avaudioplayer, UITableView NSURL

我们两清 提交于 2019-12-25 05:20:16

问题


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]

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?

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

Should I try streaming it?


回答1:


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);

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



来源:https://stackoverflow.com/questions/12416659/loading-and-playing-audio-with-avaudioplayer-uitableview-nsurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!