Movie player iOS5 play symbol over view - objective-c

I have been trying to figure out how to get the "play symbol" on top of my movie view, so that the user just have to press that and the video will start. Anybody know how to implement that?
-(void)viewDidLoad
{
//Video player
NSString *url = [[NSBundle mainBundle] pathForResource:self.navigationItem.title ofType:#"mov"];
_player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath: url]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:_player];
_player.view.frame = CGRectMake( 350, 200, 400, 400);
[self.view addSubview:_player.view];
}
//Plays the movie once the "Play" button is pressed
-(IBAction)playMovie
{
[_player play];
}
//Exits fullscreen when movie is finished
- (void) playerPlaybackDidFinish:(NSNotification*)notification
{
_player.fullscreen = NO;
}

You can either create an IBOutlet for your button or you will have to add the play button yourself via code like this... (in order to access it via code.)
// Add play button
playButton = [[UIButton alloc] initWithFrame:CGRectMake(53, 212, 214, 36)];
[playButton setBackgroundImage:[UIImage imageNamed:#"playButton.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:#selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[[self view] addSubview:playButton];
and once you are done adding the movieplayer to the view.... you need to bring the playbutton to the front like ....
[self.view bringSubviewToFront:playButton];
remember this to be done once you have added the movieplayer to the view...
source ... http://iosdevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html

Related

Objective C : How to Insert text in Movie Player View?

I am making a Apache Cordova Application.
On this Application, I used the plugin : Cordova Streaming Media plugin
Link is : https://github.com/nchutchind/Streaming-Media-Cordova-Plugin
I want to insert a "Hello World!" label on my movie but I don't know how to do that.
I think the script is : StreamingMedia.m
Link is : https://github.com/nchutchind/Streaming-Media-Cordova-Plugin/blob/master/src/ios/StreamingMedia.m
And function is : startPlayer()
Can you tell me where is my error :
-(void)startPlayer:(NSString*)uri {
NSURL *url = [NSURL URLWithString:uri];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Listen for playback finishing
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// Listen for click on the "Done" button
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:nil];
// Listen for orientation change
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
if (imageView != nil) {
[moviePlayer.backgroundView setAutoresizesSubviews:YES];
[moviePlayer.backgroundView addSubview:imageView];
}
moviePlayer.backgroundView.backgroundColor = backgroundColor;
[self.viewController.view addSubview:moviePlayer.view];
// Note: animating does a fade to black, which may not match background color
[moviePlayer setFullscreen:YES animated:NO];
// Here my code :
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 0, 0)];
label.text = #"Hello World!";
CGRect frame = label.frame;
frame.origin.x = 100;
frame.origin.y = 50;
label.frame = frame;
[self.viewController.view addSubview:label];
}
Thank You!
You have two choices:
You can add a view to the app window/root controller view.
Create custom controller and add to his view MPMoviePlayerController like
subView.
Errors:
Your UILabel has 0x0 sizes. Just set up sizes :)
You send _rangeSlider to back of view via sendSubviewToBack. Remove it and it's start working.
And it seems you can just add subview to the MPMoviePlayerController view.

Why does my textField disappear?

