MonoTouch的AVFoundation.AVAsset.FromUrl中的InvalidCastException

时间:2013-08-22 10:12:55

标签: c# xamarin.ios avfoundation

我正在尝试一个非常简单的程序:使用AVFoundation加载资源视频,并将视频帧字节作为OpenGL纹理。

总体流程非常简单:

  • 我将视频加载到AVAsset
  • 然后我将视频曲目转换为AVAssetReaderTrackOutput
  • 我将其插入AVAssetReader
  • 我开始阅读
  • 对于每个视频帧,我得到一个CMSampleBuffer,我从中获得CVPixelBuffer
  • 然后我锁定CVPixelBuffer,并使用GL.TexImage2D
  • 将其上传到OpenGL

它在模拟器中工作得很好,但是当我在实际设备中尝试它时,我在第一步中遇到以下错误:

System.InvalidCastException: Cannot cast from source type to destination type.
  at MonoTouch.AVFoundation.AVAsset.FromUrl (MonoTouch.Foundation.NSUrl url) [0x00011] in /Developer/MonoTouch/Source/monotouch/src/AVFoundation/AVAsset.g.cs:231
  at MyTestApp.EAGLView.LoadVideo () [0x00033] in /Volumes/Shared/MyTestApp/MyTestApp/EAGLView.cs:191

抛出异常的代码如下:

private AVAsset videoAsset;
private AVAssetReaderTrackOutput videoReaderOutput;
private AVAssetReader videoReader;

private void LoadVideo()
{
    var videoPath = NSBundle.MainBundle.PathForResource("video", "mp4");
    var videoURL = NSUrl.FromFilename(videoPath);
    Console.WriteLine ("Loading from " + videoURL.ToString ());
    object videoAssetRaw = AVAsset.FromUrl(videoURL); // Exception thrown here
    videoAsset = videoAssetRaw as AVAsset;
    var tracks = videoAsset.Tracks;
    NSError error;
    AVAssetTrack videoTrack = tracks[0];

    AVVideoSettings settings = new AVVideoSettings ();
    settings.PixelFormat = CVPixelFormatType.CV32BGRA;

    videoReaderOutput = AVAssetReaderTrackOutput.FromTrack(videoTrack, settings.ToDictionary());
    videoReader = new AVAssetReader (videoAsset, out error);
    videoReader.AddOutput (videoReaderOutput);
    videoReader.StartReading ();
}

我直接将其放入object而不是AVAsset,只是为了确保InvalidCastException没有被抛到我身边,但正如异常所示,它是在AVAsset.g.cs的第231行抛出,我没有。

视频文件正在构建为“BundleResource”,我正在使用最新版本的xamarin(今天早些时候下载)。

关于可能导致此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

怎么样而不是......

object videoAssetRaw = AVAsset.FromUrl(videoURL);

尝试......

AVUrlAssetOptions options = null;
var asset = new AVUrlAsset (url, options);