如何使用 NSFileHandle 和 NSURLConnection 将 wav 文件从网络下载到 iPhone 上的某个位置?

发布于 2024-08-26 17:38:51 字数 1434 浏览 8 评论 0原文

我想从网络服务下载 wav 文件,将其缓存在 iPhone 上并使用 AVAudioPlayer 播放它。在处理相对较大的文件时,使用 NSFileHandle 和 NSURLConnection 似乎是一个可行的解决方案。但是,在模拟器中运行应用程序后,我在定义的目录(NSHomeDirectory/tmp)下看不到任何保存的文件。下面是我的基本代码。我哪里做错了?任何想法表示赞赏!

#define TEMP_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"tmp"]

- (void)downloadToFile:(NSString*)name
{
    NSString* filePath = [[NSString stringWithFormat:@"%@/%@.wav", TEMP_FOLDER, name] retain];
    self.localFilePath = filePath;

    // set up FileHandle
    self.audioFile = [[NSFileHandle fileHandleForWritingAtPath:localFilePath] retain];
    [filePath release];

    // Open the connection
    NSURLRequest* request = [NSURLRequest 
                             requestWithURL:self.webURL
                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                             timeoutInterval:60.0];
    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

#pragma mark -
#pragma mark NSURLConnection methods

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
    [self.audioFile writeData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError*)error
{
    NSLog(@"Connection failed to downloading sound: %@", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [connection release];
    [audioFile closeFile];

}

I want to download a wav file from a web service, cache it on the iphone and playback it using AVAudioPlayer. Using NSFileHandle and NSURLConnection seems a viable solution when dealing with relatively large files. However, after running the app in the simulator I don't see any saved file under the defined directory (NSHomeDirectory/tmp). Below is my basic code. Where am I doing wrong? Any thoughts are appreciated!

#define TEMP_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"tmp"]

- (void)downloadToFile:(NSString*)name
{
    NSString* filePath = [[NSString stringWithFormat:@"%@/%@.wav", TEMP_FOLDER, name] retain];
    self.localFilePath = filePath;

    // set up FileHandle
    self.audioFile = [[NSFileHandle fileHandleForWritingAtPath:localFilePath] retain];
    [filePath release];

    // Open the connection
    NSURLRequest* request = [NSURLRequest 
                             requestWithURL:self.webURL
                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                             timeoutInterval:60.0];
    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

#pragma mark -
#pragma mark NSURLConnection methods

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
    [self.audioFile writeData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError*)error
{
    NSLog(@"Connection failed to downloading sound: %@", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [connection release];
    [audioFile closeFile];

}

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

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

发布评论

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

评论(2

浊酒尽余欢 2024-09-02 17:38:51

NSFileHandle fileHandleForWritingAtPath: 要求文件已存在。您如何创建该文件?

NSFileHandle fileHandleForWritingAtPath: requires the file to already exist. How are you creating the file?

风向决定发型 2024-09-02 17:38:51

在哪里

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

你的代表

?这是您应该写入/保存文件的位置。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data

这是您附加收到的数据的位置。

Where is your

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

delegate?

this is where you should write/save the file.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data

this is where you append the data you receive.

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