ios - 一个 AVPlayerItem 不能与 iOS 8.4 中的多个 AVPlayer 实例相关联

标签 ios cocoa-touch mpmovieplayercontroller avplayer ios8.4

更新到 iOS 8.4 后,我收到了臭名昭著的 MPMoviePlayerController 异常:

一个 AVPlayerItem 不能与多个 AVPlayer 实例相关联

我见过几种解决方法,主要包括在重新使用播放器之前重新初始化播放器。但是,对我来说,当我尝试播放新视频时不会发生崩溃,而是当我通过旋转到纵向模式为播放器关闭全屏时发生。

这是我的代码:

@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

全屏更改发生在 UIViewController 中,它有 MoviePlayerViewController 作为您的 subview :

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if (!self.moviePlayerViewController.moviePlayer.fullscreen &&
        UIInterfaceOrientationIsPortrait(fromInterfaceOrientation)) {
            [self.moviePlayerViewController.moviePlayer setFullscreen:YES animated:YES];
    }
}

当我使用播放器中的全屏按钮手动进入或退出全屏时没有问题。我也可以将播放器旋转到全屏。但是,当我尝试将它旋转出全屏(即从横向到纵向)时,我得到了异常,似乎在行中:

[self.moviePlayer setFullscreen:NO animated:YES];

这是发生异常时我的堆栈跟踪:

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

最佳答案

解决方法是:

不要使用 MPMoviePlayerController,抱歉。重构为使用 AVPlayerViewController。这是一个更现代的 API;您正在使用的那个已被弃用,因此它在较新版本的 iOS 上发生奇怪的神秘崩溃并不令人震惊。

关于ios - 一个 AVPlayerItem 不能与 iOS 8.4 中的多个 AVPlayer 实例相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31337858/

相关文章:

iphone - 如何从 UITabBarController 呈现 MPMoviePlayerViewController?

iphone - 如何自定义 MPMoviePlayerController 背景和行为的进度条?

cocoa-touch - 在 IOS4.0 中使用 UIWebView 加载请求时发生内存泄漏

iphone - NSLayoutConstraint 和动态 UITableViewCell 高度

iphone - 在 float 和 float 之间获取随机 float 的最佳方法是什么?

ios - 进入全屏时 iPhone 上的 MPMoviePlayerController 暂停

ios - UITableView 和 UITextField borderColor 问题

iphone - 如何在 iphone 中单击左侧 Controller 的 TableView 行更改中心 View ?(MFSideMenu)

iphone - ImagePicker 中的 View 层次问题

javascript - window.open 在 phonegap