添加观察者时AVPlayer崩溃

时间:2017-07-12 12:53:30

标签: ios iphone xamarin xamarin.ios

我遇到了AVPlayer的一个奇怪问题。我正在使用AV播放器播放在我的应用程序中下载的视频。下面的代码是我如何设置AVPlayer。

var filePath = new NSString("file://" + filePathOnDisk);
  var fileUrl = new NSUrl(filePath);

_asset = AVAsset.FromUrl(fileUrl);
_playerItem = new AVPlayerItem(_asset);

_player = new AVPlayer(_playerItem);
_playerStateObserver = _player.AddObserver("status", NSKeyValueObservingOptions.New, PlayerReadyObserver);

_playerLayer = AVPlayerLayer.FromPlayer(_player);
_playerLayer.Frame = Frame;
Layer.AddSublayer(_playerLayer);

当我在上面第6行添加观察者时,观察者工作正常并调用我分配给它的Action。问题是当我尝试暂停AVPlayer时。它导致Mono运行时崩溃,无法用C#中的try / catch捕获。

我已经确认如果我不添加观察者,我可以暂停AVPlayer而不会发生任何崩溃,但是当玩家准备好玩时,我不知道该怎么听。

添加一个观察者会返回一个IDisposable,我也有一个参考。如果我在调用Pause之前处理该变量,那么就不会发生崩溃。任何人都可以解释为什么会发生这种情况。下面是发生此崩溃时的崩溃日志。

任何见解都将受到赞赏。 谢谢。

以下的调用

2017-07-12 14:36:52.789 Educor.Mobile.Ios[8803:411224] critical: Stacktrace:

2017-07-12 14:36:52.789 Educor.Mobile.Ios[8803:411224] critical:   at <unknown> <0xffffffff>
2017-07-12 14:36:52.790 Educor.Mobile.Ios[8803:411224] critical:   at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSend (intptr,intptr) [0x0000a] in <58a9bd8a3cbb4d0092bc0a767ff66c0f>:0
2017-07-12 14:36:52.790 Educor.Mobile.Ios[8803:411224] critical:   at AVFoundation.AVPlayer.Pause () [0x00003] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/build/ios/native/AVFoundation/AVPlayer.g.cs:196
2017-07-12 14:36:52.790 Educor.Mobile.Ios[8803:411224] critical:   at Educor.Mobile.Ios.CustomViews.BindableViews.BindableNativeVideoPlayer.TogglePlayState () [0x0000d] in /Users/danemackier/MyFiles/Work/Clients/Educor/MoodleApp/MoodleSrc/Educor.Mobile.Ios/CustomViews/BindableViews/BindableNativeVideoPlayer.cs:71
2017-07-12 14:36:52.790 Educor.Mobile.Ios[8803:411224] critical:   at Educor.Mobile.Ios.Views.NativeVideoPlayer.NativeVideoPlayerView.<HandlePlayPauseUi>b__8_0 () [0x00001] in /Users/danemackier/MyFiles/Work/Clients/Educor/MoodleApp/MoodleSrc/Educor.Mobile.Ios/Views/NativeVideoPlayer/NativeVideoPlayerView.cs:68
2017-07-12 14:36:52.790 Educor.Mobile.Ios[8803:411224] critical:   at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/Foundation/NSAction.cs:163
2017-07-12 14:36:52.791 Educor.Mobile.Ios[8803:411224] critical:   at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) [0x0004f] in <2f4074c3120b4d80802e10af84b67d41>:0
2017-07-12 14:36:52.791 Educor.Mobile.Ios[8803:411224] critical:   at <unknown> <0xffffffff>
2017-07-12 14:36:52.791 Educor.Mobile.Ios[8803:411224] critical:   at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) [0x0005c] in <58a9bd8a3cbb4d0092bc0a767ff66c0f>:0
2017-07-12 14:36:52.791 Educor.Mobile.Ios[8803:411224] critical:   at UIKit.UIApplication.Main (string[],intptr,intptr) [0x00005] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/UIKit/UIApplication.cs:79
2017-07-12 14:36:52.791 Educor.Mobile.Ios[8803:411224] critical:   at UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/UIKit/UIApplication.cs:63
2017-07-12 14:36:52.791 Educor.Mobile.Ios[8803:411224] critical:   at Educor.Mobile.Ios.Application.Main (string[]) [0x00001] in /Users/danemackier/MyFiles/Work/Clients/Educor/MoodleApp/MoodleSrc/Educor.Mobile.Ios/Main.cs:12
2017-07-12 14:36:52.792 Educor.Mobile.Ios[8803:411224] critical:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) [0x00051] in <075daed4d09344098906c84c2a5b9b38>:0
2017-07-12 14:36:52.792 Educor.Mobile.Ios[8803:411224] critical: 
Native stacktrace:

