我正在尝试一个非常简单的程序:使用AVFoundation加载资源视频,并将视频帧字节作为OpenGL纹理。
总体流程非常简单:
AVAsset
AVAssetReaderTrackOutput
AVAssetReader
CMSampleBuffer
,我从中获得CVPixelBuffer
。CVPixelBuffer
,并使用GL.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 ();
}
我直接将其放入object
而不是AVAsset
,只是为了确保InvalidCastException
没有被抛到我身边,但正如异常所示,它是在AVAsset.g.cs
的第231行抛出,我没有。
视频文件正在构建为“BundleResource”,我正在使用最新版本的xamarin(今天早些时候下载)。
关于可能导致此问题的任何想法?
答案 0 :(得分:0)
怎么样而不是......
object videoAssetRaw = AVAsset.FromUrl(videoURL);
尝试......
AVUrlAssetOptions options = null;
var asset = new AVUrlAsset (url, options);