I’m currently using the following code to play a mp3 with AVPlayer
NSString *title = [[NSString alloc] initWithFormat:@"http://.......com/%@", mp3File];
NSURL *url = [[NSURL alloc] initWithString:title];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:anItem];
[player play];
However I also want to store the downloaded file, so subsequent plays will not require downloading the file again. For this I am using the following
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:surahAyah]];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSString *title = [[NSString alloc] initWithFormat:@"http://.......com/%@", mp3File];
NSURL *url = [[NSURL alloc] initWithString:title];
NSData *dbFile = [[NSData alloc] initWithContentsOfURL:url];
[dbFile writeToFile:path atomically:YES];
}
What is the best practice for consolidating the above and making using of the cached? AVPlayerItem and storing that instead of making a fresh call to download and save the file?
The MP3 files are approximately 100KB in size, so I’m not too worried about download progress.
I’ve managed to get this working successfully using the awesome AFNetworking library