cocoa touch - An AVPlayerItem cannot be associated with more than one instance of AVPlayer in iOS 8.4 -


after updating ios 8.4 getting infamous exception mpmovieplayercontroller says:

an avplayeritem cannot associated more 1 instance of avplayer

i have seen several workarounds consist of reinitialising player before reusing it. however, me crash doesn't happen when try play new video, rather when turn off fullscreen player rotating portrait mode.

this code:

@implementation movieplayerviewcontroller  -(void)viewdidload {     [super viewdidload];      self.movieplayer.controlstyle = mpmoviecontrolstyleembedded;      [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(movieplayerwillenterfullscreennotification:) name:mpmovieplayerwillenterfullscreennotification object:nil];     [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(movieplayerwillexitfullscreennotification:) name:mpmovieplayerwillexitfullscreennotification object:nil]; }  - (void) movieplayerwillenterfullscreennotification:(nsnotification*)notification {     [[uidevice currentdevice] begingeneratingdeviceorientationnotifications];     [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(deviceorientationdidchange) name:uideviceorientationdidchangenotification object:nil]; }  - (void) movieplayerwillexitfullscreennotification:(nsnotification*)notification {     [[uidevice currentdevice] endgeneratingdeviceorientationnotifications];     [[nsnotificationcenter defaultcenter] removeobserver:self name:uideviceorientationdidchangenotification object:nil]; }  - (void)deviceorientationdidchange {     uideviceorientation orientation = [[uidevice currentdevice] orientation];     if (orientation == uideviceorientationportrait) {         [self.movieplayer setfullscreen:no animated:yes];     } }  @end 

the change fullscreen happens in uiviewcontroller has movieplayerviewcontroller u subview:

-(void)didrotatefrominterfaceorientation:(uiinterfaceorientation)frominterfaceorientation {     if (!self.movieplayerviewcontroller.movieplayer.fullscreen &&         uiinterfaceorientationisportrait(frominterfaceorientation)) {             [self.movieplayerviewcontroller.movieplayer setfullscreen:yes animated:yes];     } } 

there no problem when go in or out of fullscreen manually using fullscreen-button in player. can rotate player fullscreen fine. however, when try rotate out of fullscreen (i.e. landscape portrait) exception, seemingly in line:

[self.movieplayer setfullscreen:no animated:yes]; 

here stack trace when exception occurs:

thread : fatal exception: nsinvalidargumentexception 0  corefoundation                 0x00000001865e02d8 __exceptionpreprocess 1  libobjc.a.dylib                0x0000000197f3c0e4 objc_exception_throw 2  avfoundation                   0x0000000184db4b50 -[avplayeritem _attachtofigplayer] 3  avfoundation                   0x0000000184da7770 -[avplayer _attachitem:andperformoperation:withobject:] 4  avfoundation                   0x0000000184dc8f00 -[avqueueplayer insertitem:afteritem:] 5  mediaplayer                    0x00000001889d1d30 -[mpqueueplayer insertitem:afteritem:] 6  mediaplayer                    0x000000018893de7c -[mpavqueuecoordinator _syncplayeritems] 7  mediaplayer                    0x000000018893d8a4 -[mpavqueuecoordinator _syncitems] 8  mediaplayer                    0x000000018893c68c -[mpavqueuecoordinator reloaditemskeepingcurrentitem:] 9  mediaplayer                    0x000000018899fd38 -[mpavplaylistmanager setplaylistfeeder:startindex:keepplaying:] 10 mediaplayer                    0x000000018899fb4c __67-[mpavplaylistmanager reloadwithplaybackcontext:completionhandler:]_block_invoke 11 mediaplayer                    0x000000018889fa5c -[mparrayqueuefeeder reloadwithplaybackcontext:completionhandler:] 12 mediaplayer                    0x000000018899f9b4 -[mpavplaylistmanager reloadwithplaybackcontext:completionhandler:] 13 mediaplayer                    0x00000001888b7550 -[mpavcontroller reloadwithplaybackcontext:completionhandler:] 14 mediaplayer                    0x000000018888d114 -[mpmovieplayercontrollernew _preparetoplaywithstartindex:] 15 mediaplayer                    0x000000018888a988 -[mpmovieplayercontrollernew _movieplayerdidbecomeactivenotification:] 16 corefoundation                 0x00000001865862c4 __cfnotificationcenter_is_calling_out_to_an_observer__ 17 corefoundation                 0x00000001864c3450 _cfxnotificationpost 18 foundation                     0x00000001873f2a80 -[nsnotificationcenter postnotificationname:object:userinfo:] 19 mediaplayer                    0x000000018888d530 -[mpmovieplayercontrollernew _postnotificationname:object:userinfo:] 20 mediaplayer                    0x000000018888d494 -[mpmovieplayercontrollernew _postnotificationname:object:] 21 mediaplayer                    0x00000001888878dc -[mpmovieplayercontrollernew setfullscreen:animated:] 22 myapp                          0x000000010004ddf8 -[movieplayerviewcontroller deviceorientationdidchange] (movieplayerviewcontroller.m:36) 23 corefoundation                 0x00000001865862c4 __cfnotificationcenter_is_calling_out_to_an_observer__ 24 corefoundation                 0x00000001864c3450 _cfxnotificationpost 25 foundation                     0x00000001873f2a80 -[nsnotificationcenter postnotificationname:object:userinfo:] 26 uikit                          0x000000018b059b34 -[uidevice setorientation:animated:] 27 uikit                          0x000000018b0597f0 -[uiapplication handleevent:withnewevent:] 28 uikit                          0x000000018b059080 -[uiapplication sendevent:] 29 uikit                          0x000000018b0c52c4 _uiapplicationhandleevent 30 graphicsservices               0x000000018fdc9194 _purpleeventcallback 31 graphicsservices               0x000000018fdc8c84 purpleeventcallback 32 corefoundation                 0x0000000186597a54 __cfrunloop_is_calling_out_to_a_source1_perform_function__ 33 corefoundation                 0x00000001865979b4 __cfrunloopdosource1 34 corefoundation                 0x0000000186595934 __cfrunlooprun 35 corefoundation                 0x00000001864c12d4 cfrunlooprunspecific 36 graphicsservices               0x000000018fdc76fc gseventrunmodal 37 uikit                          0x000000018b0bef40 uiapplicationmain 38 myapp                          0x000000010002b2dc main (main.m:16) 39 libdyld.dylib                  0x00000001985e6a08 start 

the solution is:

don't use mpmovieplayercontroller, sorry. refactor use avplayerviewcontroller instead. more modern api; 1 using has been deprecated not shocking has strange mysterious crash on newer version of ios.


Comments