在 MPMoviePlayerController 中缓存渐进式下载内容

发布于 2024-09-11 15:27:10 字数 207 浏览 2 评论 0原文

我有一个在 iphone (sdk 4) 中实现的音乐播放器,它可以与 Mp3 流(即时转码)或原始渐进式下载一起使用。

无论如何,是否可以缓存渐进式下载的内容(我不直接控制),以便它不会重新下载整个内容?

或者是否有全局缓存设置可以控制? (顺便说一句,我使用 ASIHTTP API 来联系我的 HTTP 服务器并访问允许缓存的数据)。

提前致谢。

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.

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

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我三岁 2024-09-18 15:27:10

您可以使用 NSURLConnection 保存文件并播放。

示例:

-(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];
}

下面是播放代码

//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];

You can use NSURLConnection to save the file and play it back.

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文