在 iPhone 上,如何使用 fileHandle 即时下载 mp3 文件?

发布于 2024-08-03 19:58:21 字数 1483 浏览 3 评论 0原文

我正在尝试在我的流应用程序上实现 AudioFileStreamSeek 功能。但我没有办法让它运行。甚至Matt Gallagher也在他的博客上说道:

顺便说一句,AudioFileStreamSeek 函数似乎完全损坏了。如果你不能让它工作(就像我不能),只需寻找文件中的一个新点,将不连续设置为 true 并让 AudioFileStream 处理它。

我的代码有点像这样,但我无法让它工作:

    NSString *path = [[NSString alloc] initWithContentsOfURL: url];

    NSLog(@"url = %@", path);
    SInt64 currentOffset;
    UInt32 flags = 0;

    OSStatus status = AudioFileStreamSeek( audioFileStream, 150, &currentOffset, &flags );
    NSLog(@"Setting next byte offset to: %qi, flags: %d", (long long)currentOffset, flags);

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath: path];
    // then read data from the new offset set by AudioFileStreamSeek
    [fileHandle seekToFileOffset:currentOffset];
    NSData * data = [fileHandle readDataOfLength:4096];

    NSLog(@"data length %d, bytes %d", [data length], [data bytes]);


    if (discontinuous)
    {
        err = AudioFileStreamParseBytes(audioFileStream, length, bytes, kAudioFileStreamParseFlag_Discontinuity);
        if (err)
        {
            [self failWithErrorCode:AS_FILE_STREAM_PARSE_BYTES_FAILED];
            return;
        }
    }
    else
    {
        err = AudioFileStreamParseBytes(audioFileStream, length, bytes, 0);
        if (err)
        {
            [self failWithErrorCode:AS_FILE_STREAM_PARSE_BYTES_FAILED];
            return;
        }
    }

请帮忙......

I am trying to implement the AudioFileStreamSeek feature on my streaming app. But there is no way I can get this running. Even Matt Gallagher said on his blog:

Icidentally, the AudioFileStreamSeek function seems completely broken. If you can't get it to work (as I couldn't) just seek to a new point in the file, set discontinuous to true and let AudioFileStream deal with it.

My code kindda looks like this but I can't get it to work:

    NSString *path = [[NSString alloc] initWithContentsOfURL: url];

    NSLog(@"url = %@", path);
    SInt64 currentOffset;
    UInt32 flags = 0;

    OSStatus status = AudioFileStreamSeek( audioFileStream, 150, ¤tOffset, &flags );
    NSLog(@"Setting next byte offset to: %qi, flags: %d", (long long)currentOffset, flags);

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath: path];
    // then read data from the new offset set by AudioFileStreamSeek
    [fileHandle seekToFileOffset:currentOffset];
    NSData * data = [fileHandle readDataOfLength:4096];

    NSLog(@"data length %d, bytes %d", [data length], [data bytes]);


    if (discontinuous)
    {
        err = AudioFileStreamParseBytes(audioFileStream, length, bytes, kAudioFileStreamParseFlag_Discontinuity);
        if (err)
        {
            [self failWithErrorCode:AS_FILE_STREAM_PARSE_BYTES_FAILED];
            return;
        }
    }
    else
    {
        err = AudioFileStreamParseBytes(audioFileStream, length, bytes, 0);
        if (err)
        {
            [self failWithErrorCode:AS_FILE_STREAM_PARSE_BYTES_FAILED];
            return;
        }
    }

Please help...

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

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

发布评论

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

评论(1

酸甜透明夹心 2024-08-10 19:58:21

如果没有任何其他解决方案,您可以做的就是创建一个 NSConnection,然后当您收到 NSData 时,您可以通过处理收到到 NSConnectionDelegate 的每个新 NSData 块来有效地创建流。 NSConnection 将确保按顺序将其发送给您,因此您不必担心顺序是否正确。但请注意,根据您的应用程序,您可能需要在主应用程序线程之外执行此操作,以便即使下载停止并且您必须重新缓冲,用户仍然可以使用您的应用程序。

Lacking any other solutions, what you can do is create an NSConnection and then as you receive the NSData you can effectively create streaming by processing each new chunk of NSData that you receive to your NSConnectionDelegate. NSConnection will make sure to send it to you in order, so you won't have to worry about getting it ordered correctly. Note though that, depending on your application, you may need to do this outside the main application thread, so that the user can still work with your app even if the download stalls and you have to rebuffer.

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