I have a navigation controller that pushes another view which plays video files. Everything is working fine and on the simulator there is no problem at all. The problem is when I test the application on the iPhone, the movies play well but if I press the home button on the iPhone then I launch the app again I receive the following warning in Xcode Debugging area:
2011-11-21 20:23:05.216 KMW[324:707] MP _playbackInterruptionDidEndNotification :: NSConcreteNotification 0x164e90 {name = AVController_PlaybackInterruptionDidEndNotification;
object = ; userInfo = {
"AVController_InterruptionStatusNotificationParameter" = "non-resumable.SoloAmbientSound";
"AVController_InterruptorNameNotificationParameter" = "AudioSession-324"; }}, _state = 0
Although this warning is appearing I can continue using the App and watch movies without any problem but I am afraid that in a certain situation this warning causes a problem. Here is how I am setting up the view that plays the movies:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString *path = [[NSBundle mainBundle] pathForResource:#"movieFileName" ofType:#"m4v"];
movieURL = [NSURL fileURLWithPath:path];
self.moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:movieURL] autorelease];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 10, 320, 181);
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer play];
}
- (void) viewWillDisappear:(BOOL)animated
{
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
}
- (void)dealloc
{
[movieURL release];
[moviePlayer release];
[super dealloc];
}
Any ideas about this strange warning?
Related
i being trying to play video from lib but i cant figure out what im doing wrong i have tried many things and its not showing any kind of error my code is below
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
NSURL *url = [[NSURL alloc]initWithString:mediaType];
NSLog(#"%#",url);
[self dismissModalViewControllerAnimated:NO];
/* Create a new movie player object. */
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
if (player)
{
/* Specify the URL that points to the movie file. */
[player setContentURL:url];
/* Add a background view as a subview to hide our other view controls
underneath during movie playback. */
CGRect viewInsetRect = CGRectInset ([self.view bounds],
kMovieViewOffsetX,
kMovieViewOffsetY );
/* Inset the movie frame in the parent view frame. */
[[player view] setFrame:viewInsetRect];
[player view].backgroundColor = [UIColor lightGrayColor];
/* To present a movie in your application, incorporate the view contained
in a movie player’s view property into your application’s view hierarchy.
Be sure to size the frame correctly. */
[self.view addSubview: [player view]];
[player setCurrentPlaybackRate:0.5];
[player play];
}
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: player];
}
// When the movie is done, release the controller.
-(void) myMovieFinishedCallback: (NSNotification*) aNotification
{
MPMoviePlayerController *playerr = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:playerr];
[playerr stop];
[self.view removeFromSuperview];
[playerr autorelease];
}
what happen is first presetModelViewController is getting displayed then i can shows videos which are in my lib i can select one of them but when i choose video then it shows my grey color view and nothing else and my video is not getting played.
If you could find any errors in my code and can help me with this then it will be great help.
Please go through the below link will help you with your existing issue and also implementation.
http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios
NSURL *videoURL = [NSURL URLWithString:videoURLString];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:videoURL];
[moviePlayer prepareToPlay];
[moviePlayer play];
[self.view addSubview:moviePlayer.view];
This is what solved my problem thanks to spider1983
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
[self dismissModalViewControllerAnimated: YES];
}
// For responding to the user accepting a newly-captured picture or movie
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
// Handle a movie capture
if (CFStringCompare ((__bridge_retained CFStringRef)mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
NSLog(#"%#",[info objectForKey:
UIImagePickerControllerMediaURL]);
/* Create a new movie player object. */
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[info objectForKey:UIImagePickerControllerMediaURL]];
if (player)
{
/* Specify the URL that points to the movie file. */
[player setContentURL:[info objectForKey:
UIImagePickerControllerMediaURL]];
CGRect viewInsetRect = CGRectInset ([self.view bounds],
kMovieViewOffsetX,
kMovieViewOffsetY );
/* Inset the movie frame in the parent view frame. */
[[player view] setFrame:viewInsetRect];
[player view].backgroundColor = [UIColor lightGrayColor];
/* To present a movie in your application, incorporate the view contained
in a movie player’s view property into your application’s view hierarchy.
Be sure to size the frame correctly. */
[self.view addSubview: [player view]];
[player prepareToPlay];
[player setCurrentPlaybackRate:0.5];
[player play];
}
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: player];
}
}
In my application I play video on one of the view which contains following code
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"bgVideo" ofType:#"mov"]];
player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player setControlStyle:MPMovieControlStyleNone];
player.view.frame = CGRectMake(35, 190, 245, 156);
[self.view addSubview:player.view];
[player play];
[player.view setBackgroundColor:[UIColor clearColor]];
I wrote this code on viewWillAppear method
but when I come to this view initially my MPMoviePlayerController shows black screen before starting the video for fraction of second.
I don't want black screen for second.
what shoud I do for it?
thanx in advance.
maybe player a strong property and synthesize it. It worked.
should work in your case as well.
My experience, will not problem any more. Please reply if you can not work.
Edit
I had a little mistaken.
Black screen is shown before the start, because the video is not loaded.
you can not predict the time taken to loading the video. Even if you allocate player to viewWillApper method. black screen will appear.
This black screen can not control it. YouTube app, or a default video App also same.
In During a Black screen, ActivityIndicator or "Loading" message should show to user. It is reasonable.
Finally In general, the larger the video size and quality, takes longer to load.
if you want exactly loading time, you should be notified.
refer a sample code.
- (void)viewWillAppear:(BOOL)animated
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"]];
player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player setControlStyle:MPMovieControlStyleNone];
player.view.frame = CGRectMake(35, 190, 245, 156);
[self.view addSubview:player.view];
[player.view setBackgroundColor:[UIColor clearColor]];
player.shouldAutoplay = NO;
[player prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loadMoviePlayerStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:self.player];
[super viewWillAppear:animated];
}
- (void)loadMoviePlayerStateChanged:(NSNotification *)noti
{
int loadState = self.player.loadState;
if(loadState & MPMovieLoadStateUnknown)
{
NSLog(#"MPMovieLoadStateUnknown");
return;
}
else if(loadState & MPMovieLoadStatePlayable)
{
NSLog(#"MPMovieLoadStatePlayable");
[player play];
}
else if(loadState & MPMovieLoadStatePlaythroughOK)
{
NSLog(#"MPMovieLoadStatePlaythroughOK");
} else if(loadState & MPMovieLoadStateStalled)
{
NSLog(#"MPMovieLoadStateStalled");
}
}
I have been able to successfully play a video on start of the app by calling a function that plays the video in the viewDidLoad method. But on doing so, i am unable to see the "Done" button. The "Done" button only seems to appear if i call that same function with the help of a button. I am posting the code below. How will I make this done appear by calling the same function in the viewDidLoad method? What have i been doing wrong? Thank you!
in my viewcontroller.m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self playMedia];
}
- (void) playMedia {
//hide status bar first
[[UIApplication sharedApplication] setStatusBarHidden:YES];
//resets again in playMediaFinished
movieFile = [[NSBundle mainBundle] pathForResource:#"sec" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath: movieFile];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMediaFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer setFullscreen:NO animated:YES];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
CGAffineTransform transform = self.view.transform;
[moviePlayer.view setFrame:CGRectMake(0, 0, 480, 320)];
moviePlayer.shouldAutoplay = YES;
NSLog(#"Should be playing Movie");
}
- (void) playMediaFinished: (NSNotification*) theNotification {
moviePlayer = [theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if ([moviePlayer
respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
[playerView removeFromSuperview];
//reset status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
NSLog(#"Should be DONE playing Movie");
}
Try doing this using viewDidAppear instead of viewDidLoad. I'm not surprised starting a video in viewDidLoad is causing unexpected behavior considering you're presenting the video before the main view appears on screen. Keep in mind, the view is done loading before it is actually visible.
I am trying to implement video into my app and I am having a hard time. I have tried this tutorial and it was super helpful.
But now when I try to run my app and click on the button, the video frame comes up and just stays black. My video is in the correct format, I also made sure that I am pulling in the MediaPlayer Framework. Has anyone ran into this issue before or knows why this would be happening?
This is what I have:
-(IBAction)playMovie:(id)sender
{
UIButton *playButton = (UIButton *) sender;
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"big-buck-bunny-clip" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view setFrame:CGRectMake(38, 100, 250, 163)];
[self.view addSubview:moviePlayerController.view];
//moviePlayerController.fullscreen = YES;
moviePlayerController.initialPlaybackTime = 5;
[moviePlayerController play];
}
Have you added MPMoviePlayerController's view onto your view?
MPMoviePlayerController *movieController = ...;
movieController.view.frame = self.view.bounds;
[self.view addSubview:movieController.view];
Frame could also be black if you've set shouldAutoplay to YES and controlStyle to MPMovieControlStyleNone
--Edit
This is how I init my player and it works, maybe that will be of some help. It's playing HTTP Live Streaming video, but it'll play anything you put into it. You should try the url in this sample. If it'll work then there's definitely some problem with your contentUrl.
url = [NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayerController.view.frame = self.view.bounds;
[self.view insertSubview:moviePlayerController.view atIndex:0];
moviePlayerController.controlStyle = MPMovieControlStyleNone;
moviePlayerController.shouldAutoplay = YES;
[moviePlayerController prepareToPlay];
I had the same problem, and also following the same tutorial, but then I found the class moviePlayerViewController that its easier to use and you don't even have to worry about dismiss buttons.
I fixed used the following code: (I think it works better with ios5)
self.moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:finalurl];
moviePlayerViewController.view.frame = self.view.bounds;
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
moviePlayerViewController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayerViewController.moviePlayer.shouldAutoplay = YES;
[moviePlayerViewController.moviePlayer prepareToPlay];
moviePlayerViewController.moviePlayer.fullscreen=YES;
i have a tabbar based application with 2 tabs. One to show images and the other to show videos. If i click on the video tab, different thumbnails are showing up. If i click on one of these images, i would like to push that videoviewController to play a video in another navigation view - if i turn the iphone around, it will play fullscreen. So, actually its the same functionality like the original "photo"- app.
What i´ve got so far is a scrollview with different buttons where i added a backgroundimage. By clicking on one of these buttons, i´ve added the button-functionality:
[myButton addTarget:nil action:#selector(buttonDown: ) forControlEvents:UIControlEventTouchUpInside];
And here´s my "buttonDown"-method:
-(void) buttonDown:(NSString*) sender {
moviePlayer = [[MoviePlayer alloc] init];
[self.view addSubview:moviePlayer.view];
NSLog(#"MoviePlayer: %#", moviePlayer);
[self.navigationController pushViewController:moviePlayer animated:YES];
[moviePlayer release];}
Here´s my "MoviePlayer.m"-file:
- (void)viewDidLoad {
UIView* imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100 )];
imageView.backgroundColor = [UIColor greenColor];
[self.view addSubview:imageView];
[imageView release];
NSString *url = [[NSBundle mainBundle] pathForResource:#"testmovie" ofType:#"mp4"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
//player.scalingMode = MPMovieScalingModeAspectFill;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player autorelease];
}
So the navigation works great - by clicking on a button, the view is pushed (and i get my green test-uiview) and the sound of that video file is been playing - but no video is visible, only that sound.
I´ve no idea - if i test that code in a new viewbased-project-template it works great. So i think there some problems with the views.
Any ideas on that?
Would be great. Thanks for your time.
Give this a go:
[player setFullscreen:YES];
[self presentMoviePlayerViewControllerAnimated:player];
I believe that should resolve your woes! Please vote for answer if it assists!