I do some practice with iPhone4S about UITextField on iOS 7.1.2.
Interface:
The interface of the first version is simple, which just has a custom UITextField named MyUITextField and a UIButtom object that use to cancel the search operation. Inside the textField, its leftView property is initially set to a UIButton object, which background image is a magnifying glass. When users tap in the textField, that magnifying glass button will be removed and the leftView property will also set to a new UIButton object, which background image is a inverted triangle. When users tap the triangle button, app will create a new view object.
Main code:
//In this version,the textField's original frame property is set to (10,40,250,30)
//and the main operations are like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextField.delegate = self;
self.myTextField.leftViewMode = UITextFieldViewModeUnlessEditing;
self.myTextField.leftView = [self creCustomSearchBar];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyBoardWillAppear:)
name:#"UIKeyboardWillShowNotification"
object:nil];
[self.cancleSearchButton addTarget:self
action:#selector(cancleSearch:)
forControlEvents:UIControlEventTouchUpInside];
}
/*when tapping in the textField,keyboard will appear*/
- (void)keyBoardWillAppear:(NSNotification *)notification
{
CGRect newLeftViewIndicatorRect = CGRectMake(20,5,20,20);
UIButton *newLeftViewIndicator = [[UIButton alloc] initWithFrame:newLeftViewIndicatorRect];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"searchbar_textfield_down_icon#2x" ofType:#"png"];
[newLeftViewIndicator setBackgroundImage:[UIImage imageWithContentsOfFile:imagePath] forState:UIControlStateNormal];
[newLeftViewIndicator addTarget:self action:#selector(createActuralLeftView:) forControlEvents:UIControlEventTouchUpInside];
self.myTextField.leftView = newLeftViewIndicator;
/*in the second verson need to call */
//[self adjustViewFrame];
[self.cancleSearchButton setTitle:#"取消" forState:UIControlStateNormal];
}
- (void)createActuralLeftView:(UIButton *)leftViewButton
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapRegionAboveKeyboard:)];
[self.view addGestureRecognizer:tapGesture];
NewLeftView *leftView = [[NewLeftView alloc] initWithFrame:CGRectMake(10, 80, 140, 100)];
leftView.delegate = self;
[self.view addSubview:leftView];
}
- (void)tapRegionAboveKeyboard:(UITapGestureRecognizer *)tapGesture
{
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[NewLeftView class]]) {
[v removeFromSuperview];
[self.view removeGestureRecognizer:tapGesture];
return;
}
}
}
//in the second version need to call
- (void)adjustViewFrame
{
[[self.myTextField class] animateWithDuration:0.05 animations:^{
[self.myTextField setFrame:CGRectMake(self.myTextField.frame.origin.x, 40, self.myTextField.frame.size.width, self.myTextField.frame.size.height)];
}];
[[self.cancleSearchButton class] animateWithDuration:0.05 animations:^{
[self.cancleSearchButton setFrame:CGRectMake(self.cancleSearchButton.frame.origin.x, 40, self.cancleSearchButton.frame.size.width, self.cancleSearchButton.bounds.size.height)];
}];
}
In the first version things work well.
But in the second version, I set the textField frame property to (10,260,250,30), so I need to call the adjustViewFrame method in the keyBoardWillAppear: to reposition the textField in case of obscuring by the keyboard.
Here comes the problem: the textField's position is correctly moved to the right place, but when I tap the inverted triangle button, the textField disappeared.
I can't figure out what's going wrong here.

disable user interaction in MPMoviePlayerController

I need to disable total user interaction during a play of a litle video, i already tried to set the style of the control to none : MPMovieControlStyleNone, and put a UIView above the MPMoviePlayerController, but when i pinch the video disappears, and the sound still playing, like i dismissed the video, but it still playing on the background, plus the user interaction is disabled.
Heres how i do:
-(IBAction)startGame:(id)sender
{
NSURL * url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Teste" ofType:#"m4v"]];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
mBlankView = [[UIView alloc] initWithFrame:moviePlayer.view.frame];
mBlankView.userInteractionEnabled = NO;
[mBlankView setMultipleTouchEnabled:NO];
[mBlankView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:mBlankView];
}
To disable Movieplayer control completely u need to disable your movieplayers view userinteraction as follows:
moviePlayer.view.userInteractionEnabled = NO;

1024x768 on ipad using MPMoviePlayerController