2017-07-12 14:36:52.802 Educor.Mobile.Ios[8803:411224] critical:    0   libmonosgen-2.0.dylib               0x00000001054030f1 mono_handle_native_crash + 257
2017-07-12 14:36:52.802 Educor.Mobile.Ios[8803:411224] critical:    1   libmonosgen-2.0.dylib               0x000000010535e090 mono_sigsegv_signal_handler + 288
2017-07-12 14:36:52.802 Educor.Mobile.Ios[8803:411224] critical:    2   libsystem_platform.dylib            0x0000000109b3cbba _sigtramp + 26
2017-07-12 14:36:52.802 Educor.Mobile.Ios[8803:411224] critical:    3   libmonosgen-2.0.dylib               0x0000000105707428 mono_defaults + 0
2017-07-12 14:36:52.803 Educor.Mobile.Ios[8803:411224] critical:    4   AVFoundation                        0x00000001025def4d -[AVPlayer willChangeValueForKey:] + 86
2017-07-12 14:36:52.803 Educor.Mobile.Ios[8803:411224] critical:    5   AVFoundation                        0x00000001025e5615 -[AVPlayer setRate:withVolumeRampDuration:playImmediately:] + 90
2017-07-12 14:36:52.803 Educor.Mobile.Ios[8803:411224] critical:    6   AVFoundation                        0x00000001025e598a -[AVPlayer setRate:] + 79
2017-07-12 14:36:52.803 Educor.Mobile.Ios[8803:411224] critical:    7   libxamarin-debug.dylib              0x0000000105853a59 xamarin_dyn_objc_msgSend + 217
2017-07-12 14:36:52.803 Educor.Mobile.Ios[8803:411224] critical:    8   ???                                 0x000000011545587e 0x0 + 4651833470
2017-07-12 14:36:52.804 Educor.Mobile.Ios[8803:411224] critical:    9   ???                                 0x000000011674d85e 0x0 + 4671723614
2017-07-12 14:36:52.804 Educor.Mobile.Ios[8803:411224] critical:    10  ???                                 0x00000001147f5584 0x0 + 4638856580
2017-07-12 14:36:52.804 Educor.Mobile.Ios[8803:411224] critical:    11  libmonosgen-2.0.dylib               0x0000000105361045 mono_jit_runtime_invoke + 1301
2017-07-12 14:36:52.804 Educor.Mobile.Ios[8803:411224] critical:    12  libmonosgen-2.0.dylib               0x000000010553cbf8 do_runtime_invoke + 88
2017-07-12 14:36:52.804 Educor.Mobile.Ios[8803:411224] critical:    13  libmonosgen-2.0.dylib               0x000000010553cb70 mono_runtime_invoke + 208
2017-07-12 14:36:52.805 Educor.Mobile.Ios[8803:411224] critical:    14  libxamarin-debug.dylib              0x000000010584a2a5 xamarin_invoke_trampoline + 5589
2017-07-12 14:36:52.805 Educor.Mobile.Ios[8803:411224] critical:    15  libxamarin-debug.dylib              0x000000010585250d xamarin_arch_trampoline + 189
2017-07-12 14:36:52.805 Educor.Mobile.Ios[8803:411224] critical:    16  libxamarin-debug.dylib              0x00000001058538d1 xamarin_x86_64_common_trampoline + 110
2017-07-12 14:36:52.805 Educor.Mobile.Ios[8803:411224] critical:    17  Foundation                          0x000000010293fe9e __NSThreadPerformPerform + 326
2017-07-12 14:36:52.805 Educor.Mobile.Ios[8803:411224] critical:    18  CoreFoundation                      0x0000000104ef9761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
2017-07-12 14:36:52.805 Educor.Mobile.Ios[8803:411224] critical:    19  CoreFoundation                      0x0000000104ede98c __CFRunLoopDoSources0 + 556
2017-07-12 14:36:52.806 Educor.Mobile.Ios[8803:411224] critical:    20  CoreFoundation                      0x0000000104edde76 __CFRunLoopRun + 918
2017-07-12 14:36:52.806 Educor.Mobile.Ios[8803:411224] critical:    21  CoreFoundation                      0x0000000104edd884 CFRunLoopRunSpecific + 420
2017-07-12 14:36:52.806 Educor.Mobile.Ios[8803:411224] critical:    22  GraphicsServices                    0x0000000106949a6f GSEventRunModal + 161
2017-07-12 14:36:52.806 Educor.Mobile.Ios[8803:411224] critical:    23  UIKit                               0x0000000100d21c68 UIApplicationMain + 159
2017-07-12 14:36:52.806 Educor.Mobile.Ios[8803:411224] critical:    24  ???                                 0x0000000114d4a37c 0x0 + 4644447100
2017-07-12 14:36:52.806 Educor.Mobile.Ios[8803:411224] critical:    25  ???                                 0x0000000114d49fbd 0x0 + 4644446141
2017-07-12 14:36:52.806 Educor.Mobile.Ios[8803:411224] critical: 
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

1 个答案:

答案 0 :(得分:1)

您在函数调用中跳过observer

而不是

_playerStateObserver = _player.AddObserver("status", NSKeyValueObservingOptions.New, PlayerReadyObserver);

尝试

_playerStateObserver = _player.AddObserver(yourObserver, "status", NSKeyValueObservingOptions.New, nil);