MPViewController showing black screen - ios

I'd seen many people facing this issue. Have tried implementing some of their solutions (declaring a property for moviePlayer, explicitly set _moviePlayer.contentURL. But its still showing a black screen for me (iOS7).
Here's the code:
MoviePlayer.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface MoviePlayer : UIViewController
#property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
#property (nonatomic,weak) NSURL *vidURL;
#end
MoviePlayer.m
#import "MoviePlayer.h"
#interface MoviePlayer ()
#end
#implementation MoviePlayer
-(void)viewDidLoad {
[super viewDidLoad];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:_vidURL];
NSLog(#"%#", _vidURL);
_moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(90*M_PI/180.0f);
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
_moviePlayer.view.transform = landscapeTransform;
_moviePlayer.contentURL = _vidURL;
[_moviePlayer.view setFrame:self.view.bounds];
[self.view addSubview:_moviePlayer.view];
}
#end
Print out of the NSLog:
assets-library://asset/asset.mov?id=07E61ABA-3EEB-461B-B170-000E7AE3F9A8&ext=mov
Update:
vidURL passed from another viewControlelr:
-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
ALAsset *asset = self.assets[indexPath.row];
ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
NSURL *vidURL = [defaultRep url];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle:nil];
MoviePlayer *moviePController = [storyboard instantiateViewControllerWithIdentifier:#"moviePlayer"];
moviePController.vidURL = vidURL;
[self.navigationController presentViewController:moviePController animated:YES completion:nil];
}

