iphone MediaPlayerController does not show the media player - iphone

HI,
I am developing an app which contains 3 tab view control. One each for audio, video and images.
When i select the video tab, I have all the items displayed in the table view format so that on selection of any cell, the video is shown.
Example.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]] ;
[moviePlayer play];
}
The problem with this is though I am able to play the video I am unable to see the video and can hear only the audio.
In addition, if I were to close the app while the video is playing, and start it the app, the application crashes.
Can you please let me know how should I go ahead ?

After spending some more time, I have found out that for version above 3.2 one has to use MPMoviePlayerViewController.
The use of the same resolved the issue.
if ([[[UIDevice currentDevice] systemVersion ] doubleValue] >= 3.2) {
MPMoviePlayerViewController *mediaPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlAddress]];
if (mediaPlayer) {
[self presentMoviePlayerViewControllerAnimated:mediaPlayer];
mediaPlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile ;
[mediaPlayer.moviePlayer play];
[mediaPlayer release];
}
}

Check out this technote from Apple:
http://developer.apple.com/library/ios/#qa/qa2010/qa1240.html
To summarize, you need to add the movie player controller's view as a subview of your app's view.

Related

How to play video from URL in Ipad?

In my application i get the links of video from server in UITableview .all these link are store on Sever in Textfile,i get all these link from sever one by one and assign each to cell in UITableview.all these i done succefully but i want when i click on any cell in UITableview its play the video in next view.here is my code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
nextview *dvController = [[nextview alloc] initWithNibName:#"nextview" bundle:nil];
[self presentModalViewController:dvController animated:YES];
strFile = [Listdata objectAtIndex:indexPath.row];
NSLog(#"test=%#",strFile);
[dvController release];
}
and in next view i assign the link which i store in "strFile" to MPMoviePlayerController here is my code.
-(void)viewDidAppear:(BOOL)animated
{
NSLog(#"mytest=%#",strFile);
NSURL *url = [NSURL URLWithString:strFile];
NSLog(#"myurl=%#",url);
myplayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
myplayer.view.frame = CGRectMake(0, 0,900, 700);
[self.view addSubview:myplayer.view];
[myplayer play];
}
In NSlog i see the link but its not play in MPMoviePlayerController.Any one can guide me that what mistake i make.thanx in advance.
#prince : can u tell me what url you are getting in NSLog?? i think u're mistaken in getting the url.
we can get url either by
url = [NSURL fileURLWithPath:strUrl];
else
url = [NSURL URLWithString:strUrl];
try it out once.
You probably shouldn't be setting the frame and adding to the subview, You may be confused with MPMoviePlayerViewController. Try this...
-(void)viewDidAppear:(BOOL)animated {
NSLog(#"mytest=%#",strFile);
NSURL *url = [NSURL URLWithString:strFile];
NSLog(#"myurl=%#",url);
myplayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
myplayer.scalingMode = MPMovieScalingModeAspectFill;
[myplayer play];
}
EDIT disregard my code, you can't directly play a YouTube video in MPMoviePlayerController. Check this Post instead Play YouTube videos with MPMoviePlayerController instead of UIWebView
to youtube video play inside app is to create a UIWebView with the embed tag from Youtube for the movie you want to play as the UIWebView's content. UIWebView will detect that the embedded object is a Youtube link, and the web view's content will be the youtube preview for the video. When your user clicks the preview, the video will be shown in an MPMoviePlayerController. try this link:
http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application
Unfortunately, there's no way to directly play a youtube video with MPMoviePlayerController because youtube does not expose direct links to the video files.

Getting blank white screen when presenting MPMoviePlayerViewController with no network connection

I am playing a media file present on the server. I purposely turned off the wifi connection in order to test the app. What I get is a blank screen.
What I want is to check before playing the video file is for internet connection. IF there is not connection, I dont want to play the video and display a alert message. How can I do so?
Can anyone help me on the same?
if ([[[UIDevice currentDevice] systemVersion ] doubleValue] >= 3.2) {
MPMoviePlayerViewController *mediaPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlAddress]];
if (mediaPlayer) {
[self presentMoviePlayerViewControllerAnimated:mediaPlayer];
mediaPlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile ;
[mediaPlayer.moviePlayer play];
[mediaPlayer release];
}
}
See the Reachability sample application.

Loading Multiple Movies in iPhone MPMoviePlayerController

-(void)initAndPlayMovie:(NSURL *)movieURL
{
// Initialize a movie player object with the specified URL
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp)
{
self.moviePlayer = mp;
[mp release];
[self.moviePlayer play];
}
}
Here, in above code, We can pass just one movie URL. Isn't it possible to pass multiple urls to it?
So, Movie Player will load second url after playing first one.
Is it possible? How can we do that?
Right now, when I try to pass other url, after finishing first one.
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[self initAndPlayMovie:secondURL];
}
The Device First change its orientation while loading and after loading Device again come back to landscape mode.
How to resolve this problem?
You might want to change the orientation by changing the statusBar orientation before you start playing videos and change it back after you are done with all.
[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:YES];
You should be able to call setContentURL just as the first movie is about to close to change to another movie. Check endPlaybackTime and fire off your method to invoke setContentURL one second prior to the movie ending.

MPMoviePlayerController and overlay window

Iam trying to play a video from a remote location and trying to overlay a window over the entire screen which is more or less transparent with few images along the edges . As soon as the movie is preloaded I play the video. This happens good the first time. But if I try to replay the video it does not play the video and neither am i able to click on the Play button from the controls of the player. I guess the overlay window is over the controls. How do i get the controls over the overlay window.
//- (void)moviePlayerContentPreloadDidFinish:(NSNotification *)notification{
NSLog(#"content preloaded");
NSDictionary *userInfo = [notification userInfo];
if ([userInfo valueForKey:#"error"]) {
NSLog(#"*** An error occurred preloading the movie");
return;
}
[self.spinner stopAnimating];
// Add the overlay view to the movie, so we can catch the clicks
OverlayViewController *ovc = [[OverlayViewController alloc] initWithNibName:#"OverlayView" bundle:nil];
self.overlayController = ovc;
[ovc release];
[self.overlayController setNextResponder:self];
MPMoviePlayerController *moviePlayer = [notification object];
[moviePlayer play];
// Locate the movie player window
NSArray *windows = [[UIApplication sharedApplication] windows];
NSLog(#"window count= %d",[windows count]);
if ([windows count] < 2) {
NSLog(#"*** Window for movie player is missing");
return;
}
UIWindow *moviePlayerWindow = [windows objectAtIndex:1];
[moviePlayerWindow addSubview:self.overlayController.view];
}
The code that I use is
Am I doing something wrong. Is it possible to get the controls over the overlay or auto play it.
Two things.
First, according to your code, in the moviePlayBackDidFinish method you must first release the moviePlayer and then instantiate it again with an alloc. You must do that every time you want to play a video.
MPMoviePlayerController *moviePlayer = [notification object];
[moviePlayer release]
...
MPMoviePlayerController *newMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: anURL];
The structure of your code works, but I think you should better have a look at the example provided by apple:
http://developer.apple.com/iphone/library/samplecode/MoviePlayer_iPhone/index.html
Second, about the overlay, you are right, the overlay gets the events but you cannot put it under the movie player controls. What you have to do is to use the events you receive in your overlay to control the player. You could use touchesBegan event to pass the initial touch event and use it with the player (play, pause, stop).

Embedded Video in a UIView with iPhone

I would like to write an app that downloads (or streams) a video (encoded as required) in a view. I dont want to use the MPVideoPlayer from the SDK as it opens the video in full screen. I would like to place another UIView (transparent) over the video so that my users can annotate over the video.
Anyone have any idea or can point me to some code that will play video in a UIView?
If you want to do this you will need to include your own (software) video decoder, which will not have access to the hardware acceleration on the system. Even if you can get it working with acceptable performance it will be a huge battery drain.
If you wish to play a video in portrait mode, I have solution for that.
If you think that -MPMovie Player can run under a view, according to me it's not possible.
MP Movie player will work as Apple has designed.
So, MP Movie player will always / almost run in full screen.
Solution for portrait mode.
#interface MPMoviePlayerController (extend)
-(void)setOrientation:(int)orientation animated:(BOOL)value;
#end
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieUR];
[moviePlayer setOrientation:UIDeviceOrientationPortrait animated:NO];
if (moviePlayer)
{
[self.moviePlayer play];
}
Hope that will help to you.
See, My question is very similar like yours.
playing video in custom size screen - view in iphone
Try this:
UIViewController *v = [[UIViewController alloc] init];
v.view.backgroundColor = [UIColor orangeColor];
NSString *path = [[NSBundle mainBundle] pathForResource:#"demo" ofType:#"mp4"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(#"cannot find %# in bundle or doctuments", path);
}
NSURL *url = [NSURL fileURLWithPath:path];
MoviePlayerViewController *mpvc = [[MoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mpvc.moviePlayer];
mpvc.moviePlayer.fullscreen = NO;
[mpvc.moviePlayer setControlStyle:MPMovieControlStyleNone];
mpvc.moviePlayer.view.frame = CGRectMake(10, 100, 300, 300);
[v.view addSubview:mpvc.moviePlayer.view];
[mpvc.moviePlayer play];
[self presentModalViewController:v animated:YES];
[v release];
Maybe you should check the MediaPlayer's private headers, and simply add the video's view as your view's subview.