我試圖將目標C的樣本轉換爲Monotouch,並且遇到了一些困難。Monotouch AVAssetReader
基本上我想讀取一個視頻文件,並逐個將幀解碼爲一個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的內容是不正確的格式?我檢查了視頻文件,並且它加載得很好,「資產」包含有關視頻的有效信息。
這是原來的目標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];
我不是爲目的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中的錯誤,還是我設置錯誤?
我有一個想法,我可能需要等待圖像數據準備好,但在這種情況下,我不知道如何。相當卡住了...