添加观察器时 AVPlayer 崩溃



我在AVPlayer上遇到了一个奇怪的问题。我正在使用AVplayer播放在我的应用程序中下载的视频。下面的代码是我设置 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。问题是当我尝试暂停 AVP layer。它会导致 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.
=================================================================

您在函数调用中跳过了observer

而不是

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

尝试

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

最新更新