问题
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