I am using MPMoviePlayerController to play a video inside my iPad application. I am using below code snippet to add the movie player to view:
self.player =[[MPMoviePlayerController alloc] initWithContentURL: urlVideo];
[self.player prepareToPlay];
[self.player setMovieSourceType:MPMovieSourceTypeStreaming];
[self.player.view setBounds:CGRectMake(0, 0, 512, 374)];
if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
{
NSLog(#"landscape..");
[self.player.view setCenter:CGPointMake(512, 374)];
}
else{
NSLog(#"portrait.");
[self.player.view setCenter:CGPointMake(384, 502)];
}
self.player.controlStyle=MPMovieControlStyleEmbedded;
[self.player setScalingMode:MPMovieScalingModeAspectFit];
self.player.shouldAutoplay=YES;
[self.viewController.view addSubview: self.player.view];
self.btnCloseNormalMode= [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnCloseNormalMode setImage:[UIImage imageNamed:#"close_button_icon.png"] forState:UIControlStateNormal];
[self.btnCloseNormalMode setFrame:CGRectMake(470, 5, 32, 32)];
[self.btnCloseNormalMode addTarget:self action:#selector(onSmallCloseBtnTap) forControlEvents:UIControlEventTouchUpInside];
[self.player.view addSubview:self.btnCloseNormalMode];
But as understandable from the above code, its adding on the view abruptly. Suddenly one black view appears on the view which is not user friendly. We want to add the movie player view with some animation to make it smooth. Can I make the movie player view to come with a animation like how modal appears or any suitable UIView animation. But I have no idea regarding animation. Could someone please help me on this !!! Thanks.
Related
I am using a UICollectionView with paging feature and adding AVPlayer on each cell to play video with URL, but my collection view is giving a strange behavior. When it loads the first cell and video starts playing then it is fine but when I slide and load the next cell then both videos are playing together, while I want the previous one to stop playing. I tried many thing to resolve this issue but nothing is working, someone please suggest me something to work with this. My code of custom cell class where I am adding AVPlayer is given below:
[self.avPlayer pause];
self.avPlayer = nil;
[self.avLPlayer removeFromSuperlayer];
CGRect videoRect;
videoRect = CGRectMake(0,0,[[UIScreen mainScreen]bounds].size.width,[[UIScreen mainScreen]bounds].size.height);
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[indicator startAnimating];
[indicator bringSubviewToFront:_viewForShowVideo];
UIWindow* currentWindow = [UIApplication sharedApplication].keyWindow;
indicator.frame = CGRectMake(currentWindow.frame.size.width/2, _viewForShowVideo.frame.size.height/2, 20, 20);
[_viewForShowVideo addSubview:indicator];
self.avPlayerItem = [AVPlayerItem playerItemWithURL:Url];
self.avPlayer = [AVPlayer playerWithPlayerItem:self.avPlayerItem];
self.avLPlayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
[_avLPlayer setFrame:videoRect];
[_avLPlayer setBackgroundColor:[UIColor blackColor].CGColor] ;
[_avLPlayer setVideoGravity:AVLayerVideoGravityResizeAspect];
_avLPlayer.opacity = 1;
_avPlayer.allowsExternalPlayback = NO;
[_avPlayer setMuted:NO];
_avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[AVAudioSession sharedInstance]setCategory: AVAudioSessionCategoryPlayback error: nil];
[_viewForShowVideo.layer addSublayer:_avLPlayer];
[_avPlayer play];
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnPlayer:)];
[_viewForShowVideo addGestureRecognizer:tgr];
[_viewForShowVideo setBackgroundColor:[UIColor greenColor]];
I need to add a custom button to the AVPlayerViewController that will appear in both fullscreen and non-fullscreen for an app running iOS 8.
Adding a button to the AVPlayerViewController.view or the containing view will work for non-fullscreen but when the player switches to fullscreen the button is no longer visible. I have found that if I add a button to the AVPlayerViewController.ContentOverlayView then it appears in fullscreen and non-fullscreen, but then it doesn't appear that the ContentOverlayView responds to any touches so the button cannot be clicked. Does anyone know of a different place to add the button or a way to make the ContentOverlayView respond to touches?
Example Code
AVPlayerViewController *playerView = [[AVPlayerViewController alloc] init];
playerView.player = [AVPlayer playerWithURL:movieURL];
CGRect viewInsetRect = CGRectInset ([self.view bounds],
kMovieViewOffsetX,
kMovieViewOffsetY );
/* Inset the movie frame in the parent view frame. */
[[playerView view] setFrame:viewInsetRect];
[self.view addSubview: [playerView view]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor yellowColor];
btn.frame = CGRectMake(50, 50, 200, 75);
[btn addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:YES];
[btn setEnabled:YES];
[playerView.contentOverlayView addSubview:btn];
I managed to get this working by adding the following to my AVPlayerViewController in IB:
An overlay view with a button inside it
Add constraints to center the button in the overlay view
Created an IBOutlet to the view (I called it 'overlay')
Created an action from the button to AVPlayerViewController that prints something so that we can see that tapping the button is working
Dragged the overlay view out of the view hierarchy and onto the top bar of the view controller (appears as a view icon in top bar of the view controller along with the exit icon etc)
and then in the code for the AVPlayerViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.overlay)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.overlay.frame = self.view.bounds
}
#IBAction func playBtnTapped(sender: AnyObject) {
println("PlayBtnTapped")
}
The button is now centered in fullscreen/normal viewing and the button works
I had the same predicament just now.
Instead of:
[playerView.contentOverlayView addSubview:btn];
use (after you add the playerView)
[self.view addSubview:btn];
This will add the button in the minimised player.
Now for the fullscreen I didn't found any other reliable method of checking if the player went fullscreen or back, so here is a a solution:
self.windowChecker = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(checkIfFullscreen) userInfo:nil repeats:YES];
and this:
-(void)checkIfFullscreen{
NSLog(#"videoBounds\n %f %f ", self.avVideoPlayer.contentOverlayView.frame.size.width,self.avVideoPlayer.contentOverlayView.frame.size.height);
if ([self playerIsFullscreen]) { // check the frame on this
for(UIWindow* tempWindow in [[UIApplication sharedApplication]windows]){
for(UIView* tempView in [tempWindow subviews]){
if ([[tempView description] rangeOfString:#"UIInputSetContainerView"].location != NSNotFound){
UIButton *testB = [[UIButton alloc] initWithFrame: CGRectMake(20, 20, 400, 400)];
testB.backgroundColor = [UIColor redColor];
[testB addTarget:self action:#selector(buttonTouch) forControlEvents:UIControlEventTouchDown];
[tempView addSubview:testB];
break;
}
}
}
}
}
I know this is a not a nice way to do it, but it is the only way I managed to add some custom UI that also receives touch events on the player.
Cheers!
i created a MPMoviePlayerViewController in my UIView (not in my UIVIewController!) like this:
self.playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 70, 125, 100)];
[self.playButton setBackgroundImage:[UIImage imageNamed:#"video_play.png"] forState:UIControlStateNormal];
[self.playButton addTarget:self action:#selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
self.playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
self.playerViewController.moviePlayer.fullscreen = NO;
self.playerViewController.view.frame = CGRectMake(0, 0, 320, 200);
[self.playerViewController.moviePlayer prepareToPlay];
self.playerViewController.moviePlayer.shouldAutoplay = NO;
self.playerViewController.view.backgroundColor = [UIColor yellowColor];
[self addSubview:self.playerViewController.view];
[self.playerViewController.view addSubview:self.playButton];
}
- (void)buttonPressed:(id)sender{
(NSLog(#"Click"));
[self.playerViewController.moviePlayer setFullscreen:YES animated:YES];
[self.playerViewController.moviePlayer play];
}
As you can see i added a Button on the videoView, beacause this part should only be a Preview and when the user clicks on the Button the MPMoviePlayerViewController should animate to fullscreen and start playing, and when the Video is finished it should go back to the Preview View. Everything works so far, but i have two Problems:
First Problem:
Everytime i open the View my StatusBar becomes hidden and it looks like this:
So i set in my viewWillAppear and viewDidAppear of my UIViewController:
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
This works, but now the statusbar gets hidden and immediately appears again, that looks ugly, any chance to solve this Problem?
Second Problem:
When i click on the Custom Button the Video gets fullscreen and everything works correct! But when i press the DONE Button of the Video everything goes back to the Preview Screen and it looks like this: The StatusBar is hidden, the navigationbar is also broken and there is a lot of black Space above the Video, whats the Problem here?
Ok i found a solution for this Problem, its a little bit hacky but i found no other solution. In my ViewController i do:
- (void)viewDidLoad {
[super viewDidLoad];
float delay = 0.1;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].statusBarHidden = NO;
});
and for the case User taps back Button i do:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[UIApplication sharedApplication].statusBarHidden = NO;
}
I want one application that to play sound file and show it in UIProgress when to play music.
I have to use UISlider instead UIProgress (for example I want can further this sound or Rewind it with UISlider like music player in iphone)
I could create one application that play music with UIProgress but I want could further or rewind this music with touch and push.
please guide me and tell me about it.
- (void)viewDidLoad
{
[super viewDidLoad];
durationControl= [[UISlider alloc]initWithFrame:CGRectMake(65, 250 , 130, 10)];
// durationControl.maximumValue = 130;
[durationControl addTarget:self action:#selector(changeDuration) forControlEvents:UIControlEventValueChanged];
[durationControl setThumbImage:[self imageNamed:#"trackbutton"] forState:UIControlStateNormal];
[durationControl setThumbImage:[self imageNamed:#"trackbutton1"] forState:UIControlStateHighlighted];
[self.view addSubview:durationControl];
}
-(void)ffwAudio{
[player changeDuration:durationControl.value+=5];
durationControl.value = player.currentTime;
}
-(void)bwAudio{
if (durationControl.value>=5) {
[player changeDuration:durationControl.value-=5];
durationControl.value = player.currentTime;
}
-(void)changeDuration:(double)value{
player.currentTime = value;
durationControl.value = player.currentTime;
}
I am using the MPMoviePlayerController to play a movie from the web.
Depending on the table row selected a different movie is loaded. However, i would like the MPMoviePlayerController to disappear (or hide itself), once a new row is selected.
Here is the code that gets called to play my movie and to, eventually, hide it
- (IBAction) playMovie{
NSURL *url = [NSURL URLWithString:vidMovie];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
moviePlayer.view.frame = vidPlayer.frame;// CGRectMake(64, 624, 640, 360);
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
}
- (void) hidePlayer{
[moviePlayer stop];
[moviePlayer release];
}
in my .h i declare moviePlayer as such
MPMoviePlayerController *moviePlayer;
I've tried setting the moviePlayer frame height and width to 0 but that still shows the play button.
I've tried the variables .hidden and .opaque and still i get nothing
Could anyone help me figure out what i might have forgotten. Any help would be greatly appreciated!
Thanks
I found it after trying all sorts of different things...
Seems i needed to retain my moviePlyer to be able to remove it in another part of my code.
If anyone has the same problem, here is my solution!
- (IBAction) playMovie{
NSURL *url = [NSURL URLWithString:vidMovie];
moviePlayer = [[[MPMoviePlayerController alloc]initWithContentURL:url] retain];
moviePlayer.view.frame = vidPlayer.frame;// CGRectMake(64, 624, 640, 360);
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
}
- (void) hidePlayer{
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
}
Hope this might be able to help othes!