0

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

一般流程非常简单:

  • 我将视频加载到AVAsset
  • 然后我将视频轨道放入AVAssetReaderTrackOutput
  • 我把它插入一个AVAssetReader
  • 我开始阅读
  • 对于每个视频帧,我得到一个CMSampleBuffer,从中我得到一个CVPixelBuffer
  • 然后我锁定CVPixelBuffer, 并将其上传到 OpenGLGL.TexImage2D

它在模拟器中运行良好,但是当我在实际设备中尝试时,第一步出现以下错误:

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 ();
}

我将它放入 anobject而不是AVAsset直接放入,只是为了确保InvalidCastException不会在我身边抛出,但正如异常读取的那样,它被抛出在AVAsset.g.cs我没有的第 231 行。

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

关于可能导致这种情况的任何想法?

4

1 回答 1

0

不如代替...

object videoAssetRaw = AVAsset.FromUrl(videoURL);

尝试 ...

AVUrlAssetOptions options = null;
var asset = new AVUrlAsset (url, options);
于 2013-09-08T20:16:22.820 回答