How do I add a volume control in my MPMoviePlayerController instance - ios

The below is the code which opens a video on a button press. Everything is fine but I want to add a volume control for my video player. Is there a default feature from the MPMoviePlayerController that I can just enable, or do I have to use the slider from the objects library , drag and drop it, and the adjust the volume according to the slider value?
NSString *url = [[NSBundle mainBundle] pathForResource:#"space-time" ofType:#"mp4"];
moviePlayer= [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[self.view addSubview:moviePlayer.view];
moviePlayer.fullscreen = YES;
[moviePlayer play];

to add a volume view use this code
make a uiview in your viewcontroller of about 200width by 30height,
drop outlet for that view (view1) after that
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:self.view1.bounds] ;
[self.view1 addSubview:volumeView];
volume slider will show up in that view1

Related

Embedding video into view controller

I am trying to add a video to a view controller. I tried using the AVPlayerViewController but i was not able to achieve the result i wanted. I do not want the video to open in a new view controller. Instead i want it to be playing in the background. There will be buttons in the foreground and the user can press it. Here is a picture of how the page is supposed to look like.
The globe will be rotating and the user can login or sign-up. From what i have researched i understand that it is only possible through using a gif. But how is youtube able to achieve this? Is it possible for me to do so?
you need to add video first to your main view using Avplayer like following:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Untitled" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
avPlayer = [AVPlayer playerWithURL:fileURL];
layer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view.layer addSublayer: layer];
Then u need to create UiView with 2 button as you specify "Login" and "Sign-Up" and make Uivew BackgroundColor Property:clear color. and add this code to place view on your main view.
CGRect viewframe=self.buttonView.frame;
viewframe.origin.x=0;
viewframe.origin.y=0;
viewframe.size.height=self.view.frame.size.height;
viewframe.size.width=self.view.frame.size.width;
self.bnuttonView.frame=viewframe;
[self.view addSubview:self.buttonView];
In this way your video will play in background and Login and Sign-up button are working in foreground.

Using AVPlayer to view transparent video

I am trying to view a video witch has an alpha channel (the background is transparent). The only problem is that I don't seem to get how to make the background of the player transparent. I know I have to use AVplayer, but I can't access it's .view property. How can I add it to the subview and add a layer?
NSString *path = [NSString stringWithFormat:#"%#%#", [[NSBundle mainBundle] resourcePath], #"/New Project 5.m4v"];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
moviePlayer = [[AVPlayer alloc] initWithURL:filePath];
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:moviePlayer];
self.playerLayer.frame = self.view.bounds;
moviePlayer.view.alpha = 0.3;
[moviePlayer.layer addSublayer:playerLayer];
[moviePlayer play];
The iOS SDK does not properly support alpha channel video playback. That applies for AVFramework as well as the MediaPlayer Framework. Video material that contains an alpha channel will not work as you expect it to when using Apple's API.
And as you actually show within your code, AVPlayer does not use a UIView as its surface for playing videos but a subclass of CALayer, AVLayer.
You will need to rethink your application design or chose a different playback SDK.

ios6 moviePlayer first frame not appearing upon creation

I have methods inside my open class controller that instantiates a moviePlayer, which is set to 'autoPlay = NO';
I have added the movieplayer.view as a subview of the controllers view, configured it and created a full screen button on top for starting the video. Since iOS4.3, this has been working fine. The button is transparent and the first frame of the video showed through ( which was a picture of a custom Automoble Auto-Start button).
Since iOS6, I only get a black screen.
Clicking the image-button does start the video as it should; calls [moviePlayer play]
Has something changed that I have not taken into consideration?
I have provided the two sections of code I think are necessary.
#define INTRO_MOVIE #"Intro.mov"
-(void)viewDidLoad
{
if(SHOULD_PLAY_INTRO_VIDEO)//Debug switch to ignore the intro video
{
// Prepare the movie and player
[self configureIntroMoviePlayer];
[self.view addSubview:moviePlayer.view];
[self.view bringSubviewToFront:moviePlayer.view];
// Add and Show the Start Button to start the App
[self configureStartButton];
[self.view addSubview:startButton];
}
}
-(void)configureIntroMoviePlayer
{
LOGINFO
// Prepare the Intro Video
NSString *pathToIntroVideo = [ mainFilePath_ stringByAppendingPathComponent: INTRO_MOVIE];
NSURL *URLToIntroVideo = [NSURL fileURLWithPath:pathToIntroVideo];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:URLToIntroVideo];
[moviePlayer setShouldAutoplay:NO];
moviePlayer.view.frame = CGRectMake(0, -20, 1024, 768);
[moviePlayer setControlStyle:MPMovieControlStyleNone];
//fixing video brightness Difference with iPad2
if(isIpad2)
{
moviePlayer.backgroundView.backgroundColor = [UIColor blackColor];
moviePlayer.view.alpha = .99;
}
// Create the sKip button for cancelling the Intro Video
skipIntro = [UIButton buttonWithType:UIButtonTypeCustom];
[skipIntro showsTouchWhenHighlighted];
skipIntro.frame = CGRectMake(900, 20, 111, 57);
[skipIntro addTarget:self action:#selector(skipIntroWasPressed) forControlEvents:UIControlEventTouchUpInside];
}
I am not sure why I got a -1 rating for this question for lack of research or clarity?
Maybe I do not know the proper usage of this forum.
I apologize.
I did find that adding [moviePlayer prepareToPlay] solved the problem. Like I said, it was odd that the first frame always showed up prior to iOS 6.
Have you tried:
[moviePlayer.view addSubView:startButton];

