Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS 8.4中,AVPlayerItem不能与多个AVPlayer实例关联_Ios_Cocoa Touch_Mpmovieplayercontroller_Avplayer_Ios8.4 - Fatal编程技术网

在iOS 8.4中,AVPlayerItem不能与多个AVPlayer实例关联

在iOS 8.4中,AVPlayerItem不能与多个AVPlayer实例关联,ios,cocoa-touch,mpmovieplayercontroller,avplayer,ios8.4,Ios,Cocoa Touch,Mpmovieplayercontroller,Avplayer,Ios8.4,升级到iOS 8.4后,我得到了一个臭名昭著的异常,即MPMoviePlayerController,它说: 一个AVPlayerItem不能与多个AVPlayer实例关联 我已经看到了一些解决方法,主要包括在重用播放器之前重新初始化它。然而,对我来说,当我尝试播放新视频时,崩溃不会发生,而是当我通过旋转到纵向模式关闭播放器的全屏时 这是我的代码: @implementation MoviePlayerViewController -(void)viewDidLoad { [super

升级到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
作为u子视图时,将更改为全屏:

-(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。重构以使用
avplayervewcontroller
。这是一个更现代的API;您正在使用的版本已被弃用,因此它在较新版本的iOS上出现奇怪的神秘崩溃并不令人震惊。

我遇到了一个您听起来有些熟悉的问题,但可能完全相同,也可能不完全相同。我有多个视图控制器,我正在安排一个视图控制器暂停它的电影,以便另一个可以播放。但是,当设备旋转时,我使用的标准iOS选项卡栏控制器将加载选项卡栏上任何其他选项卡的其他视图控制器(我相信称为viewDidLoad),如果它们尚未加载。我没有预料到这种行为,因为我从未选择过选项卡。我加载电影的代码在viewDidLoad中,因此它试图在另一个视图控制器上开始播放另一部从未被要求“显示”的电影。我花了一段时间才意识到发生了什么,因为这两个视图控制器继承自同一个基类,并且它看起来像调试器中的正确对象,直到我仔细观察


如果内存可用,我移动了代码以加载并开始在ViewWillDisplay中播放我的电影,这样它就不会执行,除非真正选择了选项卡。然后,当设备旋转,另一个视图控制器突然加载时,它没有任何不良副作用。但是看到它这样做还是很奇怪。

请在抛出异常时添加堆栈跟踪。在lldb提示符下键入
bt all
以获取它。堆栈跟踪就像apple支持一样是的,堆栈跟踪类似于evan提供的跟踪。然而,我实际上已经决定完全放弃
MPMoviePlayerController
,用
AVPlayer
avplayercontroller
替换它
MPMoviePlayerController
在iOS9中已被弃用,因此我不会在必须更换的东西上浪费更多时间。@NobleK-仅供参考,我正在使用
AVPlayer
,并看到了
AVPlayer
AVPlayerItem
的相同崩溃。根本原因基本上是描述中所说的——一个playerItem实例不能附加到多个player实例。有三个API调用可以将playerItem附加到播放器:
+playerWithPlayerItem:
-initWithPlayerItem:
-replaceCurrentItemWithPlayerItem:
。请注意确保一旦playerItem连接到某个播放器,您就不会尝试将其连接到另一个播放器。Anurag,我最初的问题是在使用
MPMoviePlayerController
时,这意味着我与
AVPlayer
AVPlayerItem
都没有直接联系。此外,虽然人们在播放新视频时经常报告此问题,但对我来说,它发生在其他情况下,如退出全屏时。对我来说,这真的像是一个bug,因为异常出现在我没有启动任何新流或任何东西的时候。此外,该问题在iOS 8.4之前从未出现过。
avplayervcontroller
仅在iOS 8和更高版本上可用。该问题专门询问iOS 8.4。该问题询问应用程序在iOS 8.4上运行时的行为,但应用程序的部署目标不一定是iOS 8或更高版本。但认真地说,对建议某人远离不推荐的API投了反对票。根据定义,“不推荐”意味着它应该可以工作。如果它不起作用,那么它将被“删除”。