I have a button that unhides a UIWebView and then streams a youtube video. On the iphone it works fine and pressing the play button goes straight to fullscreen and then when its finished hitting the done button hides the UIWebView and return control to app.
However when I try the same on iPad, pressing play does not automatically go to fullscreen (you can hit the fullscreen control but I dont want users to have that option) - is there a way to do this please?
The code I have is:
- (IBAction)topButton:(id)sender {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(youTubeFinished:) name:#"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
videoWebView.hidden=NO;
NSString *embedcode = [self.detailItem objectForKey:#"youtubeEmbedCodeBeginner"];
[[self videoWebView]loadHTMLString:embedcode baseURL:nil];
}
and:
-(void)youTubeFinished:(NSNotification *)notification{
// your code here
videoWebView.hidden=YES;
}
Any help would be appreciated
Try This
selector:#selector(someSelector) name:#"UIMoviePlayerControllerDidEnterFullcreenNotification" object:nil]
// For iOS 4.3 and above:
UIMoviePlayerControllerDidEnterFullscreenNotification
UIMoviePlayerControllerDidExitFullscreenNotification
// For iOS 4.2 and below:
UIMoviePlayerControllerDidEnterFullcreenNotification
UIMoviePlayerControllerDidExitFullcreenNotification
Thanks
Related
I have added few videos inSame cell using tableview. Each cell is having its own webview. Video is played in webview using the following code
NSString *embedHTML = #"<html><head>\<style type=\"text/css\">\body {\background-color:transparent;\color: white;\}\</style>\<script>\function load({document.getElementById(\"yt\").play();}\</script>\</head><body onload=\"load()\"style=\"margin:0\">\<video id=\"yt\" src=\"%#\" \width=\"%0.0f\" height=\"%0.0f\" autoplay controls></video>\</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, videoView.frame.size.width, videoView.frame.size.height,thumbnailImageLink];
[videoView loadHTMLString:html baseURL:nil];
It works absolutely fine. It displays the thumbnail video with play button on it. Tapping on Play button opens up the video player in Full Screen mode with all the controls.
Now the main issue is
When I press the done button the full screen mode goes away Well, thats as expected
But the downloading of the video Still continues. Not at all Good
So Say,
I opened up Video1 then I press on Done button, then I opened video2 But because video1 is still downloading, video2 is getting less internet speed.
So the Question is, Is there anyway out to forcefully stop Downloading when the Done button on full screen is pressed?
Note: There is a list of videos i.e. n videos = n webviews So it is not possible to detect Which webview is playing the video. If I am able to access the webview then I would have reloaded it So that the downloading can get stopped and it gets fresh content which does not start until I tap the thumbnail.
To be more Specific I can access when the video is played and when Done button is pressed using
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoPlayStarted:) name:#"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoPlayFinished:) name:#"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
And can be accessed through
BOOL isVideoInFullScreenMode;
-(void)videoPlayStarted:(NSNotification *)notification{
//Your stuff here
isVideoInFullScreenMode = YES;
}
-(void)videoPlayFinished:(NSNotification *)notification{
//Your stuffs here
isVideoInFullScreenMode = NO;
}
You can't control what's happening in the webview.
I think it is a design issue. Webviews are not magic cure to all. You need to write the code yourself if you want to control it.
I also hope you've bugged this with apple (http://bugreporter.apple.com).
I am new to iPhone. Please help me in solving this issue:
I have embedded YouTube webview in Iphone. My problem is when clicking the done button of YouTube I want to call a class and when the video finishes playing an different class should be called. Can anybody help me on identifying what are the methods called on click of done button and finish of YouTube player in iPhone?
Thanks
If the video is played in UIWebView then you can access when the video is played and when Done button is pressed using the following code along with the code you have used to load url in webview
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoPlayStarted:) name:#"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
//For Done Button
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoPlayFinished:) name:#"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
And it can be accessed through
BOOL isVideoInFullScreenMode;//Temperory added here.. must be in .h file or at teh top if required
-(void)videoPlayStarted:(NSNotification *)notification{
//Your stuff here
isVideoInFullScreenMode = YES;
}
-(void)videoPlayFinished:(NSNotification *)notification{
//Your stuffs here
isVideoInFullScreenMode = NO;
}
You are in a UIWebView, so the done button isn't related to your application: pressing the button will never call your application.
The only method you have are from UIWebViewDelegate and they are:
webView:shouldStartLoadWithRequest:navigationType:
webViewDidStartLoad:
webViewDidFinishLoad:
webView:didFailLoadWithError:
If you have to know when player finish, I suggest you to try this class: LBYouTubeView, it will play youtube video on your player component, so you can use Player delegate's method to to do what you asked for.
I have an app that streams video from the net and plays it using an MPMoviePlayerController object for playback on the device or via AirPlay.
The app supports background operation and has the 'audio' option listed within the required UIBackgroundModes key in its plist file.
When playing over AirPlay, the app can successfully be pushed to the background and the video continues to play properly. So far, so good.
According to the Apple documentation:
Including the audio key tells the system frameworks that they should
continue playing and make the necessary callbacks to the app at
appropriate intervals. If the app does not include this key, any audio
being played by the app stops when the app moves to the background.
However, these callbacks are not being made.
The app uses two types of callback: those associated with notifications MPMoviePlayerController and AVPlayer send during playback together with timer based callbacks that monitor the playback position and performance stats for monitoring purposes.
Looking at Apple's notes, I would certainly expect to receive the first type of callback so that the app can respond to MPMoviePlayerPlaybackStateDidChangeNotification, MPMoviePlayerPlaybackDidFinishNotification and MPMoviePlayerLoadStateDidChangeNotification, but this doesn't happen.
Does anyone know if it is possible to receive these during background AirPlay playback and, if so, how was this achieved?
**Please note: the app works correctly when running in the foreground and receives the notifications fine. It is only when pushed to the background and playing over AirPlay that the notifications are not received.
Likewise, the video plays over AirPlay in the background properly. It is only the notifications which are not received**
I had this issue and have fixed it though it was a few months back. I could send you my entire class for movie playback if this doesn't work. Note it is using the navigation controller model.
NOTE: This is tested on iPad 2 not on the iPhone.
I show my VC like this:
- (IBAction)playMovie:(id)sender {
MovieVC* movController = [[MovieVC alloc] initWithID:2];
movController.view.backgroundColor = [UIColor blackColor];
AppDelegate *appDel = [[UIApplication sharedApplication] delegate];
[appDel.navigationController pushViewController:movController animated:NO];
[movController release];
}
Then in my MovieVC view controller class i set up the video playback like this:
- (void)initMoviePlayer {
mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getMovieURL]];
mMoviePlayer.allowsAirPlay = YES;
mMoviePlayer.view.frame = [self.view bounds];
mMoviePlayer.view.backgroundColor = [UIColor clearColor];
mMoviePlayer.shouldAutoplay = YES;
mMoviePlayer.fullscreen = YES;
mMoviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:mMoviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
This fixed it for me, if it doesn't fix comment and ill edit with the entire class file.
In one of my projects I did what you did, and it worked for me.
Two differences in my project :
I am not streaming via airPlay (device playback only),
I am just playing audio files.
My step-by-step :
Add the audio option to UIBackgroundModes in the plist file,
Register to the NotificationCenter for MPMoviePlayerPlaybackDidFinishNotification and MPMoviePlayerPlaybackStateDidChangeNotification with the following code :
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(playbackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(playbackEnded:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
It works like a charm.
For completeness, I ought to add that at present I believe there is no solution to this problem.
I've discussed it directly with Apple via tech support and there was no practical work-around available.
This functionality was needed to allow the app to record stats about the playback of the stream at regular intervals. While this is fine when the video is played on the device's screen and over AirPlay while the app is in the foreground, it isn't possible to do with the app in the background.
The solution I've gone with instead is to disable the idle timer during all types of playback and to re-enable afterwards using:
[UIApplication sharedApplication].idleTimerDisabled = YES;
and
[UIApplication sharedApplication].idleTimerDisabled = NO;
While this isn't a solution to the original question, it's a workaround for avoiding the issue in the first place.
I have a fairly standard navigation-based iPhone app I'm developing, where on some screens, there is a button that will play a movie using an MPMoviePlayerController in fullscreen mode. There is a navigation bar at the top of the view. I'm seeing some odd behaviour with the view sizing after the movie plays. Was wondering if anybody else has seen this, and whether there is a fix. Here is what I'm seeing:
I hold the phone in one orientation (portrait or landscape)
I start the movie (it plays in fullscreen mode)
I tap the screen to hide the movie playback controls
I rotate the phone to another orientation (portrait to landscape, or vice-versa)
I tap the screen to bring back playback controls, and then tap the 'Done' button
At this point, the original view comes back on screen, in the new orientation. However, the view is not sized properly to accomodate the status bar at the top of the screen. The status bar obscures part of the navigation bar. Rotating the phone at this point corrects the problem.
If I follow the same sequence, but without hiding the playback controls, everything works just fine, and the original view is sized properly when I dismiss the movie.
Anybody have any clue what might be causing this?
Also, this seems to be pretty easy to reproduce. I'm using Xcode4.2 and the 5.0 SDK. If you create a new iPhone-only project using the "Master-Detail Application" template, and make these changes, you can see the problem happen:
First, add the following code to MasterViewController.m.
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
}
- (void) playStory
{
NSURL *movieURL = [NSURL URLWithString:#"http://double-apps.com/thisisatest.m4v"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
Then, replace the contents of the didSelectRowAtIndexPath method with [self playStory]:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self playStory];
}
That's it, run the app, hit the 'Detail' entry in the main screen, then follow the sequence listed above.
I have one application in which list of videos that are coming through xml parsing are displayed in tableview. When I click particular video it should be played in landscape mode.
For that I followed tutorial:
http://iosdevelopertips.com/video/how-to-play-movies-from-application-bundle-or-from-a-remote-server-url.html
It works perfectly but it uses separate modalcontroller so when I press done button on the video or completed video is completed it returns to the that modal view and on which
back button appears and when I press that back button after that I can go back to list of videos screen.but I want to go directly back to the screen which displays list of videos.
for that if i write code using pushviewcontroller than application crashes.How can i solve this problem.any tutorial or sample code?
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:YES];
DonationVideoViewController *dvvc=[[DonationVideoViewController alloc] initWithNibName:"DonationVideoViewController" bundle:nil];
[self.navigationController pushViewController:dvvc animated:YES];
}
UPDATE:I have solved the issue by using the code for calling custommovieplayercontroller in the same class where I have displayed the list of videos. So now video ends it remains to the page I want and I need to go to other page using pushviewcontroller
You should probably push dvvc in viewDidDisappear. But like Till says, we need to see the error message you are getting.