"Done" button in MPMoviePlayerController doesn't work when full screen mode

Well, I could find many questions about the same or similar questions and answers... However, nothing could help me. Only when I don't use the property "controlStyle" as "MPMovieControlStyleFullscreen", "done" button works. I tried this way..
MPMoviePlayerController *mpMoviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"https://dl.dropbox.com/u/14218997/thxq.mp4"]];
mpMoviePlayerController.controlStyle = MPMovieControlStyleNone;
[mpMoviePlayerController setUseApplicationAudioSession:NO];
[mpMoviePlayerController setScalingMode:MPMovieScalingModeAspectFit];
[mpMoviePlayerController setFullscreen:YES animated:YES];
[mpMoviePlayerController.view setFrame:CGRectMake(0, 0, 1024, 768)];
[[globalSingleton paintingView] addSubview:mpMoviePlayerController.view];
[mpMoviePlayerController prepareToPlay];
[mpMoviePlayerController play];
mpMoviePlayerController.controlStyle = MPMovieControlStyleFullscreen;
or this way..
MPMoviePlayerController *mp;
MPMoviePlayerViewController *mpVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:#"https://dl.dropbox.com/u/14218997/thxq.mp4"]];
mp = [mpVC moviePlayer];
mp.controlStyle = MPMovieControlStyleFullscreen;
mp.fullscreen = NO;
mp.useApplicationAudioSession = NO;
mp.view.frame = CGRectMake(0, 0, 1024, 768);
[[globalSingleton paintingView] addSubview:mp.view];
([globalSingleton paintingView] is just for representing main view. I already checked there's no problem on it.)
Please share what u know about this problem. Thx in advance!
Based on your code, It looks to me that your intention is to just have a fullscreen movie player take over the screen. In that case, you're probably better off using the MPMoviePlayerViewController, but you need to present it as a modal view controller using your current view controller like this:
MPMoviePlayerViewController *movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:someVideoURL];
movieViewController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
// Self is the UIViewController you are presenting the movie player from.
[self presentMoviePlayerViewControllerAnimated:movieViewController];
The "Done" button should work properly in this case, and dismiss the modal MPMoviePlayerViewController
On the other hand, If you're more interested in animating the movie from where you've added it in the current view hierarchy, here is an example of what I have done to achieve this:
I also found that setting the MPMoviePlayerController's controlStyle property to MPMovieControlStyleFullscreen had the same result–the "Done" button did not close the MPMoviePlayerController. When I changed it to MPMovieControlStyleDefault, it worked as expected. However, in my case, I'm adding the MPMoviePlayerController's view as a thumbnail-sized subview to the currently displayed UIViewController's view. I initially have the MPMoviePlayerController's controlStyle set to MPMovieControlStyleNone. I have a custom UIButton on top of the thumbnail-sized movie player view, and in the button's action method, I'm changing the controlStyle of the MPMoviePlayerController to MPMovieControlStyleDefault, then calling setFullscreen:animated: which animates the movie player view into fullscreen mode. Then, tapping the "done" button properly animates the player back into place in the thumbnail-sized subview of my UIViewController's view. Here is an example:
Initial instantiation of my MPMoviePlayerController:
// My moviePlayerController is a property
self.moviePlayer = [[MPMoviePlayerController alloc] initWithURL:videoURL];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.shouldAutoplay = NO;
// Add the moviePlayer's view as a subview of a my UIViewController's view.
moviePlayer.view.frame = CGRectMake(20, 20, 160, 90);
[self.view addSubview:moviePlayer.view];
Note: I also added a custom UIButton (to invoke fullscreen playback) on top of my moviePlayer's view and set it's action to call the following method:
- (void)buttonAction:(UIButton *)sender
{
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
}
Note: I also observe and handle the MPMoviePlayerWillExitFullscreenNotification where I set the controlStyle back to MPMovieControlStyleNone.

Play Video as the Background on iPhone on button click?

I want to create an app which has a default image as the background of a view that has many buttons on it. When a user clicks on any of the buttons, a unique video plays. The buttons are gonna be almost transparent so, I want the video to play in the background of the same view itself (replacing/playing above the image) and not open in a new Player(as it does using the media player). Has anyone tried it, or does anyone have any idea?
Thank You!
#import <MediaPlayer/MPMoviePlayerController.h>
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath: moviePath]];
moviePlayer.controlStyle = MPMovieControlStyleNone;
[RootViewController.view insertSubview: moviePlayer.view aboveSubview: myView];
[moviePlayer play];

Resources