after a long day researching into why 1024x768 video would not work on an iPad 3, and a lot of forum hunting. I could not find any reasonable solutions to this problem. Seems a lot of people where having the same situation with only a black screen being shown.
Solution was to render out the video required as 1024x 748. Reason being the status bar shown at the top of an iPad is 20px.
I hope this solves a lot of user problems they are having with this situation.
Onto my next question....
I currently have a video embedded into my view via the MPMoviePlayerController. This video is on a constant loop(An animated menu). I then have three invisible buttons layered on top of the video to which methods are called. I only have one working at present, the other two just show alerts.
The problem I am having at present is that, when the button is clicked, I want another video to play. This is working perfectly, BUT i would like the first video to FIRST play to the end the go onto the play the next video, reason being, I need it too be seamless and not be obvious its going from one video to another.
Any feedback on this would be great....
#import "MainViewController.h"
#import "VideoPlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <UIKit/UIKit.h>
#interface MainViewController ()
#property (nonatomic, retain) MPMoviePlayerController *playerViewController;
#property (nonatomic, retain) VideoPlayerViewController *myPlayerViewController;
#end
#implementation MainViewController
#synthesize playerViewController = _playerViewController;
#synthesize myPlayerViewController = _myPlayerViewController;
- (void)dealloc {
self.playerViewController = nil;
self.myPlayerViewController = nil;
[super dealloc];
}
#pragma mark - View lifecycle
-(void)buttonEventOne:(id)sender {
//[[[[UIAlertView alloc] initWithTitle:#"CONTENT ONE" message:#"This will link to content one video" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease] show];
NSURL *urlTwo = [[NSBundle mainBundle] URLForResource:#"BAE_BG_anim_pt2_748" withExtension:#"mov"];
MPMoviePlayerController *playerViewController = [[MPMoviePlayerController alloc] init];
playerViewController.contentURL = urlTwo;
playerViewController.view.frame = CGRectMake(0, 0, 1024, 768);
playerViewController.controlStyle = MPMovieControlStyleNone;
//playerViewController.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview:playerViewController.view];
[playerViewController play];
self.playerViewController = playerViewController;
[playerViewController release];
}
-(void)buttonEventTwo:(id)sender {
[[[[UIAlertView alloc] initWithTitle:#"CONTENT TWO" message:#"This will link to content one video" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease] show];
}
-(void)buttonEventThree:(id)sender {
[[[[UIAlertView alloc] initWithTitle:#"CONTENT THREE" message:#"This will link to content one video" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease] show];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:#"BAE_Main_Loop_748" withExtension:#"mov"];
// video player
MPMoviePlayerController *playerViewController = [[MPMoviePlayerController alloc] init];
playerViewController.contentURL = url;
[playerViewController prepareToPlay];
playerViewController.view.frame = CGRectMake(0, 0, 1024, 768);
playerViewController.controlStyle = MPMovieControlStyleNone;
playerViewController.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview:playerViewController.view];
[playerViewController play];
self.playerViewController = playerViewController;
[playerViewController release];
UIButton * btnOne = [UIButton buttonWithType:UIButtonTypeCustom];
btnOne.frame = CGRectMake(260, 350, 150, 50);
[btnOne setTitle:#"" forState:UIControlStateNormal];
[btnOne addTarget:self action:#selector(buttonEventOne:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnOne];
UIButton * btnTwo = [UIButton buttonWithType:UIButtonTypeCustom];
btnTwo.frame = CGRectMake(620, 350, 150, 50);
[btnTwo setTitle:#"" forState:UIControlStateNormal];
[btnTwo addTarget:self action:#selector(buttonEventTwo:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTwo];
UIButton * btnThree = [UIButton buttonWithType:UIButtonTypeCustom];
btnThree.frame = CGRectMake(450, 250, 150, 50);
[btnThree setTitle:#"" forState:UIControlStateNormal];
[btnThree addTarget:self action:#selector(buttonEventThree:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnThree];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
#end
Have you tried registering for playback notifications, specifically the MPMoviePlayerPlaybackDidFinishNotification, catchy huh? If you register for the notification when the button is hit:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(introMovieFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playerViewController];
Then in that function:
- (void)introMovieFinished:(NSNotification*)note {
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playerViewController];
// Do stuff.
}
You de-register your interest in the notifications and then start playing the next movie. Because you only register an interest when the button is hit you shouldn't receive the function call until the movie finishes.

MPMoviePlayerController controls when opened in a UIModalPresentationFormSheet style modal and the user makes the video fullscreen

In my iPad app, I have a UIButton that is calling an IBAction to call a view controller as a modal to show a video in. I wanted the modal to appear as 720x405, and that part seems to work out okay. Here is the IBAction code the button is executing:
-(IBAction)videoPlayerTest:(id)sender {
VideoModalViewController *vc = [[VideoModalViewController alloc] initWithNibName: #"VideoModalViewController" bundle: nil];
vc.fileName = #"testvideo.m4v";
vc.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:vc animated: YES];
vc.view.superview.frame = CGRectMake(0, 0, 720, 405);
vc.view.superview.center = self.view.center;
[vc release];
}
The modal comes up where I want it, and the controls respond on the MPMoviePlayerController; the jog bar, pause, play, etc. but if the user taps on the fullscreen button, the video does go fullscreen alright, but after that the MPMoviePlayerController won't respond to any subsequent taps on the player controls. If I remove the modalPresentationStyle line it will work, but the modal appears on a fullscreen view instead of the 720x405 modal like I want. I've added Observers to try resizing the frame and recenter it when the user makes the movie controller fullscreen and back to windowed, but it didn't appear to help at all. Here is that code.
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
[self setModalPresentationStyle:UIModalPresentationFullScreen];
self.view.frame = CGRectMake(0, 0, 1024, 768);
self.view.center = self.view.center;
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
[self setModalPresentationStyle:UIModalPresentationFormSheet];
self.view.frame = CGRectMake(0, 0, 720, 405);
self.view.center = self.view.center;
}
- (void)playMovie {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
NSString *videoString = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
NSURL *videoURL = [NSURL fileURLWithPath:videoString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 720, 405);
moviePlayer.view.backgroundColor = [UIColor grayColor];
[moviePlayer prepareToPlay];
[moviePlayer play];
}
This is my first post-- hope I did it right and provided enough information about the problem I'm having.
I've solved my problem. I was unaware of MPMoviePlayerViewController and I created that and used that as my modal instead. It works great.
-(void)playVideo:(NSString *)fileName {
NSString *videoString = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
NSURL *videoURL = [NSURL fileURLWithPath:videoString];
mpViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentModalViewController:mpViewController animated:NO];
[[mpViewController moviePlayer] play];
}
-(IBAction)videoPlayerTest:(id)sender {
[self playVideo:#"testvideo.m4v"];
}
Thought I'd post what I came up with just in case somebody else encounters the same