i use this code to display a video in my app
NSURL *movieUrl = [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"myvideoname"
ofType:#"mp4"]];
//create a new instance of MPMoviePlayerController
MPMoviePlayerController* myMovie=[[MPMoviePlayerController alloc]
initWithContentURL:movieUrl];
//disable scaling of our movie
myMovie.scalingMode = MPMovieScalingModeNone;
[myMovie.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: myMovie.view];
[[myMovie view] setFrame:[myView bounds]];
//don't show any controls
// myMovie.movieControlMode = MPMovieControlModeHidden;
//you can specify at which time the movie should
//start playing (default is 0.0)
myMovie.initialPlaybackTime = 2.0;
//register a callback method which will be called
//after the movie finished
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:myMovie];
myMovie.scalingMode = MPMovieScalingModeAspectFill;
//start the movie (asynchronous method)
[myMovie play];
// Do any additional setup after loading the view from its nib.
it work fine but i want to add the controls ( play , stop , sound control ...)
How can i do ? thanx
What using controlStyle?
myMovie.constrolStyle = MPMovieControlStyleEmbedded;
MPMovieControlStyle
Constants describing the style of the playback controls.
enum {
MPMovieControlStyleNone,
MPMovieControlStyleEmbedded,
MPMovieControlStyleFullscreen,
MPMovieControlStyleDefault = MPMovieControlStyleFullscreen
};
typedef NSInteger MPMovieControlStyle;
Constants
MPMovieControlStyleNone
No controls are displayed. Available in
iOS 3.2 and later. Declared in
MPMoviePlayerController.h.
MPMovieControlStyleEmbedded
Controls for an embedded view are displayed.
The controls include a start/pause
button, a scrubber bar, and a button
for toggling between fullscreen and
embedded display modes. Available in
iOS 3.2 and later. Declared in
MPMoviePlayerController.h.
MPMovieControlStyleFullscreen
Controls for fullscreen playback are displayed.
The controls include a start/pause
button, a scrubber bar, forward and
reverse seeking buttons, a button for
toggling between fullscreen and
embedded display modes, a button for
toggling the aspect fill mode, and a
Done button. Tapping the done button
pauses the video and exits fullscreen
mode. Available in iOS 3.2 and later.
Declared in MPMoviePlayerController.h.
MPMovieControlStyleDefault
Fullscreen controls are displayed by default.
Available in iOS 3.2 and later.
Declared in MPMoviePlayerController.h.
MPMovieFinishReason
You should set the control style, like myMovie.controlStyle = MPMovieControlStyleDefault; to add a control bar.
Define the controlStyle property on the MPMoviePlayerController object.
Constants describing the style of the playback controls.
enum {
MPMovieControlStyleNone,
MPMovieControlStyleEmbedded,
MPMovieControlStyleFullscreen,
MPMovieControlStyleDefault = MPMovieControlStyleFullscreen
};
typedef NSInteger MPMovieControlStyle;
Read more from here
Related
I want to add a video that shows up when i call it but not a typical (stretch-to-bounds) view
similar to the ProgressHud view that loads in the middle and darkens the background. how can I load a video say from LBYoutubeController or natively?
Thanks!
edit: ideally the video box will take up 60-70 % of the screen but again will be centered and overshadowing the background
You need to use a MPMoviePlayerController. You can set the controlStyle to MPMovieControlStyleEmbedded and this will give you a player with no controls. Set the scalingMode to MPMovieScalingModeFill to make the video conform to the frame you set for it regardless of the video's natural layout. Just add the movie player's view to your view hierarchy and you're good to go.
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] init];
moviePlayer.scalingMode = MPMovieScalingModeFill;
moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
[myView addSubview:moviePlayer.view];
To add a dimming view behind this:
UIView *back = [[UIView alloc]initWithFrame:myView.bounds];
back.backgroundColor = UIColor.blackColor;
back.alpha = 0.7;
[myView insertSubview:back belowSubview:myView];
How can I hide the volume bar in movie player and keep the other controls are appear (play, forward ... ) ? I want to show some videos that haven't any sounds, so the volume bar is totally will be useless.
can I do this ?
Thanks in advance
Set the controlStyle of the MPMoviePlayer to MPMovieControlStyleNone.
moviePlayer.controlStyle = MPMovieControlStyleNone;
But this will hide all the controls from the view.
After setting to MPMovieControlStyleNone, if you want to display the play/pause option and seek bar, You need to add custom controls.
(I did it before, I used slider as seek bar and and placed it in a UIToolBar along with a Tool bar button . Button is for play/pause option)
MPMovieControlStyle
Constants describing the style of the playback controls.
enum { MPMovieControlStyleNone, MPMovieControlStyleEmbedded,
MPMovieControlStyleFullscreen, MPMovieControlStyleDefault =
MPMovieControlStyleFullscreen }; typedef NSInteger
MPMovieControlStyle;
Constants
MPMovieControlStyleNone
No controls are displayed.
Available in iOS 3.2 and later.
Declared in MPMoviePlayerController.h.
MPMovieControlStyleEmbedded
Controls for an embedded view are displayed. The controls include a start/pause button, a scrubber bar, and a button for toggling
between fullscreen and embedded display modes.
Available in iOS 3.2 and later.
Declared in MPMoviePlayerController.h.
MPMovieControlStyleFullscreen
Controls for fullscreen playback are displayed. The controls include a start/pause button, a scrubber bar, forward and reverse
seeking buttons, a button for toggling between fullscreen and embedded
display modes, a button for toggling the aspect fill mode, and a Done
button. Tapping the done button pauses the video and exits fullscreen
mode.
Available in iOS 3.2 and later.
Declared in MPMoviePlayerController.h.
MPMovieControlStyleDefault
Fullscreen controls are displayed by default.
Available in iOS 3.2 and later.
Declared in MPMoviePlayerController.h.
Refer MPMoviePlayerController
There is no way to do this except "hacking" the subviews. You may subclass MPMoviePlayerViewController and iterate the subviews. In one of my apps, I used code like this to remove things like the media controls:
- (void)removeMediaControls
{
#try
{
// Search for the MPSwipeableView
for (UIView *view1 in [self.view subviews])
{
// Check for the MPSwipeableView
if ([[[view1 class] description] isEqualToString:#"MPSwipableView"])
{
// Search for the MPVideoBackgroundView
for (UIView *view2 in [view1 subviews])
{
// Check for the MPVideoBackgroundView
if ([[[view2 class] description] isEqualToString:#"MPVideoBackgroundView"])
{
// Search for the UIView
for (UIView *view3 in [view2 subviews])
{
// Check for the MPWildcatFullScreenVideoOverlay
if ([[[view3 class] description] isEqualToString:#"MPWildcatFullScreenVideoOverlay"])
{
// Search for the MPWildcatFullScreenNavigationBar
for (UIView *view4 in [view3 subviews])
{
// Check for the UIImageView
if ([[[view4 class] description] isEqualToString:#"UIImageView"])
{
// Remove the overlay
[view4 removeFromSuperview];
}
}
}
}
}
}
}
}
}
#catch (NSException * e)
{
}
}
The code above is too old, it worked with iOS 4.3. On iOS 5 and iOS 6 the view hierarchy changed, so you may have to updated your code with every new iOS version. See also: MPMoviePlayerController hide volume slider
one small issue in my movieplayer..![movieplayer shows like this]
1: http://i.stack.imgur.com/WujxB.png but i want to show as below screenshot
mycode:
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
moviePlayerController.view.frame = CGRectMake(0,0,320,460);
moviePlayerController.fullscreen = YES;
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
Those are the default controls for fullscreen playback, since you set moviePlayerController.fullscreen = YES.
The controls you want are for embedded playback, not fullscreen.
What you want is moviePlayerController.controlStyle = MPMovieControlStyleEmbedded; but you can only use it if your movie is embbeded in one of your views. Then you would have the controls that you want, including a toggle between fullscreen and embedded.
You have to use MPMoviePlayerViewController instead of using MPMoviePlayerController.
I have a very simple move view that upon loading, jumps right into a video. The movie controls show up great and everything. But I get a problem when I rotate to landscape, the movie player width doesn't adjust with the view, so it remains too narrow.
If I'm in the full screen mode, where my nav bar goes away and stuff, I can rotate and it works no problem.
So how can I either adjust the width of the viewer when I switch to landscape, OR just start the view in the fullscreen mode?
Note: [mpPlay setFullscreen:YES]; isn't doing it for me.
Here is the rest of my code:
- (void)viewDidLoad
{
NSBundle * bundle = [NSBundle mainBundle];
NSString * moviePath = [bundle pathForResource:#"movie" ofType:#"mp4"];
NSURL * movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController * mpPlay = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[mpPlay.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: mpPlay.view];
[mpPlay play];
}
Override didRotateFromInterfaceOrientation: in your view controller and change the movie player to the new width and height.
Any one know how to keep the MPMoviePlayerController visible while the movie is loading.
Or before the movie is ready to play?
For some reason it only shows up when the Movie is ready to play.
Heres what I have.
mp = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:[mp view]];
mp.view.frame = mediaPlayerContainer.frame;
[mediaPlayerContainer removeFromSuperview];
mp.useApplicationAudioSession = YES;
mp.shouldAutoplay = YES;
mp.controlStyle = MPMovieControlStyleFullscreen;
My mediaPlayerContainer is just a dummy container so I can visually build a frame for my mp view.
The only other thing I can think of doing is using a "screenshot" of a player as a place holder. And removing it if the player is in "play" mode.
That seems janky though.
Thnks.
MPMoviePlayerController has a backgroundView property...
You can add an UIImageView to it and it should be displayed while it is loading the video.