Getting byte Data from File

孤人 提交于 2019-12-25 14:39:10

问题


WHAT IM DOING I am trying to get an audio file (could be up to an hour long. eg. a Podcast) that I've recorded with AVAudioRecorder to be uploaded to our backend. In addition to being uploaded to the server it needs to be able to be "Paused" and "Resumed" if the user chooses. Because of this, I believe, I need to use dataWithBytesNoCopy:buffer on the NSData class to achieve this.

WHERE IM AT I know for a fact I can get the data with using the passed self.mediaURL property:

if (self.mediaURL) {

    NSData *audioData = [NSData dataWithContentsOfURL:self.mediaURL];

    if (audioData) {

        [payloadDic setObject:audioData forKey:@"audioData"];
    }

}

However, this will not give me the desired functionality. I am trying to keep track of the bytes uploaded so that I can resume if the user pauses.

QUESTION How can I use the provided self.mediaURL so that I can retrieve the file and be able to calculate the byte length like this example?

Byte *buffer = (Byte*)malloc((long)audioFile.size);
NSUInteger buffered =[rep getBytes:buffer fromOffset:0.0 length:(long)rep.size error:nil];

NSMutableData *body = [[NSMutableData alloc] init];
body = [NSMutableData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

回答1:


Instead of making things more complicated for yourself by trying to reinvent the wheel, use what the system gives you. NSURLSession lets you do a background upload. You hand the task to the session (created using the background session configuration) and just walk away. The upload takes place in pieces, when it can. No "pause" or "resume" needed; the system takes care of everything. Your app doesn't even have to be running. If authentication is needed, your app will be woken up in the background as required. This architecture is just made for the situation you describe.

If the problem is that you want random access to file data without having to read the whole thing into a massive NSData, use NSFileHandle.



来源:https://stackoverflow.com/questions/33092220/getting-byte-data-from-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!