我正在开发一个 APP 供用户将视频上传到我们的 FTP 服务器
到目前为止,一切都差不多完成了,但我遇到一个问题是用户上传视频(.MOV)后,我无法打开和播放文件。
quicktime播放器返回的错误信息是“无法打开,因为无法识别电影的文件格式”
在我的代码中,我让用户使用 ALAssetsLibrady 选择视频
然后将视频加载到 ALAsset 对象中,在开始上传之前,将视频从 ALAsset 加载到 NSInputStream 对象中,这里是代码。
ALAssetRepresentation *rep = [currentAsset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
iStream = [NSInputStream inputStreamWithData:data];
[iStream open];
下一步是设置一个 NSOutputStream 并打开它,通过以下代码处理上传操作。
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventNone:
{
break;
}
case NSStreamEventOpenCompleted:
{
//opened connection
NSLog(@"opened connection");
break;
}
case NSStreamEventHasBytesAvailable:
{
// should never happen for the output stream
[self stopSendWithStatus:@"should never happen for the output stream"];
break;
}
case NSStreamEventHasSpaceAvailable:
{
// If we don't have any data buffered, go read the next chunk of data.
NSInteger bufferSize = 65535;
uint8_t *buffer = malloc(bufferSize);
if (bufferOffset == bufferLimit) {
NSInteger bytesRead = [iStream read:buffer maxLength:bufferSize];
if (bytesRead == -1) {
[self stopSendWithStatus:@"file read error"];
} else if (bytesRead == 0) {
[self stopSendWithStatus:nil];
} else {
bufferOffset = 0;
bufferLimit = bytesRead;
}
}
// If we're not out of data completely, send the next chunk.
if (bufferOffset != bufferLimit) {
NSInteger bytesWritten = [oStream write:&buffer[bufferOffset] maxLength:bufferLimit - bufferOffset];
if (bytesWritten == -1) {
[self stopSendWithStatus:@"file write error"];
} else {
bufferOffset += bytesWritten;
}
}
//NSLog(@"available");
break;
}
case NSStreamEventErrorOccurred:
{
//stream open error
[self stopSendWithStatus:[[aStream streamError] description]];
break;
}
case NSStreamEventEndEncountered: //ignore
NSLog(@"end");
break;
}
}
没有任何错误发生,视频文件确实以正确的文件大小和名称上传到 FTP,但无法打开它。
有人知道任何线索吗?