Playing a video to start the application
I have this code, I can't see the video and I can hear the audio.
Could anyone explain why ?
-(void)awakeFromNib
{
NSURL *url2 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tecnolitevideo_1" ofType:#"mov"]];
moviePlayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:url2];
[moviePlayer presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
}
-(void)moviePlayerPlaybackStateChanged:(NSNotification *)notification {
}
- (void)viewDidLoad
{
NSURL* url = [[NSBundle mainBundle] URLForResource:#"3" withExtension:#"mov"];
m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
[m_player.view setBackgroundColor:[UIColor blackColor]];
[m_player setControlStyle:MPMovieControlStyleNone];
[[m_player view] setFrame:[self.view bounds]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[m_player play];
[self.view addSubview:[m_player view]];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[m_player.view removeFromSuperview];
[m_player stop];
m_player = nil;
}
I am try this and works fine!!!
Related
I use SWRealViewController for my sidebar.
My moviePlayer work fine with out fullscreen, but when i click fullscreen moviePlayer crash and i got an error:
2014-01-01 19:23:00.860 Chingfong[56193:70b] Warning: Attempt to
dismiss from view controller while
a presentation or dismiss is in progress!
Here i my code in movieVC.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface movieVC : UIViewController
{
NSString *link;
}
#property(nonatomic,retain) NSString *link;
#property(nonatomic, strong)UIActivityIndicatorView *activityIndicator;
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
#end
and here is movieVC.m
#import "movieVC.h"
#import "SWRevealViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#interface movieVC ()
#end
#implementation movieVC
#synthesize link;
#synthesize activityIndicator;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *urlAddress = link;
//Create a URL object.
NSURL *url1 = [NSURL URLWithString:urlAddress];
self.moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url1];
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
self.moviePlayer.view.frame = CGRectMake(0,0,320,180);
[self.view addSubview:_moviePlayer.view];
self.moviePlayer.shouldAutoplay = NO;
[self.moviePlayer setFullscreen:YES animated:YES];
}
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(#"exitedFullscreen");
[self.moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
NSLog(#"playbackFinished. Reason: Playback Ended");
break;
case MPMovieFinishReasonPlaybackError:
NSLog(#"playbackFinished. Reason: Playback Error");
break;
case MPMovieFinishReasonUserExited:
NSLog(#"playbackFinished. Reason: User Exited");
break;
default:
break;
}
[self.moviePlayer setFullscreen:NO animated:YES];
}
- (void)showMovie {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *urlAddress = link;
//Create a URL object.
NSURL *url1 = [NSURL URLWithString:urlAddress];
self.moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url1];
self.moviePlayer.view.frame = CGRectMake(0,0,320,180);
[self.view addSubview:_moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
}
-(void)viewWillDisappear:(BOOL)animated {
[self.moviePlayer stop];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here is the error I am getting
Thread 1:EXC_BAD_ACCESS (code=2, address=0xb7ffffc)
On this line
[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish
object:target
userInfo:[[userInfo copy] autorelease]];
In the AsyncImageView.m file.
The error stops the code but if I continue in debugger it freezes Xcode and shuts it down. How can I fix this issue?
In init you need to register, and in dealloc you need to un register!
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:AsyncImageLoadDidFinish object:nil];
OR
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Try the code below, it should be okay:
NSDictionary * userInfo = [NSDictionary dictionaryWithObjectsAndKeys:..., nil];
[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish
object:target
userInfo:userInfo];
or:
NSDictionary * userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:..., nil];
[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish
object:target
userInfo:userInfo];
[userInfo release];
When I click “play movie” the mpmovieplayer presents itself for half a second and I see it’s loading movie and the controls are there, but then it returns to main screen without playing the video.
How do I fix this?
Edit:
I have changed code and now the player stays up but is stuck on loading, it doesn’t play movie.
IS the problem: not preparing the movie for play? Or not stopping movie in background and then restarting it?
#import "ViewController.h"
#implementation ViewController
#synthesize moviePlayer;
-(IBAction)grabVid:(id)sender;
{
[self presentModalViewController:imagePicker animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[imagePicker setDelegate:self];
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
imagePicker.sourceType];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
{
[imagePicker dismissModalViewControllerAnimated:YES];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
//[imagePicker dismissModalViewControllerAnimated:YES];
}
-(IBAction)playMovie:(id)sender
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self.view addSubview:playercontroller.moviePlayer.view];
[playercontroller.moviePlayer setFullscreen:YES animated:YES];
playercontroller.moviePlayer.shouldAutoplay = NO;
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playercontroller.moviePlayer];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
//[playercontroller.moviePlayer release];
}
- (void)dealloc {
//[super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
-(IBAction) playVideo
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayer.initialPlaybackTime = 0;
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[[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];
// playercontroller = nil;
}
in my ipad app in viewDidLoad i set up a video in the first view so to have an intro.
In allocation tool i see that allocated memory grow till 120MB when running the video, ok i dont care, but after the video finish i would like that memory to go to 0 again BUT stick to 120MB, what am i doing wrong ?
- (void)viewDidLoad{
NSString *url = [[NSBundle mainBundle]
pathForResource:#"sfed"
ofType:#"mp4"];
playerViewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
[videoview addSubview:playerViewController.view];
MPMoviePlayerController *player = [playerViewController moviePlayer];
player.view.frame = CGRectMake(1024, 748, 0, 0);
[player setControlStyle:MPMovieControlStyleNone];
[player play];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[player release];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1];
[videoview setAlpha:0];
[UIView commitAnimations];
[videoview release];
}
I have a video in my application, but it's in portrait. I want to display it in landscape mode, but I don't have any idea how to do that.
I used this code to make my video:
- (IBAction)playMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"BAZO" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
//Uncomment om beeld formaat aan te passen
//moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayerController play];
}
- (IBAction)playSecondMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"00 01. Welcome" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
//Uncomment om beeld formaat aan te passen
//moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
-(void) tableView: (UITableView*) tableView
didSelectRowAtIndexPath: (NSIndexPath*) indexPath
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"BAZO" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
//Uncomment om beeld formaat aan te passen
//moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayerController play];
/*-(void)loadVideo:(NSString *)name ofType:(NSString *)type{
NSURL *url=[NSURL fileURLWithPath:[mainBundle pathForResource:#"BAZO" ofType:#"m4V"]]
if (!mp) mp = [[MPMoviePlayerController alloc] initWithContentURL:url];
[mp play];
}*/
}
Maybe somebody can give me a hint or a method so I can put this to landscape?
I used this code:
[MPMoviePlayerController setOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
but it gives me the warning :
MPMoviePlayerController may not respond to -setOrientation:animated:
What's happening?
Had this problem as well,
I was running it in the appDelegate file, which meant I had to force the window to change to landscape so if the original is like this:
-(void)playMovie {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"intro"
ofType:#"m4v"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player setFullscreen:YES animated:YES];
[player setControlStyle:MPMovieControlStyleNone];
[window addSubview:player.view];
[window bringSubviewToFront:player.view];
[player setFullscreen:YES animated:YES];
[player setControlStyle:MPMovieControlStyleNone];
[player play];
}
all that is needed is to add these lines:
[window setBounds:CGRectMake( 0, 0, 480, 320)];
[window setCenter:CGPointMake(160, 240];
[window setTransform:CGAffineTransformMakeRotation(M_PI/ 2)];
before the [player play], and at the movie finish call, add
struct CGRect rect = [[UIScreen mainScreen] bounds];
rect.origin.x = rect.origin.y = 0.0f;
window =[[UIWindow alloc] initWithFrame:rect];
[window makeKeyAndVisible];
to reset the view.
Not sure if it's the right way to do it, but it was the only thing worked for me.
Use MPMoviePlayerViewController instead of MPMoviePlayerController. It will handle orientation, control style, etc. I referred this answer for the same.
MPMoviePlayerViewController *playerView = [[[MPMoviePlayerViewController alloc] initWithContentURL:videoURL] autorelease];
[self presentMoviePlayerViewControllerAnimated:playerView];
and in the AppDelegate.m, declare the following function which will restrict the orientation to portrait in all cases and will only change when a video is playing
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return UIInterfaceOrientationMaskAll;
}
else
{
//NSLog(#"orientation should change");
return UIInterfaceOrientationMaskPortrait;
}
}