我正在尝试将示例从 objective-c 转换为 Monotouch,但遇到了一些困难。
基本上我想读取视频文件,并将帧一帧一帧地解码为 opengl 纹理。
这样做的关键是使用 AVAssetReader,但我不确定如何在 Monotouch 中正确设置它。
这是我的代码:
AVUrlAsset asset=new AVUrlAsset(NSUrl.FromFilename(videoFileName),null);
assetReader=new AVAssetReader(asset,System.IntPtr.Zero);
AVAssetTrack videoTrack=asset.Tracks[0];
NSDictionary videoSettings=new NSDictionary();
NSString key = CVPixelBuffer.PixelFormatTypeKey;
NSNumber val=0x754b9d0; //NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; - Had to hardcode constant as it is not defined in Monotouch?
videoSettings.SetNativeField(key,val);
//**The program crashes here:
AVAssetReaderTrackOutput trackOutput=new AVAssetReaderTrackOutput(videoTrack,videoSettings);
assetReader.AddOutput(trackOutput);
assetReader.StartReading();
程序在上面指出的那一行崩溃了,参数无效异常,说明NSDictionary的内容格式不对?我检查了视频文件,它加载良好,“ Assets ”包含有关视频的有效信息。
这是原始的 Objective C 代码:
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
AVAssetReaderTrackOutput *trackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:videoSettings];
[_assetReader addOutput:trackOutput];
[_assetReader startReading];
我不是很喜欢 Objective C,所以非常感谢任何帮助。
编辑:我使用了下面建议的代码
var videoSettings = NSDictionary.FromObjectAndKey (
new NSNumber ((int) MonoTouch.CoreVideo.CVPixelFormatType.CV32BGRA),
MonoTouch.CoreVideo.CVPixelBuffer.PixelFormatTypeKey);
并且程序不再崩溃。通过使用以下代码:
CMSampleBuffer buffer=assetReader.Outputs[0].CopyNextSampleBuffer();
CVImageBuffer imageBuffer = buffer.GetImageBuffer();
我得到的图像缓冲区应该包含视频文件中的下一帧。通过检查 imageBuffer 对象,我发现它具有与视频文件匹配的有效数据,例如宽度和高度。
但是imageBuffer BaseAddress一直是0,说明图片没有数据?我试着这样做作为测试:
CVPixelBuffer buffer=(CVPixelBuffer)imageBuffer;
CIImage image=CIImage.FromImageBuffer(buffer);
图像总是返回为空。这是否意味着实际图像数据不存在,我的 imageBuffer 对象只包含帧头信息?
如果是这样,这是 Monotouch 中的错误,还是我设置有误?
我有一个想法,我可能需要等待图像数据准备好,但在那种情况下,我也不知道该怎么做。现在很卡住...
最佳答案
您需要像这样创建 NSDictionary:
var videoSettings = NSDictionary.FromObjectAndKey (
new NSNumber ((int) MonoTouch.CoreVideo.CVPixelFormatType.CV32BGRA),
MonoTouch.CoreVideo.CVPixelBuffer.PixelFormatTypeKey);
SetNativeField
完全不同(您将名为 CVPixelBuffer.PixelFormatTypeKey
的字段设置为 0x754b9d0
,而不是添加键/值对字典)。
关于c# - Monotouch AVAssetReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8923386/