On iPhone, how can I use fileHandle to download mp3 file on the fly?

I am trying to implement the AudioFileStreamSeek function in my streaming application. But I can not achieve this. Even Matt Gallagher said on his blog:

By the way, the AudioFileStreamSeek function seems completely broken. If you can't get it to work (like I couldn't) just look for a new point in the file, set the intermittent value to true and let AudioFileStream handle it.

My kindda code 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, &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;
        }
    }

Please, help...

+3
source share
1 answer

, NSConnection, , NSData, , NSData, NSConnectionDelegate. NSConnection , . , , , , .

+3

Source: https://habr.com/ru/post/1716343/


All Articles