AVPlayerItem的loadedTimeRanges属性在MonoTouch& objective-c选择器映射

时间:2012-01-09 13:33:22

标签: c# objective-c xamarin.ios avplayer

我需要使用AVPlayerItem类的loadedTimeRanges属性来知道文件的哪个部分已经下载。不幸的是,MonoTouch似乎没有这个属性。

MonoTouch Rosetta Stone reference中,该方法存在,但已注释:

选择器:loadedTimeRanges
Property:// NSArray loadedTimeRanges {get; }

有人知道将来是否会添加该方法?

与此同时,我尝试根据xamarin doc

映射objective-c选择器

我定义了缺少的选择器:

[System.Runtime.InteropServices.DllImport(MonoTouch.Constants.ObjectiveCLibrary, EntryPoint="objc_msgSend")]
    static extern NSArray nsarray_objc_msgSend (
    IntPtr target, IntPtr selector);

之后,称之为:

/* Call loadedTimeRanges selector on AVPlayerItem */
AVPlayerItem target = m_playerItem;
MonoTouch.ObjCRuntime.Selector selector = new MonoTouch.ObjCRuntime.Selector ("loadedTimeRanges");
NSArray loadedTimeRanges = nsarray_objc_msgSend(target.Handle, selector.Handle);


/* Convert the returned value as CMTimeRange */
IntPtr intPtr = loadedTimeRanges.ValueAt(0);
System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.FromIntPtr(intPtr);
NSValue v = (NSValue)handle.Target;
MonoTouch.CoreMedia.CMTimeRange timeRange = v.CMTimeRangeValue;

但是,在调用我的选择器时出现以下错误:

Unhandled Exception: System.Runtime.InteropServices.MarshalDirectiveException: Type MonoTouch.Foundation.NSObject which is passed to unmanaged code must have a StructLayout attribute.

有人知道我失踪了吗?提前谢谢!

1 个答案:

答案 0 :(得分:2)

您无法从DllImport返回NSArray,您需要将IntPtr声明为返回类型。在任何情况下,您都不必自己添加DllImport,它已经可以使用了:

IntPtr ptr = MonoTouch.ObjCRuntime.Messaging.IntPtr_objc_msgSend (target.Handle, selector.Handle);
NSArray loadedTimesRange = new NSArray (ptr);

在这种情况下,这甚至更好:

NSValue [] values = NSArray.ArrayFromHandle<NSValue> (ptr);

单个值为:

CMTime timestamp = values [0].CMTimeValue;