I have a music player implemented in iphone (sdk 4) and it works both with streaming of Mp3 (Transcoded on the fly) or raw progressive download.
我有一个在iphone(sdk 4)中实现的音乐播放器,它可以与Mp3(即时转码)或原始渐进式下载一起使用。
Is there anyway to make the progressivedownloaded content (which I dont directly control) to be cached so it doesn redownload the whole content?
反正有没有让渐进式内容(我不直接控制)被缓存,所以它不会重新下载整个内容?
Or Is there a global cache setting to control? (On a side note, I used ASIHTTP APIs to contact my HTTP server and access data that does allow caching).
或者是否需要控制全局缓存设置? (另一方面,我使用ASIHTTP API联系我的HTTP服务器并访问允许缓存的数据)。
Thanks in advance.
提前致谢。
1 个解决方案
#1
1
You can use NSURLConnection to save the file and play it back.
您可以使用NSURLConnection保存文件并进行播放。
Example:
例:
-(void)downloadFileAtPath:(NSString *)path
{
NSURL *url = [NSURL URLWithString:path];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
[connection start];
}
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
[[NSFileManager defaultManager] createFileAtPath:pathForCurrentFile contents:nil attributes:nil];
currentFile = [NSFileHandle fileHandleForUpdatingAtPath:pathForCurrentFile];
if (currentFile)
{
[currentFile seekToEndOfFile];
}
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
if( currentFile != nil){
if (currentFile) {
[currentFile seekToEndOfFile];
}
[currentFile writeData:data];
}
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog(@"%s, %@", __PRETTY_FUNCTION__, error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[currentFile closeFile];
}
Below is the code for playback
以下是播放代码
//Playback
NSString *loopFilePath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:vidFileName];
NSURL *vidURL = [NSURL fileURLWithPath:vidFilePath];
self.vidController = [[MPMoviePlayerController alloc] initWithContentURL:vidURL];
self.vidController.view.frame = CGRectMake(0, 0, 640, 480);
self.vidController.controlStyle = MPMovieControlStyleDefault;
[self.view addSubview:self.vidController.view];
[self.vidController prepareToPlay];
[self.vidController play];
#1
1
You can use NSURLConnection to save the file and play it back.
您可以使用NSURLConnection保存文件并进行播放。
Example:
例:
-(void)downloadFileAtPath:(NSString *)path
{
NSURL *url = [NSURL URLWithString:path];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
[connection start];
}
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
[[NSFileManager defaultManager] createFileAtPath:pathForCurrentFile contents:nil attributes:nil];
currentFile = [NSFileHandle fileHandleForUpdatingAtPath:pathForCurrentFile];
if (currentFile)
{
[currentFile seekToEndOfFile];
}
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
if( currentFile != nil){
if (currentFile) {
[currentFile seekToEndOfFile];
}
[currentFile writeData:data];
}
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog(@"%s, %@", __PRETTY_FUNCTION__, error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[currentFile closeFile];
}
Below is the code for playback
以下是播放代码
//Playback
NSString *loopFilePath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:vidFileName];
NSURL *vidURL = [NSURL fileURLWithPath:vidFilePath];
self.vidController = [[MPMoviePlayerController alloc] initWithContentURL:vidURL];
self.vidController.view.frame = CGRectMake(0, 0, 640, 480);
self.vidController.controlStyle = MPMovieControlStyleDefault;
[self.view addSubview:self.vidController.view];
[self.vidController prepareToPlay];
[self.vidController play];