簡體   English   中英

Monotouch AVAssetReader

[英]Monotouch AVAssetReader

我試圖將樣本從目標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的內容格式不正確? 我已經檢查了視頻文件,並且文件加載良好,“資產”包含有關視頻的有效信息。

這是原始的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];

我對目標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);

並且圖像始終返回為null。 這是否意味着不存在實際的圖像數據,並且我的imageBuffer對象僅包含幀頭信息?

如果是這樣,這是Monotouch中的錯誤,還是我將其設置為錯誤?

我有一個想法,我可能需要等待圖像數據准備就緒,但是在那種情況下,我也不知道該怎么辦。 現在很卡住...

您需要像這樣創建NSDictionary:

var videoSettings = NSDictionary.FromObjectAndKey (
  new NSNumber ((int) MonoTouch.CoreVideo.CVPixelFormatType.CV32BGRA),
  MonoTouch.CoreVideo.CVPixelBuffer.PixelFormatTypeKey);

SetNativeField是完全不同的東西(您將名為CVPixelBuffer.PixelFormatTypeKey的字段設置為0x754b9d0 ,未在字典中添加鍵/值對)。

[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; -是否必須對常量進行硬編碼,因為它不是在Monotouch中定義的?

您應該可以將其替換為:

CVPixelFormatType.CV32BGRA

請注意,MonoTouch將此值定義為0x42475241 ,該值與您的不同。 那可能是你的錯誤。 如果不是,我建議您制作一個小的獨立測試用例,並將其附加到http://bugzilla.xamarin.com上的錯誤報告中,我們將對其進行研究。

鏈接到Objective-C示例(如果可用)也將很有幫助(此處是您問題的更新或錯誤報告)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM