使用CocoaHTTPServer流式传输视频

时间:2013-07-11 14:08:55

标签: ios objective-c macos mpmovieplayercontroller cocoahttpserver

我正在尝试在MAC OS X上实现为iOS设备流式传输视频的服务器。

在服务器端,我使用CocoaHTTPServer返回.mp4视频。

    - (HTTPFileResponse*)video:(NSString*)pPath
    {    
        BOOL              fileExists   = [[NSFileManager defaultManager] fileExistsAtPath:pPath];
        HTTPFileResponse *fileResponse = nil;

        if (fileExists && [self isVideo:pPath])
        {
            fileResponse = [[HTTPFileResponse alloc] initWithFilePath:pPath forConnection:self];
        }

        return fileResponse;
    }

在客户端,我使用MPMoviePlayerController来读取视频。

当我尝试阅读视频时,我收到此错误:

MPMovieFinishReasonPlaybackError.error : Error Domain=MediaPlayerErrorDomain Code=-11828 "Cannot Open" UserInfo=0xb92ca80 {NSLocalizedDescription=Cannot Open}"

1 个答案:

答案 0 :(得分:4)

我通过覆盖HTTPFileResponse的httpHeaders来修复此问题:

- (NSDictionary *)httpHeaders
{
    NSString *key = @"Content-Disposition";
    NSString *value = [NSString stringWithFormat:@"attachment; filename=\"%@\"", [filePath lastPathComponent]];

    return [NSDictionary dictionaryWithObjectsAndKeys:value, key, nil];
} 

这是因为HTTPFileResponse返回没有扩展名的视频。并且MPMoviePlayerController无法在没有扩展名的情况下阅读视频。

相关问题