Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 22nd, 2012  |  syntax: None  |  size: 8.90 KB  |  hits: 57  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. iPhone development mpmovieplayer crashing
  2. *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An        AVPlayerItem cannot be associated with more than one instance of AVPlayer'
  3. *** First throw call stack:
  4. (0x380da8bf 0x37c261e5 0x30acbcb5 0x30abc1f7 0x30ac3bf3 0x30c93d55 0x30c95f7b 0x380ad2dd   0x380304dd 0x380303a5 0x37e07fcd 0x31bb0743 0x25e5 0x257c)
  5.        
  6. MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentOfURL:movieURL];
  7. if (player) {
  8.     [self setMoviePlayerController:player];
  9.     [self installMovieNotificationObservers];
  10.     [player setContentURL:movieURL];
  11.     [player setMovieSourceType:sourceType];
  12.     [self applyUserSettingsToMoviePlayer];
  13.     [self.view addSubview:self.backgroundView];
  14.     [player.view setFrame:self.view.bounds];
  15.     [player.view setBackgroundColor = [UIColor blackColor];
  16.     [self.view addSubview:player.view];
  17. }
  18.        
  19. [[self moviePlayerController] stop];
  20.  
  21. MPMoviePlayerController *player = [self moviePlayerController];
  22. [player.view removeFromSuperview];
  23.  
  24. [self removeMovieNotificationHandlers];
  25. [self setMoviePlayerController:nil];
  26.        
  27. @property (strong, nonatomic) MPMoviePlayerController * moviePlayerController;
  28.        
  29. self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentOfURL:movieURL];
  30.        
  31. //  MoviePlayer.h
  32.  
  33. #import <Foundation/Foundation.h>
  34. #import <MediaPlayer/MediaPlayer.h>
  35. #import "Logger.h"
  36.  
  37. @class MoviePlayerView;
  38.  
  39. @interface MoviePlayer : NSObject
  40. {
  41.   @private
  42.   MPMoviePlayerController *controller;
  43.   MoviePlayerView *currentView;
  44. }
  45.  
  46. @property (nonatomic, readonly) MPMoviePlayerController *controller;
  47.  
  48. +(MoviePlayer *) instance;
  49.  
  50. -(void) playMovie:(NSURL*)movieURL onView:(MoviePlayerView *)view;
  51. -(void) stopMovie;
  52.  
  53. @end
  54.  
  55.  
  56.  
  57. //  MoviePlayer.m
  58.  
  59.  
  60.  
  61.  
  62. #import "MoviePlayer.h"
  63. #import "MoviePlayerView.h"
  64.  
  65. @implementation MoviePlayer
  66.  
  67. @synthesize controller;
  68.  
  69. static MoviePlayer *player = nil;
  70.  
  71. #pragma mark Singleton management
  72.  
  73. +(MoviePlayer *) instance
  74. {
  75.   @synchronized([MoviePlayer class])
  76.   {
  77.     if (player == nil)
  78.     {
  79.       player = [[super allocWithZone:NULL] init];
  80.       player->controller = [[MPMoviePlayerController alloc] init];
  81.       player->controller.shouldAutoplay = NO;
  82.       player->controller.scalingMode = MPMovieScalingModeAspectFit;
  83.       player->currentView = nil;
  84.     }
  85.     return player;
  86.   }
  87. }
  88.  
  89. +(id) allocWithZone:(NSZone *)zone
  90. {
  91.   return [[self instance] retain];
  92. }
  93.  
  94. -(id) copyWithZone:(NSZone *)zone
  95. {
  96.   return self;
  97. }
  98.  
  99. -(id) retain
  100. {
  101.   return self;
  102. }
  103.  
  104. -(NSUInteger) retainCount
  105. {
  106.   return NSUIntegerMax;
  107. }
  108.  
  109. -(oneway void) release
  110. {
  111.   // singleton will never be released
  112. }
  113.  
  114. -(id) autorelease
  115. {
  116.   return self;
  117. }
  118.  
  119. #pragma mark MoviePlayer implementations
  120.  
  121. -(void) stopMovie
  122. {
  123.   @synchronized(self)
  124.   {
  125.     if (controller.view.superview)
  126.     {
  127.       [controller.view removeFromSuperview];
  128.     }
  129.     if (controller.playbackState != MPMoviePlaybackStateStopped)
  130.     {
  131.       [controller pause];
  132.       [controller stop];
  133.     }
  134.     if (currentView)
  135.     {
  136.       NSNotificationCenter *ntfc = [NSNotificationCenter defaultCenter];
  137.       [ntfc removeObserver:currentView name:MPMoviePlayerLoadStateDidChangeNotification object:controller];
  138.       [ntfc removeObserver:currentView name:MPMoviePlayerPlaybackStateDidChangeNotification object:controller];
  139.       currentView = nil;
  140.     }
  141.   }
  142. }
  143.  
  144. -(void) playMovie:(NSURL*)movieURL onView:(MoviePlayerView *)view
  145. {
  146.   @synchronized(self)
  147.   {
  148.     [self stopMovie];
  149.     currentView = view;
  150.  
  151.     NSNotificationCenter *ntfc = [NSNotificationCenter defaultCenter];
  152.     [ntfc addObserver:currentView
  153.              selector:@selector(loadStateDidChange:)
  154.                  name:MPMoviePlayerLoadStateDidChangeNotification
  155.                object:controller];
  156.     [ntfc addObserver:currentView
  157.              selector:@selector(playbackStateDidChange:)
  158.                  name:MPMoviePlayerPlaybackStateDidChangeNotification
  159.                object:controller];
  160.  
  161.     [controller setContentURL:movieURL];
  162.     controller.view.frame = view.bounds;
  163.     [view addSubview: controller.view];
  164.     [controller play];
  165.   }
  166. }
  167.  
  168. @end
  169.  
  170.  
  171. //  MoviePlayerView.h
  172.  
  173. #import <UIKit/UIKit.h>
  174. #import "MoviePlayer.h"
  175.  
  176.  
  177. @interface MoviePlayerView : MediaView
  178. {
  179.   NSURL *movieURL;
  180.   NSURL *thumbnailURL;
  181.   UIImageView *previewImage;
  182.   UIView *iconView;
  183.   BOOL hasPreviewImage;
  184. }
  185.  
  186. -(id) initWithFrame:(CGRect)frame thumbnailURL:(NSURL *)thumbnail movieURL:(NSURL *)movie;
  187. -(void) loadStateDidChange:(NSNotification *)ntf;
  188. -(void) playbackStateDidChange:(NSNotification *)ntf;
  189.  
  190. @end
  191.  
  192.  
  193.  
  194. //  MoviePlayerView.m
  195.  
  196. #import "MoviePlayerView.h"
  197.  
  198. @interface MoviePlayerView()
  199. -(void) initView;
  200. -(void) initController;
  201. -(void) playMovie;
  202. -(void) setActivityIcon;
  203. -(void) setMovieIcon:(float)alpha;
  204. -(void) clearIcon;
  205. -(CGPoint) centerPoint;
  206. @end
  207.  
  208. @implementation MoviePlayerView
  209.  
  210. -(id) initWithFrame:(CGRect)frame thumbnailURL:(NSURL *)thumbnail movieURL:(NSURL *)movie
  211. {
  212.   self = [super initWithFrame:frame];
  213.   if (self)
  214.   {
  215.     movieURL = [movie retain];
  216.     thumbnailURL = [thumbnail retain];
  217.     [self initView];
  218.     [self initController];
  219.     hasPreviewImage = NO;
  220.     loadingFinished = YES;
  221.   }
  222.   return self;
  223. }
  224.  
  225. -(void) dealloc
  226. {
  227.   [iconView release];
  228.   [previewImage release];
  229.   [movieURL release];
  230.   [super dealloc];
  231. }
  232.  
  233. -(void)initView
  234. {
  235.   self.backgroundColor = [UIColor blackColor];
  236.  
  237.   // add preview image view and icon view
  238.   previewImage = [[UIImageView alloc] initWithFrame:self.bounds];
  239.   [previewImage setContentMode:UIViewContentModeScaleAspectFit];
  240.   UIImage *img = nil;
  241.   if (thumbnailURL)
  242.   {
  243.     img = [ImageUtils loadImageFromURL:thumbnailURL];
  244.     if (img)
  245.     {
  246.       previewImage.image = img;
  247.       hasPreviewImage = YES;
  248.     }
  249.   }
  250.   [self addSubview:previewImage];
  251.   [self setMovieIcon:(hasPreviewImage ? 0.8f : 0.3f)];
  252. }
  253.  
  254. -(void)initController
  255. {
  256.   UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playMovie)];
  257.   [self addGestureRecognizer:rec];
  258.   [rec release];
  259. }
  260.  
  261. -(void)playMovie
  262. {
  263.   [[MoviePlayer instance] playMovie:movieURL onView:self];
  264.   [self setActivityIcon];
  265. }
  266.  
  267. -(void) loadStateDidChange:(NSNotification *)ntf
  268. {
  269.   MPMoviePlayerController *controller = [ntf object];
  270.   switch (controller.loadState)
  271.   {
  272.     case MPMovieLoadStatePlayable:
  273.     {
  274.       [self clearIcon];
  275.       [controller setFullscreen:YES animated:YES];
  276.       break;
  277.     }
  278.     case MPMovieLoadStateStalled:
  279.     {
  280.       [self setActivityIcon];
  281.       break;
  282.     }
  283.     default:
  284.     {
  285.       break; // nothing to be done
  286.     }
  287.   }
  288. }
  289.  
  290. -(void) playbackStateDidChange:(NSNotification *)ntf
  291. {
  292.   MPMoviePlayerController *controller = [ntf object];
  293.   switch (controller.playbackState)
  294.   {
  295.     case MPMoviePlaybackStatePlaying:
  296.     {
  297.       [self clearIcon];
  298.       break;
  299.     }
  300.     case MPMoviePlaybackStateStopped:
  301.     {
  302.       [self setMovieIcon:(hasPreviewImage ? 0.8f : 0.3f)];
  303.       break;
  304.     }
  305.     case MPMoviePlaybackStatePaused:
  306.     {
  307.       [self setMovieIcon:0.8f];
  308.       break;
  309.     }
  310.     case MPMoviePlaybackStateInterrupted:
  311.     {
  312.       [self setActivityIcon];
  313.       break;
  314.     }
  315.     default:
  316.     {
  317.       break; // nothing to be done
  318.     }
  319.   }
  320. }
  321.  
  322. -(void) setActivityIcon
  323. {
  324.   [self clearIcon];
  325.   iconView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  326.   iconView.center = [self centerPoint];
  327.   [self addSubview:iconView];
  328.   [iconView performSelector:@selector(startAnimating)];
  329. }
  330.  
  331. -(void) setMovieIcon:(float)alpha
  332. {
  333.   [self clearIcon];
  334.   iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_movie.png"]];
  335.   iconView.center = [self centerPoint];
  336.   iconView.alpha = alpha;
  337.   [self addSubview:iconView];
  338. }
  339.  
  340. -(void) clearIcon
  341. {
  342.   if (iconView)
  343.   {
  344.     SEL stop = @selector(stopAnimating);
  345.     if ([iconView respondsToSelector:stop])
  346.     {
  347.       [iconView performSelector:stop];
  348.     }
  349.     [iconView removeFromSuperview];
  350.     [iconView release];
  351.     iconView = nil;
  352.   }
  353. }
  354.  
  355. -(CGPoint) centerPoint
  356. {
  357.   return CGPointMake(roundf(self.bounds.size.width / 2.0f), roundf(self.bounds.size.height / 2.0f));
  358. }
  359.  
  360. -(void)resize
  361. {
  362.   for (UIView *view in [self subviews])
  363.   {
  364.     if (view == iconView)
  365.     {
  366.       iconView.center = [self centerPoint];
  367.       continue;
  368.     }
  369.     view.frame = self.bounds;
  370.   }
  371.   [self addCaptionLabel];
  372. }
  373.  
  374. -(void) layoutSubviews
  375. {
  376.   [super layoutSubviews];
  377.   [self resize];
  378. }
  379.  
  380. @end
  381.        
  382. [self.moviePlayerController setContentURL:movieURL];
  383.        
  384. - (void) moviePlayBackDidFinish:(NSNotification*)notification
  385. {
  386.     self.moviePlayer = nil;
  387.     [self initanothermovieplayerandplay];
  388. }
  389.        
  390. - (void) moviePlayBackDidFinish:(NSNotification*)notification
  391. {
  392.     [UIView animateWithDuration:1
  393.                       delay: 0.0
  394.                     options: UIViewAnimationOptionCurveEaseIn
  395.                  animations:^{
  396.                      // one second to fade out the view
  397.                      self.moviePlayer.view.alpha = 0.0;
  398.                  }
  399.                  completion:^(BOOL finished){
  400.                        self.moviePlayer = nil;
  401.                        [self initanothermovieplayerandplay];
  402.                  }
  403. }