Try registering for MPMoviePlayerLoadStateDidChangeNotification
and MPMoviePlayerPlaybackDidFinishNotification
and use
if (self.player.loadState ==MPMoviePlaybackStateInterrupted) {
NSLog(#"MPMoviePlaybackStateInterrupted");
}else if (self.player.loadState ==MPMoviePlaybackStatePaused) {
// [self RemoveVideoCache];
NSLog(#"MPMoviePlaybackStatePaused");
}else if (self.player.loadState ==MPMoviePlaybackStateSeekingBackward) {
// [self RemoveVideoCache];
NSLog(#"MPMoviePlaybackStateSeekingBackward");
}
else if (self.player.loadState ==MPMoviePlaybackStateSeekingForward) {
NSLog(#"MPMoviePlaybackStateSeekingForward");
}else if (self.player.loadState ==MPMoviePlaybackStateStopped) {
NSLog(#"MPMoviePlaybackStateStopped");
}

Turns out I just had to play it after adding it as a subview:
[_moviePlayer play];

Related

MPMediaPicker does not show

In a sample app that I'm making for Apple, I cannot get the MPMediaPickerController to show.
I've already added the NSAppleMusicUsageDescription key in the info.plist file, and this is what makes my post different from any other answers found online.
I've also tried adding CoreMedia.framework.
I'm using XCode 10 beta 4
The app never asks for permissions to access the media library, and when I present the picker, the mediaPickerDidCancel method is called right away.
What am I doing wrong?
Thanks for any help!
Header file:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MPMediaPickerController.h>
#interface ViewController : UIViewController<MPMediaPickerControllerDelegate>
#end
Objc file:
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController ()
#end
#implementation ViewController {
MPMediaPickerController *picker;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)selectButtonPressed:(id)sender {
picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAnyAudio];
[picker setDelegate: self];
[picker setAllowsPickingMultipleItems: NO];
picker.prompt = #"Select a Song.";
UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootController presentViewController:picker animated:YES completion:^{
NSLog(#"Complete!");
}];
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) collection {
MPMediaItem *firstItem;
for (MPMediaItem *item in collection.items) {
firstItem = item;
break;
}
MPMusicPlayerController *samplePlayer = [MPMusicPlayerController applicationMusicPlayer];
[samplePlayer setShuffleMode: MPMusicShuffleModeOff];
[samplePlayer setRepeatMode: MPMusicRepeatModeOne];
[samplePlayer beginGeneratingPlaybackNotifications];
// self.mediaItem chosen using MPMediaPickerController
[samplePlayer setQueueWithItemCollection:[[MPMediaItemCollection alloc] initWithItems:#[firstItem]]];
[samplePlayer prepareToPlay];
// Assume that song is at least 120 seconds long
[samplePlayer play];
}
#pragma mark - delegate methods and segues
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker {
[self dismissViewControllerAnimated:true completion:^{
}];
}
#end

Why does NavgationItem reference disappears?

I created a NavigationBar and added it to the UIViewController. But after init, the reference turns to nil. I'm new to iOS and OC, I don't know why. Anyone can help? Thank you.
code summary:
#interface ContainerViewController()
#property (nonatomic, retain) UINavigationBar *nav;
#property (nonatomic, retain) UINavigationItem *navItem;
#end
#implementation ContainerViewController
- (instancetype) initWithParams:(NSDictionary *)params {
self = [super init];
if (self) {//...}
return self;
}
- setNavTitle:(NSDictionary *) params {
NSString *title = params[#"title"];
/////////////////////////////////
// here goes wrong
// self.navItem == nil here, why?
/////////////////////////////////
self.navItem.title = title;
}
- (void) viewWillAppear:(Bool)animated {
[super viewWillAppear:NO];
static float navHeight = 64.0;
UIViewController *wvController = [WebView init here];
UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), navHeight)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:title];
nav.items = [NSArray arrayWithObjects: navItem, nil];
///////////////////////////////
// I saved the reference here
//////////////////////////////
[self setNav:nav];
[self setNavItem:navItem];
[self.view addSubview:nav];
[self addChildViewController:wvController];
wvController.view.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - navHeight);
[self.view addSubview:wvController.view];
[wvController didMoveToParentViewController:self];
}
#end
This will be useful for you, kindly check and do
Tutorial point site is very easy to learn some important UI basics if you are working in Objective C

IOS MPMoviePlayerViewController Loading endlessly

I'm working on a IOS App (With Storyoard). I have a ViewController:
//movieViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface movieViewController : UIViewController {
MPMoviePlayerViewController *moviePlayer;
}
#property (strong, nonatomic) MPMoviePlayerViewController *moviePlayer;
-(void) playMovie;
#end
//movieViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self playMovie];
}
-(void)playMovie
{
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"movie"
ofType:#"mov"];
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL
fileURLWithPath:videoPath]];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
When I run I see my viewController with a "Loading..." that takes forever.
You should call -prepareToPlay and/or -play. Per MPMoviePlayerController's documentation:
Starting in iOS 5.0, in order to facilitate finer-grained playback control, a new movie player is not automatically prepared to play.
-(IBAction)startVideo:(id)sender
{
NSURL *video = [[NSURL alloc] initWithString:#"http://www.w3schools.com/html/movie.mp4"];
MPMoviePlayerViewController *controller = [[MPMoviePlayerViewController alloc]initWithContentURL:video];
controller.view.frame = CGRectMake(20, 50, 280, 180);
controller.moviePlayer.view.center = self.view.center;
[self.view addSubview:controller.view];
[controller.moviePlayer prepareToPlay];
[controller.moviePlayer play];
}

NSInvalidArgumentException "Unrecognized selector sent to instance" (using MPMoviePlayerController)

Well, I have a TableView in a RootViewController with a DetailViewController for the display of the information of the single record.
In the Detail page i have to play a multimedia file and i'm using the framework MediaPlayer, according to this guide:
http://www.techotopia.com/index.php/Video_Playback_from_within_an_iOS_4_iPhone_Application
it seems all ok, but when i click on the play button i have this error:
-[DetailsViewController playmovie]: unrecognized selector sent to instance 0x9117f60
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DetailsViewController playmovie]: unrecognized selector sent to instance 0x9117f60'
These are my files:
In the AppDelegate I use this navigation controller:
[...]
// Create a table view controller
RootViewController *rootViewController = [[RootViewController alloc]
initWithStyle:UITableViewStylePlain];
rootViewController.managedObjectContext = context;
rootViewController.entityName = #"Porti";
UINavigationController *aNavigationController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
UIBarButtonItem *homeButton;
homeButton = [[[UIBarButtonItem alloc] initWithTitle:#" Inizio " style:UIBarButtonItemStyleBordered target:self action:#selector(home)] autorelease];
UIBarButtonItem *barButton;
barButton = [[[UIBarButtonItem alloc] initWithTitle:#" Mappa dei porti " style:UIBarButtonItemStyleBordered target:self action:#selector(caricamappa)] autorelease];
[toolbar setItems:[NSArray arrayWithObjects: homeButton, barButton, nil]];
[window addSubview:[navigationController view]];
[window addSubview:toolbar];
[window makeKeyAndVisible];
[rootViewController release];
[aNavigationController release];
and in the RootViewController I use this instruction to pass to the DetailViewController:
//Push the new table view on the stack
[self.navigationController pushViewController:detailsView animated:YES];
[detailsView release];
DetailsViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import "MLUtils.h"
#import <MediaPlayer/MediaPlayer.h>
#interface DetailsViewController : UIViewController {
IBOutlet UILabel *titleLabel;
IBOutlet UILabel *descriptionLabel;
IBOutlet UIScrollView *descriptionScrollView;
NSString *cityName;
NSString *nomefile;
NSString *extfile;
NSString *description;
}
#property (nonatomic, retain) UILabel *titleLabel;
#property (nonatomic, retain) UILabel *descriptionLabel;
#property (nonatomic, retain) UIScrollView *descriptionScrollView;
#property (nonatomic, retain) NSString *cityName;
#property (nonatomic, retain) NSString *description;
#property (nonatomic, retain) NSString *nomefile;
#property (nonatomic, retain) NSString *extfile;
- (IBAction)playmovie:(id)sender;
#end
and this is the DetailsViewController.m
#import "DetailsViewController.h"
#implementation DetailsViewController
#synthesize titleLabel, descriptionLabel, descriptionScrollView;
#synthesize cityName,description,nomefile, extfile;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.titleLabel setText:self.title];
[self.descriptionLabel setText:self.description];
float textHeight = [MLUtils calculateHeightOfTextFromWidth:self.description : descriptionLabel.font :descriptionLabel.frame.size.width :UILineBreakModeWordWrap];
CGRect frame = descriptionLabel.frame;
frame.size.height = textHeight;
descriptionLabel.frame = frame;
CGSize contentSize = descriptionScrollView.contentSize;
contentSize.height = textHeight;
descriptionScrollView.contentSize = contentSize;
}
-(void)playmovie:(id)sender
{
NSString *appNomeFile = self.nomefile;
NSString *appExtFile = self.extfile;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:appNomeFile ofType:appExtFile]];
MPMoviePlayerController *moviePlayer =
[[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
if ([moviePlayer
respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
}
[moviePlayer release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}
- (void)dealloc {
[titleLabel release];
[descriptionLabel release];[descriptionScrollView release];
[cityName release];
[description release];
[nomefile release];
[extfile release];
[super dealloc];
}
#end
My question is: where is my error ? I imagine it is in the call of the playmovie method, but I can't figure out a solution!
P.S.
I've accidentally erased a comment, I'm so sorry! =(
You seem to call playmovie on a class and not on an object, or you forget to give the param. If you show us where you call it, that could help.
Anyway, the problem is that you probably do :
[DetailsViewController playmovie];
or
[oneDetailsViewController playmovie];
instead of :
[oneDetailsViewController playmovie:nil];
Here oneDetailsViewController is a DetailsViewController* object.
EDIT
Delete your XIB link, save, and make your link again to the IBAction by dragging (right-click) from the button to the file's owner.

iPhone development mpmovieplayer crashing

I am working on an app that will let me play different videos on the iPad remotely with an iPhone. I have been following along with apples example for a video player but I've been having some troubles. The videos play just fine and I can get it to play from a variety of videos but switching between them a few times it will crash and i get this in the debugger:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'
*** First throw call stack:
(0x380da8bf 0x37c261e5 0x30acbcb5 0x30abc1f7 0x30ac3bf3 0x30c93d55 0x30c95f7b 0x380ad2dd 0x380304dd 0x380303a5 0x37e07fcd 0x31bb0743 0x25e5 0x257c)
This is the code I am using to create the player:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentOfURL:movieURL];
if (player) {
[self setMoviePlayerController:player];
[self installMovieNotificationObservers];
[player setContentURL:movieURL];
[player setMovieSourceType:sourceType];
[self applyUserSettingsToMoviePlayer];
[self.view addSubview:self.backgroundView];
[player.view setFrame:self.view.bounds];
[player.view setBackgroundColor = [UIColor blackColor];
[self.view addSubview:player.view];
}
And when the current movie is stopped I use:
[[self moviePlayerController] stop];
MPMoviePlayerController *player = [self moviePlayerController];
[player.view removeFromSuperview];
[self removeMovieNotificationHandlers];
[self setMoviePlayerController:nil];
Edit:
So Ive now discovered it happens every time i try and switch a video for the 11th time. weird! I'm practically pulling my hair out.
What fixed this problem for me was stopping the MPMoviePlayerController before doing the setContentURL.
MPMoviePlayerController *streamPlayer;
[streamPlayer stop];
[streamPlayer setContentURL:[NSURL URLWithString:selectedStation]];
In the implementation you have above, ARC doesn't know that the MPMoviePlayerController is finished and needs to be released.
Define MPMoviePlayerController in your .h file and make it accessible via a #property (and #synthesize).
#property (strong, nonatomic) MPMoviePlayerController * moviePlayerController;
Then take the result of your alloc & init and assign it to that. I.E.
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentOfURL:movieURL];
you should just keep the moviePlayerController and if you want to play another video, just use
[self.moviePlayerController setContentURL:movieURL];
then in your notification callback:
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
self.moviePlayer = nil;
[self initanothermovieplayerandplay];
}
and please do not remove the notification handler from notification center, only do this in dealloc method of your VC.
now let's add some fade when the movie play is done:
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[UIView animateWithDuration:1
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
// one second to fade out the view
self.moviePlayer.view.alpha = 0.0;
}
completion:^(BOOL finished){
self.moviePlayer = nil;
[self initanothermovieplayerandplay];
}
}
I had exactly the same problem.
Nothing was wrong with my and i guess with your code :)
Just a broken video file was mine problem.
Changing *.mov type to m4a for example fixed it. Maybe one or more of the files you play are corrupted?
Try to find out which files lead to crash and than if u can try to quickly forward backward the play position of one of them while playing - this should lead to crash in few tries.
This is how i found the bad files. By the way all my bad files were movies .mov made with Snapz Pro X :)
Not sure if it is the case here, but we had a lot of problems, because the MPMoviePlayer is a singleton somewhere under the hood.
What we did is, that we implemented our own MoviePlayer wrapper which can be used from UIView (actually we have exactly one subclass of UIView MoviePlayerView to show movies) and assures that only one instance of MPMoviePlayerController exists. The code goes like this (it contains some special stuff, we need to show previews/thumbs the way we want etc. you should clean up as well as some release-statements):
// MoviePlayer.h
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import "Logger.h"
#class MoviePlayerView;
#interface MoviePlayer : NSObject
{
#private
MPMoviePlayerController *controller;
MoviePlayerView *currentView;
}
#property (nonatomic, readonly) MPMoviePlayerController *controller;
+(MoviePlayer *) instance;
-(void) playMovie:(NSURL*)movieURL onView:(MoviePlayerView *)view;
-(void) stopMovie;
#end
// MoviePlayer.m
#import "MoviePlayer.h"
#import "MoviePlayerView.h"
#implementation MoviePlayer
#synthesize controller;
static MoviePlayer *player = nil;
#pragma mark Singleton management
+(MoviePlayer *) instance
{
#synchronized([MoviePlayer class])
{
if (player == nil)
{
player = [[super allocWithZone:NULL] init];
player->controller = [[MPMoviePlayerController alloc] init];
player->controller.shouldAutoplay = NO;
player->controller.scalingMode = MPMovieScalingModeAspectFit;
player->currentView = nil;
}
return player;
}
}
+(id) allocWithZone:(NSZone *)zone
{
return [[self instance] retain];
}
-(id) copyWithZone:(NSZone *)zone
{
return self;
}
-(id) retain
{
return self;
}
-(NSUInteger) retainCount
{
return NSUIntegerMax;
}
-(oneway void) release
{
// singleton will never be released
}
-(id) autorelease
{
return self;
}
#pragma mark MoviePlayer implementations
-(void) stopMovie
{
#synchronized(self)
{
if (controller.view.superview)
{
[controller.view removeFromSuperview];
}
if (controller.playbackState != MPMoviePlaybackStateStopped)
{
[controller pause];
[controller stop];
}
if (currentView)
{
NSNotificationCenter *ntfc = [NSNotificationCenter defaultCenter];
[ntfc removeObserver:currentView name:MPMoviePlayerLoadStateDidChangeNotification object:controller];
[ntfc removeObserver:currentView name:MPMoviePlayerPlaybackStateDidChangeNotification object:controller];
currentView = nil;
}
}
}
-(void) playMovie:(NSURL*)movieURL onView:(MoviePlayerView *)view
{
#synchronized(self)
{
[self stopMovie];
currentView = view;
NSNotificationCenter *ntfc = [NSNotificationCenter defaultCenter];
[ntfc addObserver:currentView
selector:#selector(loadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:controller];
[ntfc addObserver:currentView
selector:#selector(playbackStateDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:controller];
[controller setContentURL:movieURL];
controller.view.frame = view.bounds;
[view addSubview: controller.view];
[controller play];
}
}
#end
// MoviePlayerView.h
#import <UIKit/UIKit.h>
#import "MoviePlayer.h"
#interface MoviePlayerView : MediaView
{
NSURL *movieURL;
NSURL *thumbnailURL;
UIImageView *previewImage;
UIView *iconView;
BOOL hasPreviewImage;
}
-(id) initWithFrame:(CGRect)frame thumbnailURL:(NSURL *)thumbnail movieURL:(NSURL *)movie;
-(void) loadStateDidChange:(NSNotification *)ntf;
-(void) playbackStateDidChange:(NSNotification *)ntf;
#end
// MoviePlayerView.m
#import "MoviePlayerView.h"
#interface MoviePlayerView()
-(void) initView;
-(void) initController;
-(void) playMovie;
-(void) setActivityIcon;
-(void) setMovieIcon:(float)alpha;
-(void) clearIcon;
-(CGPoint) centerPoint;
#end
#implementation MoviePlayerView
-(id) initWithFrame:(CGRect)frame thumbnailURL:(NSURL *)thumbnail movieURL:(NSURL *)movie
{
self = [super initWithFrame:frame];
if (self)
{
movieURL = [movie retain];
thumbnailURL = [thumbnail retain];
[self initView];
[self initController];
hasPreviewImage = NO;
loadingFinished = YES;
}
return self;
}
-(void) dealloc
{
[iconView release];
[previewImage release];
[movieURL release];
[super dealloc];
}
-(void)initView
{
self.backgroundColor = [UIColor blackColor];
// add preview image view and icon view
previewImage = [[UIImageView alloc] initWithFrame:self.bounds];
[previewImage setContentMode:UIViewContentModeScaleAspectFit];
UIImage *img = nil;
if (thumbnailURL)
{
img = [ImageUtils loadImageFromURL:thumbnailURL];
if (img)
{
previewImage.image = img;
hasPreviewImage = YES;
}
}
[self addSubview:previewImage];
[self setMovieIcon:(hasPreviewImage ? 0.8f : 0.3f)];
}
-(void)initController
{
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(playMovie)];
[self addGestureRecognizer:rec];
[rec release];
}
-(void)playMovie
{
[[MoviePlayer instance] playMovie:movieURL onView:self];
[self setActivityIcon];
}
-(void) loadStateDidChange:(NSNotification *)ntf
{
MPMoviePlayerController *controller = [ntf object];
switch (controller.loadState)
{
case MPMovieLoadStatePlayable:
{
[self clearIcon];
[controller setFullscreen:YES animated:YES];
break;
}
case MPMovieLoadStateStalled:
{
[self setActivityIcon];
break;
}
default:
{
break; // nothing to be done
}
}
}
-(void) playbackStateDidChange:(NSNotification *)ntf
{
MPMoviePlayerController *controller = [ntf object];
switch (controller.playbackState)
{
case MPMoviePlaybackStatePlaying:
{
[self clearIcon];
break;
}
case MPMoviePlaybackStateStopped:
{
[self setMovieIcon:(hasPreviewImage ? 0.8f : 0.3f)];
break;
}
case MPMoviePlaybackStatePaused:
{
[self setMovieIcon:0.8f];
break;
}
case MPMoviePlaybackStateInterrupted:
{
[self setActivityIcon];
break;
}
default:
{
break; // nothing to be done
}
}
}
-(void) setActivityIcon
{
[self clearIcon];
iconView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
iconView.center = [self centerPoint];
[self addSubview:iconView];
[iconView performSelector:#selector(startAnimating)];
}
-(void) setMovieIcon:(float)alpha
{
[self clearIcon];
iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon_movie.png"]];
iconView.center = [self centerPoint];
iconView.alpha = alpha;
[self addSubview:iconView];
}
-(void) clearIcon
{
if (iconView)
{
SEL stop = #selector(stopAnimating);
if ([iconView respondsToSelector:stop])
{
[iconView performSelector:stop];
}
[iconView removeFromSuperview];
[iconView release];
iconView = nil;
}
}
-(CGPoint) centerPoint
{
return CGPointMake(roundf(self.bounds.size.width / 2.0f), roundf(self.bounds.size.height / 2.0f));
}
-(void)resize
{
for (UIView *view in [self subviews])
{
if (view == iconView)
{
iconView.center = [self centerPoint];
continue;
}
view.frame = self.bounds;
}
[self addCaptionLabel];
}
-(void) layoutSubviews
{
[super layoutSubviews];
[self resize];
}
#end
...
player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:...
...
but I didn't gave internet connection to phone (wi-fi) :)
I had the same problem. My solution is using prepareToPlay:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentOfURL:movieURL];
if (player) {
[player prepareToPlay];
//...
}
This error seems to be thrown for lots of different reasons, but the reason I found was that the MPMoviePlayerController class freaks out if you call methods in a certain order. From an IRC Channel:
"apparently if you call prepareToPlay WHILE setting source type and
NOT setting the view yet causes this crash"
So I fixed this by just making sure that I called prepareToPlay: LAST (or second to last, with the last being play:).
It is also weird because my original code worked in iOS 5.1, but this problem suddenly manifested when I started using the iOS 6.0 sdk. It is possibly a bug in the MPMoviePlayerController code, so I'm going to be filing a radar report on it, as calling prepareToPlay: before setting the view / setting the sourceFileType should not throw an exception (or at least an exception that seemingly has nothing to do with the actual error)

Resources